Package de.odysseus.calyxo.base.test

Source Code of de.odysseus.calyxo.base.test.TestPageContext

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.base.test;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;

import org.apache.commons.el.ExpressionEvaluatorImpl;
import org.apache.commons.el.VariableResolverImpl;

/**
*
* @author Christoph Beck
*/
public class TestPageContext extends PageContext {
  private static ExpressionEvaluator evaluator =
    new ExpressionEvaluatorImpl();
 
  private HttpServletRequest request;
  private Map attributes;

  /**
   * Creates a new instance.
   */
  public TestPageContext() {
    this(new TestRequest(), new HashMap());
  }

  /**
   * Creates a new instance.
   *
   * @param request The request we're in
   */
  public TestPageContext(HttpServletRequest request) {
    this(request, new HashMap());
  }

  /**
   * Creates a new instance.
   *
   * @param request The request we're in
   * @param attributes A java.util.Map containing entries whose
   *            keys are strings and whose values are objects.
   *            It represents the mapping from attribute names
   *            to attribute values.
   */
  public TestPageContext(HttpServletRequest request, Map attributes) {
    super();

    this.request = request;
    this.attributes = attributes;
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#initialize(javax.servlet.Servlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse, java.lang.String, boolean, int, boolean)
   */
  public void initialize(
    Servlet arg0,
    ServletRequest arg1,
    ServletResponse arg2,
    String arg3,
    boolean arg4,
    int arg5,
    boolean arg6)
    throws IOException, IllegalStateException, IllegalArgumentException {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#release()
   */
  public void release() {
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#getSession()
   */
  public HttpSession getSession() {
    return request.getSession();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#getPage()
   */
  public Object getPage() {
    return null;
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#getRequest()
   */
  public ServletRequest getRequest() {
    return request;
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#getResponse()
   */
  public ServletResponse getResponse() {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#getException()
   */
  public Exception getException() {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#getServletConfig()
   */
  public ServletConfig getServletConfig() {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#getServletContext()
   */
  public ServletContext getServletContext() {
    return getSession().getServletContext();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#forward(java.lang.String)
   */
  public void forward(String arg0) throws ServletException, IOException {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#include(java.lang.String)
   */
  public void include(String arg0) throws ServletException, IOException {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#include(java.lang.String, boolean)
   */
  public void include(String arg0, boolean arg1) throws ServletException, IOException {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Exception)
   */
  public void handlePageException(Exception arg0) throws ServletException, IOException {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Throwable)
   */
  public void handlePageException(Throwable arg0) throws ServletException, IOException {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object)
   */
  public void setAttribute(String arg0, Object arg1) {
    attributes.put(arg0, arg1);
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object, int)
   */
  public void setAttribute(String arg0, Object arg1, int arg2) {
    switch (arg2) {
      case PAGE_SCOPE: setAttribute(arg0, arg1);
      break;
      case REQUEST_SCOPE: getRequest().setAttribute(arg0, arg1);
      break;
      case SESSION_SCOPE: getSession().setAttribute(arg0, arg1);
      break;
      case APPLICATION_SCOPE: getServletContext().setAttribute(arg0, arg1);
      break;
    }
    throw new IllegalArgumentException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String)
   */
  public Object getAttribute(String arg0) {
    return attributes.get(arg0);
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String, int)
   */
  public Object getAttribute(String arg0, int arg1) {
    switch (arg1) {
      case PAGE_SCOPE: return getAttribute(arg0);
      case REQUEST_SCOPE: return getRequest().getAttribute(arg0);
      case SESSION_SCOPE: return getSession().getAttribute(arg0);
      case APPLICATION_SCOPE: return getServletContext().getAttribute(arg0);
    }
    throw new IllegalArgumentException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#findAttribute(java.lang.String)
   */
  public Object findAttribute(String arg0) {
    Object result = attributes.get(arg0);
    if (result == null) {
      result = getRequest().getAttribute(arg0);
      if (result == null) {
        result = getSession().getAttribute(arg0);
        if (result == null) {
          result = getServletContext().getAttribute(arg0);
        }
      }
    }
    return result;
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String)
   */
  public void removeAttribute(String arg0) {
    attributes.remove(arg0);
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String, int)
   */
  public void removeAttribute(String arg0, int arg1) {
    switch (arg1) {
      case PAGE_SCOPE: removeAttribute(arg0); break;
      case REQUEST_SCOPE: getRequest().removeAttribute(arg0); break;
      case SESSION_SCOPE: getSession().removeAttribute(arg0); break;
      case APPLICATION_SCOPE: getServletContext().removeAttribute(arg0); break;
    }
    throw new IllegalArgumentException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#getAttributesScope(java.lang.String)
   */
  public int getAttributesScope(String arg0) {
    if (getAttribute(arg0, PAGE_SCOPE) != null) {
      return PAGE_SCOPE;
    }
    if (getAttribute(arg0, REQUEST_SCOPE) != null) {
      return REQUEST_SCOPE;
    }
    if (getAttribute(arg0, SESSION_SCOPE) != null) {
      return SESSION_SCOPE;
    }
    if (getAttribute(arg0, APPLICATION_SCOPE) != null) {
      return APPLICATION_SCOPE;
    }
    return 0;
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#getAttributeNamesInScope(int)
   */
  public Enumeration getAttributeNamesInScope(int arg0) {
    switch (arg0) {
      case PAGE_SCOPE: return Collections.enumeration(attributes.keySet());
      case REQUEST_SCOPE: return getRequest().getAttributeNames();
      case SESSION_SCOPE: return getSession().getAttributeNames();
      case APPLICATION_SCOPE: return getServletContext().getAttributeNames();
    }
    throw new IllegalArgumentException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#getOut()
   */
  public JspWriter getOut() {
    throw new UnsupportedOperationException();
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#getExpressionEvaluator()
   */
  public ExpressionEvaluator getExpressionEvaluator() {
    return evaluator;
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.JspContext#getVariableResolver()
   */
  public VariableResolver getVariableResolver() {
    return new VariableResolverImpl(this);
  }
}
TOP

Related Classes of de.odysseus.calyxo.base.test.TestPageContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.