001: package de.webman.template.jsp;
002:
003: import java.io.IOException;
004:
005: import java.util.*;
006:
007: import javax.servlet.*;
008: import javax.servlet.jsp.*;
009: import javax.servlet.http.*;
010: import javax.servlet.jsp.tagext.BodyContent;
011: import org.apache.jasper.runtime.*;
012:
013: /**
014: * <p>
015: * A PageContext instance provides access to all the namespaces associated with
016: * a JSP page, provides access to several page attributes, as well as a layer above the
017: * implementation details.
018: * <p>
019: * An instance of an implementation dependent subclass of this abstract base
020: * class is created by a JSP implementation class at the begining of it's
021: * <code> _jspService() </code> method via an implementation default
022: * <code> JspFactory </code>, as follows:
023: *</p>
024: *<p>
025: * <p>
026: * The <code> PageContext </code> class is an abstract class, designed to be
027: * extended to provide implementation dependent implementations thereof, by
028: * conformant JSP engine runtime environments. A PageContext instance is
029: * obtained by a JSP implementation class by calling the JspFactory.getPageContext() method, and is released by calling JspFactory.releasePageContext().
030: * </p>
031: * <p>
032: * The PageContext provides a number of facilities to the page/component author and
033: * page implementor, including:
034: * <td>
035: * <li>a single API to manage the various scoped namespaces
036: * <li>a number of convenience API's to access various public objects
037: * <li>a mechanism to obtain the JspWriter for output
038: * <li>a mechanism to manage session usage by the page
039: * <li>a mechanism to expose page directive attributes to the scripting environment
040: * <li>mechanisms to forward or include the current request to other active components in the application
041: * <li>a mechanism to handle errorpage exception processing
042: * </td>
043: * </p>
044: * @author $Author: alex $
045: * @version $Revision: 1.3 $
046: */
047: public class WebManPageContext extends PageContext {
048: // per Servlet state
049:
050: protected Servlet servlet;
051: protected ServletConfig config;
052: protected ServletContext context;
053:
054: protected JspFactory factory;
055:
056: protected boolean needsSession;
057:
058: protected String errorPageURL;
059:
060: protected boolean autoFlush;
061: protected int bufferSize;
062:
063: // page scope attributes
064: protected transient Hashtable attributes = new Hashtable();
065:
066: // per request state
067:
068: protected transient ServletRequest request;
069: protected transient ServletResponse response;
070: protected transient Object page;
071:
072: protected transient HttpSession session;
073:
074: // initial output stream
075:
076: protected transient JspWriter out;
077:
078: WebManPageContext(JspFactory _factory) {
079: factory = _factory;
080: }
081:
082: public void initialize(Servlet servlet, ServletRequest request,
083: ServletResponse response, String errorPageURL,
084: boolean needsSession, int bufferSize, boolean autoFlush)
085: throws IOException, IllegalStateException,
086: IllegalArgumentException {
087:
088: // initialize state
089: this .servlet = servlet;
090: this .config = servlet.getServletConfig();
091: this .context = config.getServletContext();
092: this .needsSession = needsSession;
093: this .errorPageURL = errorPageURL;
094: this .bufferSize = bufferSize;
095: this .autoFlush = autoFlush;
096: this .request = request;
097: this .response = response;
098:
099: // setup session (if required)
100:
101: if (request instanceof HttpServletRequest && needsSession)
102: this .session = ((HttpServletRequest) request).getSession();
103: if (needsSession && session == null)
104: throw new IllegalStateException(
105: "Page needs a session and none is available");
106: // initialize the initial out ...
107:
108: this .out = doCreateOut(bufferSize, autoFlush); // throws
109: if (this .out == null)
110: throw new IllegalStateException(
111: "failed initialize JspWriter");
112:
113: // register names/values as per spec
114:
115: setAttribute(OUT, this .out);
116: setAttribute(REQUEST, request);
117: setAttribute(RESPONSE, response);
118:
119: if (session != null)
120: setAttribute(SESSION, session);
121:
122: setAttribute(PAGE, servlet);
123: setAttribute(CONFIG, config);
124: setAttribute(PAGECONTEXT, this );
125: // setAttribute(APPLICATION, context);
126: }
127:
128: public void release() {
129: servlet = null;
130: config = null;
131: context = null;
132: needsSession = false;
133: errorPageURL = null;
134: bufferSize = JspWriter.DEFAULT_BUFFER;
135: autoFlush = true;
136: request = null;
137: response = null;
138: out = null; // out is closed elsewhere
139: session = null;
140:
141: attributes.clear();
142: }
143:
144: public Object getAttribute(String name) {
145: return attributes.get(name);
146: }
147:
148: public Object getAttribute(String name, int scope) {
149: switch (scope) {
150: case PAGE_SCOPE:
151: return attributes.get(name);
152:
153: case REQUEST_SCOPE:
154: return request.getAttribute(name);
155:
156: case SESSION_SCOPE:
157: if (session == null)
158: throw new IllegalArgumentException(
159: "can't access SESSION_SCOPE without an HttpSession");
160: else
161: return session.getAttribute(name);
162:
163: case APPLICATION_SCOPE:
164: return context.getAttribute(name);
165:
166: default:
167: throw new IllegalArgumentException("unidentified scope");
168: }
169: }
170:
171: public void setAttribute(String name, Object attribute) {
172: attributes.put(name, attribute);
173: }
174:
175: public void setAttribute(String name, Object o, int scope) {
176: switch (scope) {
177: case PAGE_SCOPE:
178: attributes.put(name, o);
179: break;
180:
181: case REQUEST_SCOPE:
182: request.setAttribute(name, o);
183: break;
184:
185: case SESSION_SCOPE:
186: if (session == null)
187: throw new IllegalArgumentException(
188: "can't access SESSION_SCOPE without an HttpSession");
189: else
190: session.setAttribute(name, o);
191: break;
192:
193: case APPLICATION_SCOPE:
194: context.setAttribute(name, o);
195: break;
196:
197: default:
198: }
199: }
200:
201: public void removeAttribute(String name) {
202: attributes.remove(name);
203: }
204:
205: public void removeAttribute(String name, int scope) {
206: switch (scope) {
207: case PAGE_SCOPE:
208: attributes.remove(name);
209: break;
210:
211: case REQUEST_SCOPE:
212: throw new IllegalArgumentException(
213: "cant remove Attributes from request scope");
214:
215: case SESSION_SCOPE:
216: if (session == null)
217: throw new IllegalArgumentException(
218: "can't access SESSION_SCOPE without an HttpSession");
219: else
220: session.removeAttribute(name);
221: // was:
222: // session.removeValue(name);
223: // REVISIT Verify this is correct - akv
224: break;
225:
226: case APPLICATION_SCOPE:
227: context.removeAttribute(name);
228: break;
229:
230: default:
231: }
232: }
233:
234: public int getAttributesScope(String name) {
235: if (attributes.get(name) != null)
236: return PAGE_SCOPE;
237:
238: if (request.getAttribute(name) != null)
239: return REQUEST_SCOPE;
240:
241: if (session != null) {
242: if (session.getAttribute(name) != null)
243: return SESSION_SCOPE;
244: }
245:
246: if (context.getAttribute(name) != null)
247: return APPLICATION_SCOPE;
248:
249: return 0;
250: }
251:
252: public Object findAttribute(String name) {
253: Object o = attributes.get(name);
254: if (o != null)
255: return o;
256:
257: o = request.getAttribute(name);
258: if (o != null)
259: return o;
260:
261: if (session != null) {
262: o = session.getAttribute(name);
263: if (o != null)
264: return o;
265: }
266: return context.getAttribute(name);
267: }
268:
269: public Enumeration getAttributeNamesInScope(int scope) {
270: switch (scope) {
271: case PAGE_SCOPE:
272: return attributes.keys();
273:
274: case REQUEST_SCOPE:
275: return request.getAttributeNames();
276:
277: case SESSION_SCOPE:
278: if (session != null) {
279: return session.getAttributeNames();
280: } else
281: throw new IllegalArgumentException(
282: "can't access SESSION_SCOPE without an HttpSession");
283:
284: case APPLICATION_SCOPE:
285: return context.getAttributeNames();
286:
287: default:
288: return new Enumeration() { // empty enumeration
289: public boolean hasMoreElements() {
290: return false;
291: }
292:
293: public Object nextElement() {
294: throw new NoSuchElementException();
295: }
296: };
297: }
298: }
299:
300: public JspWriter getOut() {
301: return out;
302: }
303:
304: public HttpSession getSession() {
305: return session;
306: }
307:
308: public Servlet getServlet() {
309: return servlet;
310: }
311:
312: public ServletConfig getServletConfig() {
313: return config;
314: }
315:
316: public ServletContext getServletContext() {
317: return config.getServletContext();
318: }
319:
320: public ServletRequest getRequest() {
321: return request;
322: }
323:
324: public ServletResponse getResponse() {
325: return response;
326: }
327:
328: public Exception getException() {
329: return (Exception) request.getAttribute(EXCEPTION);
330: }
331:
332: public Object getPage() {
333: return servlet;
334: }
335:
336: private final String getAbsolutePathRelativeToContext(
337: String relativeUrlPath) {
338: String path = relativeUrlPath;
339:
340: if (!path.startsWith("/")) {
341: String uri = (String) request
342: .getAttribute("javax.servlet.include.servlet_path");
343: if (uri == null)
344: uri = ((HttpServletRequest) request).getServletPath();
345: String baseURI = uri.substring(0, uri.lastIndexOf('/'));
346: path = baseURI + '/' + path;
347: }
348: return path;
349: }
350:
351: public void include(String relativeUrlPath)
352: throws ServletException, IOException {
353: String path = getAbsolutePathRelativeToContext(relativeUrlPath);
354: context.getRequestDispatcher(path).include(request, response);
355: }
356:
357: public void forward(String relativeUrlPath)
358: throws ServletException, IOException {
359: String path = getAbsolutePathRelativeToContext(relativeUrlPath);
360: context.getRequestDispatcher(path).forward(request, response);
361: }
362:
363: Stack writerStack = new Stack();
364:
365: public BodyContent pushBody() {
366: JspWriter previous = out;
367: writerStack.push(out);
368: out = new BodyContentImpl(previous);
369: return (BodyContent) out;
370: }
371:
372: public JspWriter popBody() {
373: out = (JspWriter) writerStack.pop();
374: return out;
375: }
376:
377: public void handlePageException(Exception e) throws IOException,
378: ServletException {
379: handlePageException((Throwable) e);
380: }
381:
382: public void handlePageException(Throwable e) throws IOException,
383: ServletException {
384:
385: // set the request attribute with the exception.
386: request.setAttribute("javax.servlet.jsp.jspException", e);
387:
388: if (errorPageURL != null && !errorPageURL.equals("")) {
389: forward(errorPageURL);
390: } // Otherwise throw the exception wrapped inside a ServletException.
391: else {
392: // Set the exception as the root cause in the ServletException
393: // to get a stack trace for the real problem
394: throw new ServletException(e);
395: }
396:
397: }
398:
399: protected JspWriter doCreateOut(int bufferSize, boolean autoFlush)
400: throws IOException, IllegalArgumentException {
401: return new JspWriterImpl(response, bufferSize, autoFlush);
402: }
403:
404: }
|