001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.webapp.parser;
035:
036: import com.icesoft.faces.util.IteratorEnumeration;
037:
038: import javax.el.ELContext;
039: import javax.faces.context.ExternalContext;
040: import javax.servlet.*;
041: import javax.servlet.http.HttpSession;
042: import javax.servlet.jsp.JspWriter;
043: import javax.servlet.jsp.PageContext;
044: import javax.servlet.jsp.el.ExpressionEvaluator;
045: import javax.servlet.jsp.el.VariableResolver;
046: import java.io.IOException;
047: import java.io.PrintWriter;
048: import java.util.Enumeration;
049: import java.util.Hashtable;
050: import java.util.Map;
051:
052: /**
053: * This is a stubbed out version of the PageContext. Only the minimum number of
054: * members required to support the parser are implemented.
055: */
056: public class StubPageContext extends PageContext {
057:
058: private Map attributes = new Hashtable();
059: private HttpSession httpSession;
060: private ExternalContext externalContext;
061: private ServletRequest servletRequest;
062: private ServletResponse servletResponse;
063:
064: /*
065: * @see javax.servlet.jsp.PageContext#initialize(javax.servlet.Servlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse, java.lang.String, boolean, int, boolean)
066: */
067: public void initialize(Servlet servlet,
068: ServletRequest servletRequest,
069: ServletResponse servletResponse, String errorPageURL,
070: boolean sessionSupport, int bufferSize, boolean autoFlush)
071: throws IOException, IllegalStateException,
072: IllegalArgumentException {
073: this .servletResponse = servletResponse;
074: externalContext = javax.faces.context.FacesContext
075: .getCurrentInstance().getExternalContext();
076: }
077:
078: /*
079: * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object)
080: */
081: public void setAttribute(String name, Object value) {
082: setAttribute(name, value, PAGE_SCOPE);
083: }
084:
085: /*
086: * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object, int)
087: */
088: public void setAttribute(String name, Object value, int scope) {
089: switch (scope) {
090: case PAGE_SCOPE:
091: attributes.put(name, value);
092: break;
093: case REQUEST_SCOPE:
094: externalContext.getRequestMap().put(name, value);
095: break;
096: case SESSION_SCOPE:
097: if (httpSession == null) {
098: throw new IllegalArgumentException(
099: "Session is not stablished for this request");
100: }
101: httpSession.setAttribute(name, value);
102: break;
103: case APPLICATION_SCOPE:
104: externalContext.getApplicationMap().put(name, value);
105: break;
106: default:
107: throw new IllegalArgumentException(scope
108: + " scope is not valid");
109: }
110: }
111:
112: /*
113: * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String)
114: */
115: public Object getAttribute(String name) {
116: return getAttribute(name, PAGE_SCOPE);
117: }
118:
119: /*
120: * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String, int)
121: */
122: public Object getAttribute(String name, int scope) {
123: switch (scope) {
124: case PAGE_SCOPE:
125: return attributes.get(name);
126: case REQUEST_SCOPE:
127: return externalContext.getRequestMap().get(name);
128: case SESSION_SCOPE:
129: if (httpSession == null) {
130: throw new IllegalArgumentException(
131: "Session is not stablished for this request");
132: }
133: return httpSession.getAttribute(name);
134: case APPLICATION_SCOPE:
135: return (externalContext.getApplicationMap().get(name));
136: default:
137: throw new IllegalArgumentException(scope
138: + " scope is not valid");
139: }
140: }
141:
142: /*
143: * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String)
144: */
145: public void removeAttribute(String name) {
146: removeAttribute(name, PAGE_SCOPE);
147: }
148:
149: /*
150: * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String, int)
151: */
152: public void removeAttribute(String name, int scope) {
153: switch (scope) {
154: case PAGE_SCOPE:
155: attributes.remove(name);
156: break;
157: case REQUEST_SCOPE:
158: externalContext.getRequestMap().remove(name);
159: break;
160: case SESSION_SCOPE:
161: if (httpSession == null) {
162: throw new IllegalArgumentException(
163: "Session is not stablished for this request");
164: }
165: httpSession.removeAttribute(name);
166: break;
167: case APPLICATION_SCOPE:
168: externalContext.getApplicationMap().remove(name);
169: break;
170: default:
171: throw new IllegalArgumentException(scope
172: + " scope is not valid");
173: }
174: }
175:
176: /*
177: * @see javax.servlet.jsp.JspContext#getOut()
178: */
179: public JspWriter getOut() {
180: return new JspWriterImpl(new PrintWriter(System.out));
181: }
182:
183: /*
184: * @see javax.servlet.jsp.PageContext#getPage()
185: */
186: public Object getPage() {
187: throw new UnsupportedOperationException();
188: }
189:
190: /*
191: * @see javax.servlet.jsp.PageContext#getServletConfig()
192: */
193: public ServletConfig getServletConfig() {
194: throw new UnsupportedOperationException();
195: }
196:
197: /*
198: * @see javax.servlet.jsp.PageContext#getServletContext()
199: */
200: public ServletContext getServletContext() {
201: throw new UnsupportedOperationException();
202: }
203:
204: public ELContext getELContext() {
205: throw new UnsupportedOperationException();
206: }
207:
208: /*
209: * @see javax.servlet.jsp.PageContext#getRequest()
210: */
211: public ServletRequest getRequest() {
212: return servletRequest;
213: }
214:
215: /*
216: * @see javax.servlet.jsp.PageContext#getResponse()
217: */
218: public ServletResponse getResponse() {
219: return servletResponse;
220: }
221:
222: /*
223: * @see javax.servlet.jsp.PageContext#getSession()
224: */
225: public HttpSession getSession() {
226: return httpSession;
227: }
228:
229: /*
230: * @see javax.servlet.jsp.PageContext#release()
231: */
232: public void release() {
233: throw new UnsupportedOperationException();
234: }
235:
236: /*
237: * @see javax.servlet.jsp.PageContext#getException()
238: */
239: public Exception getException() {
240: throw new UnsupportedOperationException();
241: }
242:
243: /*
244: * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Exception)
245: */
246: public void handlePageException(Exception arg0)
247: throws ServletException, IOException {
248: throw new UnsupportedOperationException();
249: }
250:
251: /*
252: * @see javax.servlet.jsp.PageContext#forward(java.lang.String)
253: */
254: public void forward(String arg0) throws ServletException,
255: IOException {
256: throw new UnsupportedOperationException();
257: }
258:
259: /*
260: * @see javax.servlet.jsp.PageContext#include(java.lang.String)
261: */
262: public void include(String arg0) throws ServletException,
263: IOException {
264: throw new UnsupportedOperationException();
265: }
266:
267: /*
268: * @see javax.servlet.jsp.PageContext#include(java.lang.String, boolean)
269: */
270: public void include(String arg0, boolean arg1)
271: throws ServletException, IOException {
272: throw new UnsupportedOperationException();
273: }
274:
275: /*
276: * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Throwable)
277: */
278: public void handlePageException(Throwable arg0)
279: throws ServletException, IOException {
280: throw new UnsupportedOperationException();
281: }
282:
283: /*
284: * @see javax.servlet.jsp.JspContext#getAttributesScope(java.lang.String)
285: */
286: public int getAttributesScope(String arg0) {
287: throw new UnsupportedOperationException();
288: }
289:
290: /*
291: * @see javax.servlet.jsp.JspContext#getAttributeNamesInScope(int)
292: */
293: public Enumeration getAttributeNamesInScope(int scope) {
294: switch (scope) {
295: case PAGE_SCOPE:
296: return new IteratorEnumeration(((Hashtable) attributes)
297: .keySet().iterator());
298: case REQUEST_SCOPE:
299: return new IteratorEnumeration(externalContext
300: .getRequestMap().keySet().iterator());
301: case SESSION_SCOPE:
302: return new IteratorEnumeration(externalContext
303: .getSessionMap().keySet().iterator());
304: case APPLICATION_SCOPE:
305: return new IteratorEnumeration(externalContext
306: .getApplicationMap().keySet().iterator());
307: default:
308: throw new IllegalArgumentException(scope
309: + " scope is not valid");
310: }
311:
312: }
313:
314: /*
315: * @see javax.servlet.jsp.JspContext#getExpressionEvaluator()
316: */
317: public ExpressionEvaluator getExpressionEvaluator() {
318: throw new UnsupportedOperationException();
319: }
320:
321: /*
322: * @see javax.servlet.jsp.JspContext#getVariableResolver()
323: */
324: public VariableResolver getVariableResolver() {
325: throw new UnsupportedOperationException();
326: }
327:
328: /*
329: * @see javax.servlet.jsp.JspContext#findAttribute(java.lang.String)
330: */
331: public Object findAttribute(String name) {
332: Object attribute;
333:
334: //check page scope
335: attribute = attributes.get(name);
336: if (null != attribute) {
337: return attribute;
338: }
339:
340: //check request scope
341: attribute = externalContext.getRequestMap().get(name);
342: if (null != attribute) {
343: return attribute;
344: }
345:
346: //check session scope
347: if (httpSession != null) {
348: attribute = httpSession.getAttribute(name);
349: }
350: if (null != attribute) {
351: return attribute;
352: }
353:
354: //return null or application scope value
355: return (externalContext.getApplicationMap().get(name));
356: }
357: }
|