1 package de.webman.template.jsp; 2 3 import java.io.IOException ; 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 ; 11 import org.apache.jasper.runtime.*; 12 46 public class WebManPageContext extends PageContext 47 { 48 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 errorPageURL; 59 60 protected boolean autoFlush; 61 protected int bufferSize; 62 63 protected transient Hashtable attributes = new Hashtable(); 65 66 68 protected transient ServletRequest request; 69 protected transient ServletResponse response; 70 protected transient Object page; 71 72 protected transient HttpSession session; 73 74 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 errorPageURL, 85 boolean needsSession, int bufferSize, 86 boolean autoFlush) 87 throws IOException , IllegalStateException , IllegalArgumentException 88 { 89 90 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 103 if (request instanceof HttpServletRequest && needsSession) 104 this.session = ((HttpServletRequest)request).getSession(); 105 if (needsSession && session == null) 106 throw new IllegalStateException ("Page needs a session and none is available"); 107 109 this.out = doCreateOut(bufferSize, autoFlush); if (this.out == null) 111 throw new IllegalStateException ("failed initialize JspWriter"); 112 113 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 } 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; session = null; 140 141 attributes.clear(); 142 } 143 144 public Object getAttribute(String name) 145 { 146 return attributes.get(name); 147 } 148 149 150 public Object getAttribute(String 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 ("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 ("unidentified scope"); 170 } 171 } 172 173 174 public void setAttribute(String name, Object attribute) 175 { 176 attributes.put(name, attribute); 177 } 178 179 180 public void setAttribute(String name, Object 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 ("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 name) 207 { 208 attributes.remove(name); 209 } 210 211 public void removeAttribute(String 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 ("cant remove Attributes from request scope"); 221 222 case SESSION_SCOPE: 223 if (session == null) 224 throw new IllegalArgumentException ("can't access SESSION_SCOPE without an HttpSession"); 225 else 226 session.removeAttribute(name); 227 break; 231 232 case APPLICATION_SCOPE: 233 context.removeAttribute(name); 234 break; 235 236 default: 237 } 238 } 239 240 public int getAttributesScope(String 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 findAttribute(String name) 258 { 259 Object 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 ("can't access SESSION_SCOPE without an HttpSession"); 289 290 case APPLICATION_SCOPE: 291 return context.getAttributeNames(); 292 293 default: return new Enumeration() { public boolean hasMoreElements() { return false; } 295 296 public Object 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 getException() { return (Exception )request.getAttribute(EXCEPTION); } 310 public Object getPage() { return servlet; } 311 312 313 private final String getAbsolutePathRelativeToContext(String relativeUrlPath) 314 { 315 String path = relativeUrlPath; 316 317 if (!path.startsWith("/")) { 318 String uri = (String ) request.getAttribute("javax.servlet.include.servlet_path"); 319 if (uri == null) 320 uri = ((HttpServletRequest) request).getServletPath(); 321 String baseURI = uri.substring(0, uri.lastIndexOf('/')); 322 path = baseURI+'/'+path; 323 } 324 return path; 325 } 326 327 public void include(String relativeUrlPath) 328 throws ServletException, IOException 329 { 330 String path = getAbsolutePathRelativeToContext(relativeUrlPath); 331 context.getRequestDispatcher(path).include(request, response); 332 } 333 334 public void forward(String relativeUrlPath) 335 throws ServletException, IOException 336 { 337 String path = getAbsolutePathRelativeToContext(relativeUrlPath); 338 context.getRequestDispatcher(path).forward(request, response); 339 } 340 341 Stack writerStack = new Stack(); 342 343 public BodyContent pushBody() 344 { 345 JspWriter previous = out; 346 writerStack.push(out); 347 out = new BodyContentImpl(previous); 348 return (BodyContent ) out; 349 } 350 351 public JspWriter popBody() 352 { 353 out = (JspWriter) writerStack.pop(); 354 return out; 355 } 356 357 public void handlePageException(Exception e) 358 throws IOException , ServletException 359 { 360 handlePageException((Throwable )e); 361 } 362 363 public void handlePageException(Throwable e) 364 throws IOException , ServletException 365 { 366 367 request.setAttribute("javax.servlet.jsp.jspException", e); 369 370 if (errorPageURL != null && !errorPageURL.equals("")) { 371 forward(errorPageURL); 372 } else { 374 throw new ServletException(e); 377 } 378 379 } 380 381 protected JspWriter doCreateOut(int bufferSize, boolean autoFlush) 382 throws IOException , IllegalArgumentException 383 { 384 return new JspWriterImpl(response, bufferSize, autoFlush); 385 } 386 387 } 388 | Popular Tags |