KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > template > jsp > WebManPageContext


1 package de.webman.template.jsp;
2
3 import java.io.IOException JavaDoc;
4
5 import java.util.*;
6
7 import javax.servlet.*;
8 import javax.servlet.jsp.*;
9 import javax.servlet.http.*;
10 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
11 import org.apache.jasper.runtime.*;
12 /**
13  * <p>
14  * A PageContext instance provides access to all the namespaces associated with
15  * a JSP page, provides access to several page attributes, as well as a layer above the
16  * implementation details.
17  * <p>
18  * An instance of an implementation dependent subclass of this abstract base
19  * class is created by a JSP implementation class at the begining of it's
20  * <code> _jspService() </code> method via an implementation default
21  * <code> JspFactory </code>, as follows:
22  *</p>
23  *<p>
24  * <p>
25  * The <code> PageContext </code> class is an abstract class, designed to be
26  * extended to provide implementation dependent implementations thereof, by
27  * conformant JSP engine runtime environments. A PageContext instance is
28  * obtained by a JSP implementation class by calling the JspFactory.getPageContext() method, and is released by calling JspFactory.releasePageContext().
29  * </p>
30  * <p>
31  * The PageContext provides a number of facilities to the page/component author and
32  * page implementor, including:
33  * <td>
34  * <li>a single API to manage the various scoped namespaces
35  * <li>a number of convenience API's to access various public objects
36  * <li>a mechanism to obtain the JspWriter for output
37  * <li>a mechanism to manage session usage by the page
38  * <li>a mechanism to expose page directive attributes to the scripting environment
39  * <li>mechanisms to forward or include the current request to other active components in the application
40  * <li>a mechanism to handle errorpage exception processing
41  * </td>
42  * </p>
43  * @author $Author: alex $
44  * @version $Revision: 1.3 $
45  */

