001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.base.test;
017:
018: import java.io.IOException;
019: import java.util.Collections;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import javax.servlet.Servlet;
025: import javax.servlet.ServletConfig;
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028: import javax.servlet.ServletRequest;
029: import javax.servlet.ServletResponse;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpSession;
032: import javax.servlet.jsp.JspWriter;
033: import javax.servlet.jsp.PageContext;
034: import javax.servlet.jsp.el.ExpressionEvaluator;
035: import javax.servlet.jsp.el.VariableResolver;
036:
037: import org.apache.commons.el.ExpressionEvaluatorImpl;
038: import org.apache.commons.el.VariableResolverImpl;
039:
040: /**
041: *
042: * @author Christoph Beck
043: */
044: public class TestPageContext extends PageContext {
045: private static ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl();
046:
047: private HttpServletRequest request;
048: private Map attributes;
049:
050: /**
051: * Creates a new instance.
052: */
053: public TestPageContext() {
054: this (new TestRequest(), new HashMap());
055: }
056:
057: /**
058: * Creates a new instance.
059: *
060: * @param request The request we're in
061: */
062: public TestPageContext(HttpServletRequest request) {
063: this (request, new HashMap());
064: }
065:
066: /**
067: * Creates a new instance.
068: *
069: * @param request The request we're in
070: * @param attributes A java.util.Map containing entries whose
071: * keys are strings and whose values are objects.
072: * It represents the mapping from attribute names
073: * to attribute values.
074: */
075: public TestPageContext(HttpServletRequest request, Map attributes) {
076: super ();
077:
078: this .request = request;
079: this .attributes = attributes;
080: }
081:
082: /* (non-Javadoc)
083: * @see javax.servlet.jsp.PageContext#initialize(javax.servlet.Servlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse, java.lang.String, boolean, int, boolean)
084: */
085: public void initialize(Servlet arg0, ServletRequest arg1,
086: ServletResponse arg2, String arg3, boolean arg4, int arg5,
087: boolean arg6) throws IOException, IllegalStateException,
088: IllegalArgumentException {
089: throw new UnsupportedOperationException();
090: }
091:
092: /* (non-Javadoc)
093: * @see javax.servlet.jsp.PageContext#release()
094: */
095: public void release() {
096: }
097:
098: /* (non-Javadoc)
099: * @see javax.servlet.jsp.PageContext#getSession()
100: */
101: public HttpSession getSession() {
102: return request.getSession();
103: }
104:
105: /* (non-Javadoc)
106: * @see javax.servlet.jsp.PageContext#getPage()
107: */
108: public Object getPage() {
109: return null;
110: }
111:
112: /* (non-Javadoc)
113: * @see javax.servlet.jsp.PageContext#getRequest()
114: */
115: public ServletRequest getRequest() {
116: return request;
117: }
118:
119: /* (non-Javadoc)
120: * @see javax.servlet.jsp.PageContext#getResponse()
121: */
122: public ServletResponse getResponse() {
123: throw new UnsupportedOperationException();
124: }
125:
126: /* (non-Javadoc)
127: * @see javax.servlet.jsp.PageContext#getException()
128: */
129: public Exception getException() {
130: throw new UnsupportedOperationException();
131: }
132:
133: /* (non-Javadoc)
134: * @see javax.servlet.jsp.PageContext#getServletConfig()
135: */
136: public ServletConfig getServletConfig() {
137: throw new UnsupportedOperationException();
138: }
139:
140: /* (non-Javadoc)
141: * @see javax.servlet.jsp.PageContext#getServletContext()
142: */
143: public ServletContext getServletContext() {
144: return getSession().getServletContext();
145: }
146:
147: /* (non-Javadoc)
148: * @see javax.servlet.jsp.PageContext#forward(java.lang.String)
149: */
150: public void forward(String arg0) throws ServletException,
151: IOException {
152: throw new UnsupportedOperationException();
153: }
154:
155: /* (non-Javadoc)
156: * @see javax.servlet.jsp.PageContext#include(java.lang.String)
157: */
158: public void include(String arg0) throws ServletException,
159: IOException {
160: throw new UnsupportedOperationException();
161: }
162:
163: /* (non-Javadoc)
164: * @see javax.servlet.jsp.PageContext#include(java.lang.String, boolean)
165: */
166: public void include(String arg0, boolean arg1)
167: throws ServletException, IOException {
168: throw new UnsupportedOperationException();
169: }
170:
171: /* (non-Javadoc)
172: * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Exception)
173: */
174: public void handlePageException(Exception arg0)
175: throws ServletException, IOException {
176: throw new UnsupportedOperationException();
177: }
178:
179: /* (non-Javadoc)
180: * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Throwable)
181: */
182: public void handlePageException(Throwable arg0)
183: throws ServletException, IOException {
184: throw new UnsupportedOperationException();
185: }
186:
187: /* (non-Javadoc)
188: * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object)
189: */
190: public void setAttribute(String arg0, Object arg1) {
191: attributes.put(arg0, arg1);
192: }
193:
194: /* (non-Javadoc)
195: * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object, int)
196: */
197: public void setAttribute(String arg0, Object arg1, int arg2) {
198: switch (arg2) {
199: case PAGE_SCOPE:
200: setAttribute(arg0, arg1);
201: break;
202: case REQUEST_SCOPE:
203: getRequest().setAttribute(arg0, arg1);
204: break;
205: case SESSION_SCOPE:
206: getSession().setAttribute(arg0, arg1);
207: break;
208: case APPLICATION_SCOPE:
209: getServletContext().setAttribute(arg0, arg1);
210: break;
211: }
212: throw new IllegalArgumentException();
213: }
214:
215: /* (non-Javadoc)
216: * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String)
217: */
218: public Object getAttribute(String arg0) {
219: return attributes.get(arg0);
220: }
221:
222: /* (non-Javadoc)
223: * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String, int)
224: */
225: public Object getAttribute(String arg0, int arg1) {
226: switch (arg1) {
227: case PAGE_SCOPE:
228: return getAttribute(arg0);
229: case REQUEST_SCOPE:
230: return getRequest().getAttribute(arg0);
231: case SESSION_SCOPE:
232: return getSession().getAttribute(arg0);
233: case APPLICATION_SCOPE:
234: return getServletContext().getAttribute(arg0);
235: }
236: throw new IllegalArgumentException();
237: }
238:
239: /* (non-Javadoc)
240: * @see javax.servlet.jsp.JspContext#findAttribute(java.lang.String)
241: */
242: public Object findAttribute(String arg0) {
243: Object result = attributes.get(arg0);
244: if (result == null) {
245: result = getRequest().getAttribute(arg0);
246: if (result == null) {
247: result = getSession().getAttribute(arg0);
248: if (result == null) {
249: result = getServletContext().getAttribute(arg0);
250: }
251: }
252: }
253: return result;
254: }
255:
256: /* (non-Javadoc)
257: * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String)
258: */
259: public void removeAttribute(String arg0) {
260: attributes.remove(arg0);
261: }
262:
263: /* (non-Javadoc)
264: * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String, int)
265: */
266: public void removeAttribute(String arg0, int arg1) {
267: switch (arg1) {
268: case PAGE_SCOPE:
269: removeAttribute(arg0);
270: break;
271: case REQUEST_SCOPE:
272: getRequest().removeAttribute(arg0);
273: break;
274: case SESSION_SCOPE:
275: getSession().removeAttribute(arg0);
276: break;
277: case APPLICATION_SCOPE:
278: getServletContext().removeAttribute(arg0);
279: break;
280: }
281: throw new IllegalArgumentException();
282: }
283:
284: /* (non-Javadoc)
285: * @see javax.servlet.jsp.JspContext#getAttributesScope(java.lang.String)
286: */
287: public int getAttributesScope(String arg0) {
288: if (getAttribute(arg0, PAGE_SCOPE) != null) {
289: return PAGE_SCOPE;
290: }
291: if (getAttribute(arg0, REQUEST_SCOPE) != null) {
292: return REQUEST_SCOPE;
293: }
294: if (getAttribute(arg0, SESSION_SCOPE) != null) {
295: return SESSION_SCOPE;
296: }
297: if (getAttribute(arg0, APPLICATION_SCOPE) != null) {
298: return APPLICATION_SCOPE;
299: }
300: return 0;
301: }
302:
303: /* (non-Javadoc)
304: * @see javax.servlet.jsp.JspContext#getAttributeNamesInScope(int)
305: */
306: public Enumeration getAttributeNamesInScope(int arg0) {
307: switch (arg0) {
308: case PAGE_SCOPE:
309: return Collections.enumeration(attributes.keySet());
310: case REQUEST_SCOPE:
311: return getRequest().getAttributeNames();
312: case SESSION_SCOPE:
313: return getSession().getAttributeNames();
314: case APPLICATION_SCOPE:
315: return getServletContext().getAttributeNames();
316: }
317: throw new IllegalArgumentException();
318: }
319:
320: /* (non-Javadoc)
321: * @see javax.servlet.jsp.JspContext#getOut()
322: */
323: public JspWriter getOut() {
324: throw new UnsupportedOperationException();
325: }
326:
327: /* (non-Javadoc)
328: * @see javax.servlet.jsp.JspContext#getExpressionEvaluator()
329: */
330: public ExpressionEvaluator getExpressionEvaluator() {
331: return evaluator;
332: }
333:
334: /* (non-Javadoc)
335: * @see javax.servlet.jsp.JspContext#getVariableResolver()
336: */
337: public VariableResolver getVariableResolver() {
338: return new VariableResolverImpl(this);
339: }
340: }
|