View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.context.contexts;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.jsp.PageContext;
20  
21  import net.sf.morph.reflect.BeanReflector;
22  import net.sf.morph.reflect.reflectors.PageContextAttributeReflector;
23  
24  /**
25   * <p>
26   * Context for JSP pages that evaluates page context attributes, request
27   * parameters, request attributes, session attributes, and application
28   * attributes, in that order. Exposing request parameters in this manner is
29   * somewhat unusual, so
30   * {@link HttpServletContext#setReflectingRequestParameters(boolean)}can be
31   * called to turn this behavior off.
32   * </p>
33   * 
34   * <p>
35   * This class is <em>not</em> threadsafe, but since usually only a single
36   * thread is accessing a request at any given time, this should be OK.
37   * </p>
38   * 
39   * @author Matt Sgarlata
40   * @since Dec 4, 2004
41   */
42  public class JspContext extends ReflectorHierarchicalContext {
43  	
44  	private static final BeanReflector PAGE_CONTEXT_ATTRIBUTE_REFLECTOR = new PageContextAttributeReflector();
45  	
46  	private HttpServletContext httpServletContext;
47  	
48  	private PageContext pageContext;
49  	
50  	public JspContext() {
51  		httpServletContext = new HttpServletContext();
52  		
53  		this.setBeanReflector(PAGE_CONTEXT_ATTRIBUTE_REFLECTOR);
54  		this.setParentContext(httpServletContext);
55  	}
56  	
57  	public JspContext(PageContext pageContext) {
58  		this();
59  		setPageContext(pageContext);
60  	}
61  	
62  	/**
63  	 * @return Returns the pageContext.
64  	 */
65  	public PageContext getPageContext() {
66  		return pageContext;
67  	}
68  	/**
69  	 * @param pageContext The pageContext to set.
70  	 */
71  	public void setPageContext(PageContext pageContext) {
72  		this.pageContext = pageContext;
73  		this.httpServletContext.setRequest((HttpServletRequest) pageContext.getRequest());
74  		this.setDelegate(pageContext);
75  	}
76  	public boolean isReflectingRequestParameters() {
77  		return httpServletContext.isReflectingRequestParameters();
78  	}
79  	public void setReflectingRequestParameters(
80  		boolean reflectingRequestParameters) {
81  		httpServletContext.setReflectingRequestParameters(reflectingRequestParameters);
82  	}
83  }