46 public class WebManPageContext extends PageContext
47 {
48     // per Servlet state
49

50     protected Servlet servlet;
51     protected ServletConfig config;
52     protected ServletContext context;
53
54     protected JspFactory factory;
55
56     protected boolean needsSession;
57
58     protected String JavaDoc errorPageURL;
59
60     protected boolean autoFlush;
61     protected int bufferSize;
62
63     // page scope attributes
64
protected transient Hashtable attributes = new Hashtable();
65
66     // per request state
67

68     protected transient ServletRequest request;
69     protected transient ServletResponse response;
70     protected transient Object JavaDoc page;
71
72     protected transient HttpSession session;
73
74     // initial output stream
75

76     protected transient JspWriter out;
77     
78     WebManPageContext(JspFactory _factory)
79     {
80         factory = _factory;
81     }
82
83     public void initialize(Servlet servlet, ServletRequest request,
84                            ServletResponse response, String JavaDoc errorPageURL,
85                            boolean needsSession, int bufferSize,
86                            boolean autoFlush)
87         throws IOException JavaDoc, IllegalStateException JavaDoc, IllegalArgumentException JavaDoc
88     {
89
90         // initialize state
91
this.servlet = servlet;
92         this.config = servlet.getServletConfig();
93         this.context = config.getServletContext();
94         this.needsSession = needsSession;
95         this.errorPageURL = errorPageURL;
96         this.bufferSize = bufferSize;
97         this.autoFlush = autoFlush;
98         this.request = request;
99         this.response = response;
100
101         // setup session (if required)
102

103         if (request instanceof HttpServletRequest && needsSession)
104             this.session = ((HttpServletRequest)request).getSession();
105         if (needsSession && session == null)
106             throw new IllegalStateException JavaDoc("Page needs a session and none is available");
107         // initialize the initial out ...
108

109         this.out = doCreateOut(bufferSize, autoFlush); // throws
110
if (this.out == null)
111             throw new IllegalStateException JavaDoc("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 JavaDoc getAttribute(String JavaDoc name)
145     {
146         return attributes.get(name);
147     }
148
149
150     public Object JavaDoc getAttribute(String JavaDoc name, int scope)
151     {
152         switch (scope) {
153             case PAGE_SCOPE:
154             return attributes.get(name);
155
156             case REQUEST_SCOPE:
157             return request.getAttribute(name);
158
159             case SESSION_SCOPE:
160             if (session == null)
161                 throw new IllegalArgumentException JavaDoc("can't access SESSION_SCOPE without an HttpSession");
162             else
163                 return session.getAttribute(name);
164
165             case APPLICATION_SCOPE:
166             return context.getAttribute(name);
167
168             default:
169             throw new IllegalArgumentException JavaDoc("unidentified scope");
170         }
171     }
172
173
174     public void setAttribute(String JavaDoc name, Object JavaDoc attribute)
175     {
176         attributes.put(name, attribute);
177     }
178
179
180     public void setAttribute(String JavaDoc name, Object JavaDoc o, int scope)
181     {
182         switch (scope) {
183             case PAGE_SCOPE:
184             attributes.put(name, o);
185             break;
186
187             case REQUEST_SCOPE:
188             request.setAttribute(name, o);
189             break;
190
191             case SESSION_SCOPE:
192             if (session == null)
193                 throw new IllegalArgumentException JavaDoc("can't access SESSION_SCOPE without an HttpSession");
194             else
195                 session.setAttribute(name, o);
196             break;
197
198             case APPLICATION_SCOPE:
199             context.setAttribute(name, o);
200             break;
201
202             default:
203         }
204     }
205     
206     public void removeAttribute(String JavaDoc name)
207     {
208         attributes.remove(name);
209     }
210
211     public void removeAttribute(String JavaDoc name, int scope)
212     {
213         switch (scope)
214         {
215             case PAGE_SCOPE:
216             attributes.remove(name);
217             break;
218
219             case REQUEST_SCOPE:
220             throw new IllegalArgumentException JavaDoc("cant remove Attributes from request scope");
221
222             case SESSION_SCOPE:
223             if (session == null)
224                 throw new IllegalArgumentException JavaDoc("can't access SESSION_SCOPE without an HttpSession");
225             else
226                         session.removeAttribute(name);
227                     // was:
228
// session.removeValue(name);
229
// REVISIT Verify this is correct - akv
230
break;
231
232             case APPLICATION_SCOPE:
233             context.removeAttribute(name);
234             break;
235
236             default:
237         }
238     }
239
240     public int getAttributesScope(String JavaDoc name)
241     {
242         if (attributes.get(name) != null) return PAGE_SCOPE;
243
244         if (request.getAttribute(name) != null)
245             return REQUEST_SCOPE;
246
247         if (session != null) {
248             if (session.getAttribute(name) != null)
249                 return SESSION_SCOPE;
250         }
251
252         if (context.getAttribute(name) != null) return APPLICATION_SCOPE;
253
254         return 0;
255     }
256
257     public Object JavaDoc findAttribute(String JavaDoc name)
258     {
259         Object JavaDoc o = attributes.get(name);
260         if (o != null)
261             return o;
262
263         o = request.getAttribute(name);
264         if (o != null)
265             return o;
266
267         if (session != null) {
268             o = session.getAttribute(name);
269             if (o != null)
270                 return o;
271         }
272         return context.getAttribute(name);
273     }
274
275     public Enumeration getAttributeNamesInScope(int scope)
276     {
277         switch (scope) {
278             case PAGE_SCOPE:
279             return attributes.keys();
280
281             case REQUEST_SCOPE:
282             return request.getAttributeNames();
283
284             case SESSION_SCOPE:
285             if (session != null) {
286                 return session.getAttributeNames();
287             } else
288                 throw new IllegalArgumentException JavaDoc("can't access SESSION_SCOPE without an HttpSession");
289
290             case APPLICATION_SCOPE:
291             return context.getAttributeNames();
292
293             default: return new Enumeration() { // empty enumeration
294
public boolean hasMoreElements() { return false; }
295
296             public Object JavaDoc nextElement() { throw new NoSuchElementException(); }
297             };
298         }
299     }
300
301
302     public JspWriter getOut() { return out; }
303     public HttpSession getSession() { return session; }
304     public Servlet getServlet() { return servlet; }
305     public ServletConfig getServletConfig() { return config; }
306     public ServletContext getServletContext() { return config.getServletContext(); }
307     public ServletRequest getRequest() { return request; }
308     public ServletResponse getResponse() { return response; }
309     public Exception JavaDoc getException() { return (Exception JavaDoc)request.getAttribute(EXCEPTION); }
310     public Object JavaDoc getPage() { return servlet; }
311
312
313     private final String JavaDoc getAbsolutePathRelativeToContext(String JavaDoc relativeUrlPath)
314     {
315         String JavaDoc path = relativeUrlPath;
316
317         if (!path.startsWith("/")) {
318         String JavaDoc uri = (String JavaDoc) request.getAttribute("javax.servlet.include.servlet_path");
319         if (uri == null)
320         uri = ((HttpServletRequest) request).getServletPath();
321             String JavaDoc baseURI = uri.substring(0, uri.lastIndexOf('/'));
322             path = baseURI+'/'+path;
323         }
324         return path;
325     }
326
327     public void include(String JavaDoc relativeUrlPath)
328         throws ServletException, IOException JavaDoc
329     {
330         String JavaDoc path = getAbsolutePathRelativeToContext(relativeUrlPath);
331         context.getRequestDispatcher(path).include(request, response);
332     }
333
334     public void forward(String JavaDoc relativeUrlPath)
335         throws ServletException, IOException JavaDoc
336     {
337         String JavaDoc path = getAbsolutePathRelativeToContext(relativeUrlPath);
338         context.getRequestDispatcher(path).forward(request, response);
339     }
340
341     Stack writerStack = new Stack();
342
343     public BodyContent JavaDoc pushBody()
344     {
345         JspWriter previous = out;
346         writerStack.push(out);
347         out = new BodyContentImpl(previous);
348         return (BodyContent JavaDoc) out;
349     }
350
351     public JspWriter popBody()
352     {
353         out = (JspWriter) writerStack.pop();
354         return out;
355     }
356
357     public void handlePageException(Exception JavaDoc e)
358     throws IOException JavaDoc, ServletException
359     {
360         handlePageException((Throwable JavaDoc)e);
361     }
362     
363     public void handlePageException(Throwable JavaDoc e)
364     throws IOException JavaDoc, ServletException
365     {
366
367         // set the request attribute with the exception.
368
request.setAttribute("javax.servlet.jsp.jspException", e);
369
370         if (errorPageURL != null && !errorPageURL.equals("")) {
371             forward(errorPageURL);
372         } // Otherwise throw the exception wrapped inside a ServletException.
373
else {
374             // Set the exception as the root cause in the ServletException
375
// to get a stack trace for the real problem
376
throw new ServletException(e);
377         }
378
379     }
380
381     protected JspWriter doCreateOut(int bufferSize, boolean autoFlush)
382         throws IOException JavaDoc, IllegalArgumentException JavaDoc
383     {
384         return new JspWriterImpl(response, bufferSize, autoFlush);
385     }
386
387 }
388
Popular Tags