001: package com.quadcap.http.servlets.jsp;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042: import java.io.PrintWriter;
043:
044: import java.util.Enumeration;
045: import java.util.Hashtable;
046: import java.util.Vector;
047:
048: import javax.servlet.RequestDispatcher;
049: import javax.servlet.Servlet;
050: import javax.servlet.ServletConfig;
051: import javax.servlet.ServletContext;
052: import javax.servlet.ServletException;
053: import javax.servlet.ServletRequest;
054: import javax.servlet.ServletResponse;
055:
056: import javax.servlet.http.HttpServletRequest;
057: import javax.servlet.http.HttpServletResponse;
058: import javax.servlet.http.HttpSession;
059:
060: import com.quadcap.http.server22.HttpResponse;
061:
062: import com.quadcap.util.Debug;
063:
064: /**
065: * The PageContext implementation.
066: *
067: * @author Stan Bailes
068: */
069: public class PageContext extends javax.servlet.jsp.PageContext {
070: Hashtable pageAttributes = null;
071: Servlet servlet;
072: HttpServletRequest request;
073: HttpServletResponse response;
074: HttpSession session;
075: String errorPageURL;
076: boolean needsSession;
077: int bufferSize;
078: boolean autoFlush;
079: javax.servlet.jsp.JspWriter out;
080:
081: public void initialize(Servlet servlet, ServletRequest request,
082: ServletResponse response, String errorPageURL,
083: boolean needsSession, int bufferSize, boolean autoFlush)
084: throws IOException, IllegalStateException,
085: IllegalArgumentException {
086: this .servlet = servlet;
087: this .request = (HttpServletRequest) request;
088: this .response = (HttpServletResponse) response;
089: this .errorPageURL = errorPageURL;
090: this .needsSession = needsSession;
091: this .bufferSize = bufferSize;
092: this .autoFlush = autoFlush;
093: this .session = null;
094:
095: if (response instanceof HttpResponse) {
096: HttpResponse qresp = (HttpResponse) response;
097: this .out = qresp.getJspWriter(bufferSize, autoFlush);
098: } else {
099: this .out = new JspWriter(response.getWriter(), bufferSize,
100: autoFlush);
101: }
102: }
103:
104: public void release() {
105: this .servlet = null;
106: this .request = null;
107: this .response = null;
108: this .errorPageURL = null;
109: this .session = null;
110: }
111:
112: public void setAttribute(String name, Object val) {
113: setAttribute(name, val, PAGE_SCOPE);
114: }
115:
116: public void setAttribute(String name, Object val, int scope) {
117: switch (scope) {
118: case PAGE_SCOPE:
119: if (pageAttributes == null)
120: pageAttributes = new Hashtable();
121: pageAttributes.put(name, val);
122: break;
123: case REQUEST_SCOPE:
124: request.setAttribute(name, val);
125: break;
126: case SESSION_SCOPE:
127: if (session != null)
128: session.putValue(name, val);
129: break;
130: case APPLICATION_SCOPE:
131: getServletContext().setAttribute(name, val);
132: }
133: }
134:
135: public Object getAttribute(String name) {
136: return getAttribute(name, PAGE_SCOPE);
137: }
138:
139: public Object getAttribute(String name, int scope) {
140: switch (scope) {
141: case PAGE_SCOPE:
142: if (pageAttributes != null)
143: return pageAttributes.get(name);
144: break;
145: case REQUEST_SCOPE:
146: return request.getAttribute(name);
147: case SESSION_SCOPE:
148: if (session != null)
149: return session.getValue(name);
150: break;
151: case APPLICATION_SCOPE:
152: return getServletContext().getAttribute(name);
153: }
154: return null;
155: }
156:
157: public Object findAttribute(String name) {
158: Object obj = null;
159: for (int i = 0; obj == null && i < 4; i++) {
160: obj = getAttribute(name, i);
161: }
162: return obj;
163: }
164:
165: public void removeAttribute(String name) {
166: removeAttribute(name, PAGE_SCOPE);
167: }
168:
169: public void removeAttribute(String name, int scope) {
170: switch (scope) {
171: case PAGE_SCOPE:
172: if (pageAttributes != null)
173: pageAttributes.remove(name);
174: break;
175: case REQUEST_SCOPE:
176: request.setAttribute(name, ""); // XXX no remove!
177: break;
178: case SESSION_SCOPE:
179: if (session != null)
180: session.removeValue(name);
181: break;
182: case APPLICATION_SCOPE:
183: getServletContext().removeAttribute(name);
184: }
185: }
186:
187: public int getAttributesScope(String name) {
188: int scope = 0;
189: for (int i = 0; scope == 0 && i < 4; i++) {
190: if (getAttribute(name, i) != null) {
191: scope = i + 1;
192: }
193: }
194: return scope;
195: }
196:
197: public Enumeration getAttributeNamesInScope(int scope) {
198: switch (scope) {
199: case PAGE_SCOPE:
200: if (pageAttributes != null)
201: return pageAttributes.keys();
202: break;
203: case REQUEST_SCOPE:
204: return request.getAttributeNames();
205: case SESSION_SCOPE:
206: Vector v = new Vector();
207: if (session != null) {
208: String[] s = session.getValueNames();
209: for (int i = 0; i < s.length; i++)
210: v.addElement(s[i]);
211: }
212: return v.elements();
213: case APPLICATION_SCOPE:
214: return getServletContext().getAttributeNames();
215: }
216: return new Vector().elements();
217: }
218:
219: public javax.servlet.jsp.JspWriter getOut() {
220: return out;
221: }
222:
223: public HttpSession getSession() {
224: if (session == null)
225: session = new JspSession(request);
226: return session;
227: }
228:
229: public Object getPage() {
230: return servlet;
231: }
232:
233: public ServletRequest getRequest() {
234: return request;
235: }
236:
237: public ServletResponse getResponse() {
238: return response;
239: }
240:
241: public Exception getException() {
242: return (Exception) request.getAttribute("exception");
243: }
244:
245: public ServletConfig getServletConfig() {
246: return servlet.getServletConfig();
247: }
248:
249: public ServletContext getServletContext() {
250: return getServletConfig().getServletContext();
251: }
252:
253: final String relativize(String url) throws ServletException {
254: if (url.charAt(0) != '/') {
255: String p = request.getServletPath();
256: int idx = p.lastIndexOf('/');
257: if (idx >= 0) {
258: url = p.substring(0, idx) + "/" + url;
259: }
260: }
261: return url;
262: }
263:
264: public void forward(String url) throws ServletException,
265: IOException {
266: url = relativize(url);
267: RequestDispatcher rd = getServletContext()
268: .getRequestDispatcher(url);
269: out.clearBuffer();
270: rd.forward(request, response);
271: }
272:
273: public void include(String url) throws ServletException,
274: IOException {
275: url = relativize(url);
276: RequestDispatcher rd = getServletContext()
277: .getRequestDispatcher(url);
278: rd.include(request, response);
279: }
280:
281: public void handleFinally() throws IOException {
282: if (autoFlush) {
283: out.flush();
284: }
285: }
286:
287: public void handlePageException(Exception e)
288: throws ServletException, IOException {
289: if (errorPageURL == null) {
290: if (Trace.level() > 1) {
291: Debug.print(e);
292: }
293: response
294: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
295: PrintWriter w = response.getWriter();
296: w.println("<html><head><title>");
297: w.println(e.toString());
298: w.println("</title></head><body>");
299: w.println("<pre>");
300: e.printStackTrace(w);
301: w.println("</pre></body></html>");
302: w.flush();
303: } else {
304: request.setAttribute("exception", e);
305: forward(errorPageURL);
306: }
307: }
308: }
|