001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.sun.portal.providers.jsp.jasper3.jasper.runtime;
057:
058: import java.io.IOException;
059: import java.io.InputStreamReader;
060:
061: import java.util.Enumeration;
062: import java.util.Hashtable;
063: import java.util.NoSuchElementException;
064: import java.util.Stack;
065: import java.util.logging.Level;
066: import java.util.logging.LogRecord;
067: import java.util.logging.Logger;
068:
069: import javax.servlet.Servlet;
070: import javax.servlet.ServletConfig;
071: import javax.servlet.ServletContext;
072: import javax.servlet.ServletRequest;
073: import javax.servlet.ServletResponse;
074: import javax.servlet.ServletException;
075:
076: import javax.servlet.http.HttpSession;
077: import javax.servlet.http.HttpServletRequest;
078: import javax.servlet.http.HttpServletResponse;
079:
080: import javax.servlet.jsp.PageContext;
081: import javax.servlet.jsp.JspFactory;
082: import javax.servlet.jsp.JspWriter;
083: import javax.servlet.jsp.tagext.BodyContent;
084:
085: import com.sun.portal.providers.jsp.jasper3.jasper.Constants;
086:
087: /**
088: * Implementation of the PageContext class from the JSP spec.
089: *
090: * @author Anil K. Vijendran
091: * @author Larry Cable
092: * @author Hans Bergsten
093: */
094: public class PageContextImpl extends PageContext {
095: PageContextImpl(JspFactory factory) {
096: this .factory = factory;
097: }
098:
099: /*
100: static class InitAction implements java.security.PrivilegedAction {
101: Servlet servlet;
102: ServletRequest request;
103: ServletResponse response;
104: String errorPageURL;
105: boolean needsSession;
106: int bufferSize;
107: boolean autoFlush;
108: PageContextImpl pci;
109:
110: InitAction(PageContextImpl pci, Servlet s, ServletRequest req,
111: ServletResponse res, String err,
112: boolean n, int b,
113: boolean a) {
114: this.pci=pci;
115: servlet=s;
116: request=req;
117: response=res;
118: errorPageURL=err;
119: needsSession=n;;
120: bufferSize=b;
121: autoFlush=a;
122: }
123:
124: public Object run() {
125: try {
126: pci._initialize(servlet, request, response, errorPageURL, needsSession, bufferSize, autoFlush);
127: } catch( Throwable t ) {
128: t.printStackTrace();
129: }
130: return null;
131: }
132: }
133: */
134: public void initialize(Servlet servlet, ServletRequest request,
135: ServletResponse response, String errorPageURL,
136: boolean needsSession, int bufferSize, boolean autoFlush)
137: throws IOException, IllegalStateException,
138: IllegalArgumentException {
139: // InitAction ia=new InitAction( this, servlet, request, response,
140: // errorPageURL, needsSession, bufferSize,
141: // autoFlush);
142: // java.security.AccessController.doPrivileged( ia );
143: _initialize(servlet, request, response, errorPageURL,
144: needsSession, bufferSize, autoFlush);
145: }
146:
147: void _initialize(Servlet servlet, ServletRequest request,
148: ServletResponse response, String errorPageURL,
149: boolean needsSession, int bufferSize, boolean autoFlush)
150: throws IOException, IllegalStateException,
151: IllegalArgumentException {
152:
153: // initialize state
154:
155: this .servlet = servlet;
156: this .config = servlet.getServletConfig();
157: this .context = config.getServletContext();
158: this .needsSession = needsSession;
159: this .errorPageURL = errorPageURL;
160: this .bufferSize = bufferSize;
161: this .autoFlush = autoFlush;
162: this .request = request;
163: this .response = response;
164:
165: // setup session (if required)
166: if (request instanceof HttpServletRequest && needsSession)
167: this .session = ((HttpServletRequest) request).getSession();
168:
169: if (needsSession && session == null)
170: throw new IllegalStateException(
171: "Page needs a session and none is available");
172:
173: // initialize the initial out ...
174: // System.out.println("Initialize PageContextImpl " + out );
175: if (out == null) {
176: out = _createOut(bufferSize, autoFlush); // throws
177: } else
178: ((JspWriterImpl) out).init(response, bufferSize, autoFlush);
179:
180: if (this .out == null)
181: throw new IllegalStateException(
182: "failed initialize JspWriter");
183:
184: // register names/values as per spec
185:
186: setAttribute(OUT, this .out);
187: setAttribute(REQUEST, request);
188: setAttribute(RESPONSE, response);
189:
190: if (session != null)
191: setAttribute(SESSION, session);
192:
193: setAttribute(PAGE, servlet);
194: setAttribute(CONFIG, config);
195: setAttribute(PAGECONTEXT, this );
196: setAttribute(APPLICATION, context);
197: }
198:
199: public void release() {
200: servlet = null;
201: config = null;
202: context = null;
203: needsSession = false;
204: errorPageURL = null;
205: bufferSize = JspWriter.DEFAULT_BUFFER;
206: autoFlush = true;
207: request = null;
208: response = null;
209: // Reuse // XXX problems - need to fix them first!!
210: // out = null; // out is closed elsewhere
211: if ((out != null) && (out instanceof JspWriterImpl)) {
212: ((JspWriterImpl) out).recycle();
213: }
214: session = null;
215:
216: attributes.clear();
217: }
218:
219: public Object getAttribute(String name) {
220: return attributes.get(name);
221: }
222:
223: public Object getAttribute(String name, int scope) {
224: switch (scope) {
225: case PAGE_SCOPE:
226: return attributes.get(name);
227:
228: case REQUEST_SCOPE:
229: return request.getAttribute(name);
230:
231: case SESSION_SCOPE:
232: if (session == null)
233: throw new IllegalArgumentException(
234: "can't access SESSION_SCOPE without an HttpSession");
235: else
236: return session.getAttribute(name);
237:
238: case APPLICATION_SCOPE:
239: return context.getAttribute(name);
240:
241: default:
242: throw new IllegalArgumentException("unidentified scope");
243: }
244: }
245:
246: public void setAttribute(String name, Object attribute) {
247: attributes.put(name, attribute);
248: }
249:
250: public void setAttribute(String name, Object o, int scope) {
251: switch (scope) {
252: case PAGE_SCOPE:
253: attributes.put(name, o);
254: break;
255:
256: case REQUEST_SCOPE:
257: request.setAttribute(name, o);
258: break;
259:
260: case SESSION_SCOPE:
261: if (session == null)
262: throw new IllegalArgumentException(
263: "can't access SESSION_SCOPE without an HttpSession");
264: else
265: session.setAttribute(name, o);
266: break;
267:
268: case APPLICATION_SCOPE:
269: context.setAttribute(name, o);
270: break;
271:
272: default:
273: }
274: }
275:
276: public void removeAttribute(String name, int scope) {
277: switch (scope) {
278: case PAGE_SCOPE:
279: attributes.remove(name);
280: break;
281:
282: case REQUEST_SCOPE:
283: request.removeAttribute(name);
284:
285: case SESSION_SCOPE:
286: if (session == null)
287: throw new IllegalArgumentException(
288: "can't access SESSION_SCOPE without an HttpSession");
289: else
290: session.removeAttribute(name);
291: // was:
292: // session.removeValue(name);
293: // REVISIT Verify this is correct - akv
294: break;
295:
296: case APPLICATION_SCOPE:
297: context.removeAttribute(name);
298: break;
299:
300: default:
301: }
302: }
303:
304: public int getAttributesScope(String name) {
305: if (attributes.get(name) != null)
306: return PAGE_SCOPE;
307:
308: if (request.getAttribute(name) != null)
309: return REQUEST_SCOPE;
310:
311: if (session != null) {
312: if (session.getAttribute(name) != null)
313: return SESSION_SCOPE;
314: }
315:
316: if (context.getAttribute(name) != null)
317: return APPLICATION_SCOPE;
318:
319: return 0;
320: }
321:
322: public Object findAttribute(String name) {
323: Object o = attributes.get(name);
324: if (o != null)
325: return o;
326:
327: o = request.getAttribute(name);
328: if (o != null)
329: return o;
330:
331: if (session != null) {
332: o = session.getAttribute(name);
333: if (o != null)
334: return o;
335: }
336:
337: return context.getAttribute(name);
338: }
339:
340: public Enumeration getAttributeNamesInScope(int scope) {
341: switch (scope) {
342: case PAGE_SCOPE:
343: return attributes.keys();
344:
345: case REQUEST_SCOPE:
346: return request.getAttributeNames();
347:
348: case SESSION_SCOPE:
349: if (session != null) {
350: return session.getAttributeNames();
351: } else
352: throw new IllegalArgumentException(
353: "can't access SESSION_SCOPE without an HttpSession");
354:
355: case APPLICATION_SCOPE:
356: return context.getAttributeNames();
357:
358: default:
359: return new Enumeration() { // empty enumeration
360: public boolean hasMoreElements() {
361: return false;
362: }
363:
364: public Object nextElement() {
365: throw new NoSuchElementException();
366: }
367: };
368: }
369: }
370:
371: public void removeAttribute(String name) {
372: try {
373: removeAttribute(name, PAGE_SCOPE);
374: removeAttribute(name, REQUEST_SCOPE);
375: removeAttribute(name, SESSION_SCOPE);
376: removeAttribute(name, APPLICATION_SCOPE);
377: } catch (Exception ex) {
378: // we remove as much as we can, and
379: // simply ignore possible exceptions
380: }
381: }
382:
383: public JspWriter getOut() {
384: return out;
385: }
386:
387: public HttpSession getSession() {
388: return session;
389: }
390:
391: public Servlet getServlet() {
392: return servlet;
393: }
394:
395: public ServletConfig getServletConfig() {
396: return config;
397: }
398:
399: public ServletContext getServletContext() {
400: return config.getServletContext();
401: }
402:
403: public ServletRequest getRequest() {
404: return request;
405: }
406:
407: public ServletResponse getResponse() {
408: return response;
409: }
410:
411: public Exception getException() {
412: return (Exception) request.getAttribute(EXCEPTION);
413: }
414:
415: public Object getPage() {
416: return servlet;
417: }
418:
419: private final String getAbsolutePathRelativeToContext(
420: String relativeUrlPath) {
421: String path = relativeUrlPath;
422:
423: if (!path.startsWith("/")) {
424: String uri = (String) request
425: .getAttribute("javax.servlet.include.servlet_path");
426: if (uri == null)
427: uri = ((HttpServletRequest) request).getServletPath();
428: String baseURI = uri.substring(0, uri.lastIndexOf('/'));
429: path = baseURI + '/' + path;
430: }
431:
432: return path;
433: }
434:
435: public void include(String relativeUrlPath)
436: throws ServletException, IOException {
437: String path = getAbsolutePathRelativeToContext(relativeUrlPath);
438: out.flush();
439: context.getRequestDispatcher(path).include(request, response);
440: }
441:
442: public void forward(String relativeUrlPath)
443: throws ServletException, IOException {
444: String path = getAbsolutePathRelativeToContext(relativeUrlPath);
445: context.getRequestDispatcher(path).forward(request, response);
446: }
447:
448: Stack writerStack = new Stack();
449:
450: public BodyContent pushBody() {
451: JspWriter previous = out;
452: writerStack.push(out);
453: out = new BodyContentImpl(previous);
454: return (BodyContent) out;
455: }
456:
457: public JspWriter popBody() {
458: out = (JspWriter) writerStack.pop();
459: return out;
460: }
461:
462: public void handlePageException(Exception e) throws IOException,
463: ServletException {
464:
465: Logger debugLogger = com.sun.portal.log.common.PortalLogger
466: .getLogger(PageContextImpl.class);
467:
468: if (debugLogger.isLoggable(Level.SEVERE)) {
469: LogRecord logRecord = new LogRecord(Level.SEVERE,
470: "PSDT_CSPP0003");
471: logRecord.setLoggerName(debugLogger.getName());
472: logRecord.setThrown(e);
473: debugLogger.log(logRecord);
474: }
475:
476: // set the request attribute with the exception.
477: request.setAttribute("javax.servlet.jsp.jspException", e);
478:
479: if (errorPageURL != null && !errorPageURL.equals("")) {
480: try {
481: forward(errorPageURL);
482: } catch (IllegalStateException ise) {
483: include(errorPageURL);
484: }
485: } // Otherwise throw the exception wrapped inside a ServletException.
486: else {
487: // Set the exception as the root cause in the ServletException
488: // to get a stack trace for the real problem
489: if (e instanceof IOException)
490: throw (IOException) e;
491: if (e instanceof ServletException)
492: throw (ServletException) e;
493: // e.printStackTrace();
494: throw new ServletException(e);
495: }
496:
497: }
498:
499: public void handlePageException(Throwable t) throws IOException,
500: ServletException {
501:
502: Logger debugLogger = com.sun.portal.log.common.PortalLogger
503: .getLogger(PageContextImpl.class);
504:
505: if (debugLogger.isLoggable(Level.SEVERE)) {
506: LogRecord logRecord = new LogRecord(Level.SEVERE,
507: "PSDT_CSPP0003");
508: logRecord.setLoggerName(debugLogger.getName());
509: logRecord.setThrown(t);
510: debugLogger.log(logRecord);
511: }
512:
513: // set the request attribute with the exception.
514: request.setAttribute("javax.servlet.jsp.jspException", t);
515:
516: if (errorPageURL != null && !errorPageURL.equals("")) {
517: try {
518: forward(errorPageURL);
519: } catch (IllegalStateException ise) {
520: include(errorPageURL);
521: }
522: } // Otherwise throw the exception wrapped inside a ServletException.
523: else {
524: // Set the exception as the root cause in the ServletException
525: // to get a stack trace for the real problem
526: if (t instanceof IOException)
527: throw (IOException) t;
528: if (t instanceof ServletException)
529: throw (ServletException) t;
530: // e.printStackTrace();
531: throw new ServletException(t);
532: }
533:
534: }
535:
536: protected JspWriter _createOut(int bufferSize, boolean autoFlush)
537: throws IOException, IllegalArgumentException {
538: return new JspWriterImpl(response, bufferSize, autoFlush);
539: }
540:
541: /*
542: * fields
543: */
544:
545: // per Servlet state
546: protected Servlet servlet;
547: protected ServletConfig config;
548: protected ServletContext context;
549:
550: protected JspFactory factory;
551:
552: protected boolean needsSession;
553:
554: protected String errorPageURL;
555:
556: protected boolean autoFlush;
557: protected int bufferSize;
558:
559: // page scope attributes
560:
561: protected transient Hashtable attributes = new Hashtable(16);
562:
563: // per request state
564:
565: protected transient ServletRequest request;
566: protected transient ServletResponse response;
567: protected transient Object page;
568:
569: protected transient HttpSession session;
570:
571: // initial output stream
572:
573: protected transient JspWriter out;
574: }
|