Quadcap Embeddable Server

com/quadcap/http/servlets/jsp/PageContext.java

Go to the documentation of this file.
00001 package com.quadcap.http.servlets.jsp; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 import java.io.PrintWriter; 00043 00044 import java.util.Enumeration; 00045 import java.util.Hashtable; 00046 import java.util.Vector; 00047 00048 import javax.servlet.RequestDispatcher; 00049 import javax.servlet.Servlet; 00050 import javax.servlet.ServletConfig; 00051 import javax.servlet.ServletContext; 00052 import javax.servlet.ServletException; 00053 import javax.servlet.ServletRequest; 00054 import javax.servlet.ServletResponse; 00055 00056 import javax.servlet.http.HttpServletRequest; 00057 import javax.servlet.http.HttpServletResponse; 00058 import javax.servlet.http.HttpSession; 00059 00060 import com.quadcap.http.server22.HttpResponse; 00061 00062 import com.quadcap.util.Debug; 00063 00064 /** 00065 * The PageContext implementation. 00066 * 00067 * @author Stan Bailes 00068 */ 00069 public class PageContext extends javax.servlet.jsp.PageContext { 00070 Hashtable pageAttributes = null; 00071 Servlet servlet; 00072 HttpServletRequest request; 00073 HttpServletResponse response; 00074 HttpSession session; 00075 String errorPageURL; 00076 boolean needsSession; 00077 int bufferSize; 00078 boolean autoFlush; 00079 javax.servlet.jsp.JspWriter out; 00080 00081 public void initialize(Servlet servlet, ServletRequest request, 00082 ServletResponse response, 00083 String errorPageURL, boolean needsSession, 00084 int bufferSize, 00085 boolean autoFlush) 00086 throws IOException, IllegalStateException, IllegalArgumentException 00087 { 00088 this.servlet = servlet; 00089 this.request = (HttpServletRequest)request; 00090 this.response = (HttpServletResponse)response; 00091 this.errorPageURL = errorPageURL; 00092 this.needsSession = needsSession; 00093 this.bufferSize = bufferSize; 00094 this.autoFlush = autoFlush; 00095 this.session = null; 00096 00097 if (response instanceof HttpResponse) { 00098 HttpResponse qresp = (HttpResponse)response; 00099 this.out = qresp.getJspWriter(bufferSize, autoFlush); 00100 } else { 00101 this.out = new JspWriter(response.getWriter(), bufferSize, autoFlush); 00102 } 00103 } 00104 00105 public void release() { 00106 this.servlet = null; 00107 this.request = null; 00108 this.response = null; 00109 this.errorPageURL = null; 00110 this.session = null; 00111 } 00112 00113 public void setAttribute(String name, Object val) { 00114 setAttribute(name, val, PAGE_SCOPE); 00115 } 00116 00117 public void setAttribute(String name, Object val, int scope) { 00118 switch (scope) { 00119 case PAGE_SCOPE: 00120 if (pageAttributes == null) pageAttributes = new Hashtable(); 00121 pageAttributes.put(name, val); 00122 break; 00123 case REQUEST_SCOPE: 00124 request.setAttribute(name, val); 00125 break; 00126 case SESSION_SCOPE: 00127 if (session != null) session.putValue(name, val); 00128 break; 00129 case APPLICATION_SCOPE: 00130 getServletContext().setAttribute(name, val); 00131 } 00132 } 00133 00134 public Object getAttribute(String name) { 00135 return getAttribute(name, PAGE_SCOPE); 00136 } 00137 00138 public Object getAttribute(String name, int scope) { 00139 switch (scope) { 00140 case PAGE_SCOPE: 00141 if (pageAttributes != null) return pageAttributes.get(name); 00142 break; 00143 case REQUEST_SCOPE: 00144 return request.getAttribute(name); 00145 case SESSION_SCOPE: 00146 if (session != null) return session.getValue(name); 00147 break; 00148 case APPLICATION_SCOPE: 00149 return getServletContext().getAttribute(name); 00150 } 00151 return null; 00152 } 00153 00154 public Object findAttribute(String name) { 00155 Object obj = null; 00156 for (int i = 0; obj == null && i < 4; i++) { 00157 obj = getAttribute(name, i); 00158 } 00159 return obj; 00160 } 00161 00162 public void removeAttribute(String name) { 00163 removeAttribute(name, PAGE_SCOPE); 00164 } 00165 00166 public void removeAttribute(String name, int scope) { 00167 switch (scope) { 00168 case PAGE_SCOPE: 00169 if (pageAttributes != null) pageAttributes.remove(name); 00170 break; 00171 case REQUEST_SCOPE: 00172 request.setAttribute(name, ""); // XXX no remove! 00173 break; 00174 case SESSION_SCOPE: 00175 if (session != null) session.removeValue(name); 00176 break; 00177 case APPLICATION_SCOPE: 00178 getServletContext().removeAttribute(name); 00179 } 00180 } 00181 00182 public int getAttributesScope(String name) { 00183 int scope = 0; 00184 for (int i = 0; scope == 0 && i < 4; i++) { 00185 if (getAttribute(name, i) != null) { 00186 scope = i+1; 00187 } 00188 } 00189 return scope; 00190 } 00191 00192 public Enumeration getAttributeNamesInScope(int scope) { 00193 switch (scope) { 00194 case PAGE_SCOPE: 00195 if (pageAttributes != null) return pageAttributes.keys(); 00196 break; 00197 case REQUEST_SCOPE: 00198 return request.getAttributeNames(); 00199 case SESSION_SCOPE: 00200 Vector v = new Vector(); 00201 if (session != null) { 00202 String[] s = session.getValueNames(); 00203 for (int i = 0; i < s.length; i++) v.addElement(s[i]); 00204 } 00205 return v.elements(); 00206 case APPLICATION_SCOPE: 00207 return getServletContext().getAttributeNames(); 00208 } 00209 return new Vector().elements(); 00210 } 00211 00212 public javax.servlet.jsp.JspWriter getOut() { 00213 return out; 00214 } 00215 00216 public HttpSession getSession() { 00217 if (session == null) session = new JspSession(request); 00218 return session; 00219 } 00220 00221 public Object getPage() { 00222 return servlet; 00223 } 00224 00225 public ServletRequest getRequest() { 00226 return request; 00227 } 00228 00229 public ServletResponse getResponse() { 00230 return response; 00231 } 00232 00233 public Exception getException() { 00234 return (Exception)request.getAttribute("exception"); 00235 } 00236 00237 public ServletConfig getServletConfig() { 00238 return servlet.getServletConfig(); 00239 } 00240 00241 public ServletContext getServletContext() { 00242 return getServletConfig().getServletContext(); 00243 } 00244 00245 final String relativize(String url) throws ServletException { 00246 if (url.charAt(0) != '/') { 00247 String p = request.getServletPath(); 00248 int idx = p.lastIndexOf('/'); 00249 if (idx >= 0) { 00250 url = p.substring(0, idx) + "/" + url; 00251 } 00252 } 00253 return url; 00254 } 00255 00256 public void forward(String url) throws ServletException, IOException { 00257 url = relativize(url); 00258 RequestDispatcher rd = getServletContext().getRequestDispatcher(url); 00259 out.clearBuffer(); 00260 rd.forward(request, response); 00261 } 00262 00263 public void include(String url) throws ServletException, IOException { 00264 url = relativize(url); 00265 RequestDispatcher rd = getServletContext().getRequestDispatcher(url); 00266 rd.include(request, response); 00267 } 00268 00269 public void handleFinally() throws IOException { 00270 if (autoFlush) { 00271 out.flush(); 00272 } 00273 } 00274 00275 public void handlePageException(Exception e) 00276 throws ServletException, IOException 00277 { 00278 if (errorPageURL == null) { 00279 if (Trace.level() > 1) { 00280 Debug.print(e); 00281 } 00282 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 00283 PrintWriter w = response.getWriter(); 00284 w.println("<html><head><title>"); 00285 w.println(e.toString()); 00286 w.println("</title></head><body>"); 00287 w.println("<pre>"); 00288 e.printStackTrace(w); 00289 w.println("</pre></body></html>"); 00290 w.flush(); 00291 } else { 00292 request.setAttribute("exception", e); 00293 forward(errorPageURL); 00294 } 00295 } 00296 }