1 29 30 package com.caucho.jsp; 31 32 import com.caucho.log.Log; 33 import com.caucho.server.connection.CauchoRequest; 34 import com.caucho.server.connection.CauchoResponse; 35 import com.caucho.server.connection.RequestAdapter; 36 import com.caucho.server.connection.ResponseAdapter; 37 import com.caucho.server.webapp.WebApp; 38 import com.caucho.util.L10N; 39 import com.caucho.vfs.ClientDisconnectException; 40 import com.caucho.vfs.JarPath; 41 import com.caucho.vfs.Path; 42 import com.caucho.vfs.Vfs; 43 44 import javax.servlet.*; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.http.HttpServletResponse ; 47 import java.io.FileNotFoundException ; 48 import java.io.IOException ; 49 import java.util.logging.Level ; 50 import java.util.logging.Logger ; 51 52 63 abstract public class QServlet implements Servlet { 64 static final String COPYRIGHT = 65 "Copyright(c) 1998-2006 Caucho Technology. All rights reserved."; 66 67 private static final Logger log = Log.open(QServlet.class); 68 private static final L10N L = new L10N(QServlet.class); 69 70 protected WebApp _webApp; 71 private PageManager _manager; 72 73 private ServletConfig _config; 74 75 80 public void init(ServletConfig config) throws ServletException 81 { 82 ServletContext cxt = config.getServletContext(); 83 _webApp = (WebApp) cxt; 84 85 _config = config; 86 87 log.finer(config.getServletName() + " init"); 88 } 89 90 93 protected void setManager(PageManager manager) 94 { 95 _manager = manager; 96 } 97 98 protected PageManager getManager() 99 { 100 return _manager; 101 } 102 103 106 public ServletContext getServletContext() 107 { 108 return _webApp; 109 } 110 111 114 public ServletConfig getServletConfig() 115 { 116 return _config; 117 } 118 119 122 public String getInitParameter(String name) 123 { 124 return _config.getInitParameter(name); 125 } 126 127 133 public void service(ServletRequest req, ServletResponse res) 134 throws ServletException, IOException 135 { 136 CauchoRequest request; 137 CauchoResponse response; 138 ResponseAdapter resAdapt = null; 139 140 if (req instanceof CauchoRequest) 141 request = (CauchoRequest) req; 142 else 143 request = RequestAdapter.create((HttpServletRequest ) req, _webApp); 144 145 if (res instanceof CauchoResponse) 146 response = (CauchoResponse) res; 147 else { 148 resAdapt = ResponseAdapter.create((HttpServletResponse ) res); 149 response = resAdapt; 150 } 151 152 Page page = null; 153 154 try { 155 page = getPage(request, response); 156 157 if (page == null) { 158 response.sendError(HttpServletResponse.SC_NOT_FOUND); 159 return; 160 } 161 162 page.service(request, response); 163 } 164 catch (JspParseException e) { 165 if (e.getErrorPage() != null) 166 forwardErrorPage(request, response, e, e.getErrorPage()); 167 else 168 throw new ServletException(e); 169 } 170 catch (ClientDisconnectException e) { 171 throw e; 172 } 173 catch (Throwable e) { 174 if (page != null && page.getErrorPage() != null && 175 forwardErrorPage(request, response, e, page.getErrorPage())) { 176 } 177 else if (e instanceof IOException ) { 178 log.log(Level.FINE, e.toString(), e); 179 throw (IOException ) e; 180 } 181 else if (e instanceof ServletException) { 182 log.log(Level.FINE, e.toString(), e); 183 throw (ServletException) e; 184 } 185 else { 186 log.log(Level.FINE, e.toString(), e); 187 throw new ServletException(e); 188 } 189 } 190 191 if (resAdapt != null) { 192 resAdapt.close(); 193 ResponseAdapter.free(resAdapt); 194 } 195 } 196 197 205 public Page getPage(HttpServletRequest request, 206 HttpServletResponse response) 207 throws Exception 208 { 209 try { 210 Page page = getSubPage(request, response); 211 212 return page; 213 } 214 catch (JspParseException e) { 215 if (e.getErrorPage() != null) { 216 if (e.getCause() != null && ! (e instanceof JspLineParseException)) 217 forwardErrorPage(request, response, e.getCause(), e.getErrorPage()); 218 else 219 forwardErrorPage(request, response, e, e.getErrorPage()); 220 221 return null; 222 } 223 else 224 throw e; 225 } 226 } 227 228 236 private Page getSubPage(HttpServletRequest req, HttpServletResponse res) 237 throws Exception 238 { 239 CauchoRequest cauchoRequest = null; 240 241 initGetPage(); 242 243 247 248 if (req instanceof CauchoRequest) 249 cauchoRequest = (CauchoRequest) req; 250 251 String servletPath; 252 253 if (cauchoRequest != null) 254 servletPath = cauchoRequest.getPageServletPath(); 255 else 256 servletPath = RequestAdapter.getPageServletPath(req); 257 258 if (servletPath == null) 259 servletPath = "/"; 260 261 String uri; 262 String pageURI; 263 264 if (cauchoRequest != null) 265 uri = cauchoRequest.getPageURI(); 266 else 267 uri = RequestAdapter.getPageURI(req); 268 269 Path appDir = _webApp.getAppDir(); 270 271 String realPath; 272 Path subcontext; 273 274 Page page; 275 276 String jspPath = (String ) req.getAttribute("caucho.jsp.jsp-file"); 277 if (jspPath != null) { 278 req.removeAttribute("caucho.jsp.jsp-file"); 279 280 subcontext = getPagePath(jspPath); 281 282 return _manager.getPage(uri, jspPath, subcontext); 283 } 284 285 String pathInfo; 286 287 if (cauchoRequest != null) 288 pathInfo = cauchoRequest.getPagePathInfo(); 289 else 290 pathInfo = RequestAdapter.getPagePathInfo(req); 291 292 subcontext = getPagePath(servletPath); 293 if (subcontext != null) 294 return _manager.getPage(servletPath, subcontext); 295 296 if (pathInfo == null) { 297 realPath = _webApp.getRealPath(servletPath); 298 subcontext = appDir.lookupNative(realPath); 299 300 return _manager.getPage(servletPath, subcontext); 301 } 302 303 subcontext = getPagePath(servletPath + pathInfo); 304 if (subcontext != null) 305 return _manager.getPage(servletPath + pathInfo, subcontext); 306 307 if (servletPath != null && ! servletPath.equals("")) { 310 throw new FileNotFoundException (L.l("{0} was not found on this server.", 312 uri)); 313 } 315 316 subcontext = getPagePath(pathInfo); 317 if (subcontext != null) 318 return _manager.getPage(pathInfo, subcontext); 319 320 subcontext = getPagePath(uri); 321 if (subcontext == null) 322 throw new FileNotFoundException (L.l("{0} was not found on this server.", 323 uri)); 324 325 return _manager.getPage(uri, subcontext); 326 } 327 328 private void initGetPage() 329 { 330 _webApp.getJspApplicationContext().getELListenerArray(); 332 } 333 334 public Page getPage(String uri, String pageURI) 335 throws Exception 336 { 337 Path path = getPagePath(pageURI); 338 339 if (path == null) 340 return null; 341 342 return _manager.getPage(uri, pageURI, path); 343 } 344 345 348 private Path getPagePath(String pathName) 349 { 350 Path appDir = _webApp.getAppDir(); 351 String realPath = _webApp.getRealPath(pathName); 352 Path path = appDir.lookupNative(realPath); 353 354 if (path.isFile() && path.canRead()) 355 return path; 356 357 java.net.URL url; 358 ClassLoader loader = _webApp.getClassLoader(); 359 if (loader != null) { 360 url = _webApp.getClassLoader().getResource(pathName); 361 362 String name = url != null ? url.toString() : null; 363 364 path = null; 365 if (url != null && (name.endsWith(".jar") || name.endsWith(".zip"))) 366 path = JarPath.create(Vfs.lookup(url.toString())).lookup(pathName); 367 else if (url != null) 368 path = Vfs.lookup(url.toString()); 369 370 if (path != null && path.isFile() && path.canRead()) 371 return path; 372 } 373 374 url = ClassLoader.getSystemResource(pathName); 375 String name = url != null ? url.toString() : null; 376 377 path = null; 378 if (url != null && (name.endsWith(".jar") || name.endsWith(".zip"))) 379 path = JarPath.create(Vfs.lookup(url.toString())).lookup(pathName); 380 else if (url != null) 381 path = Vfs.lookup(url.toString()); 382 383 if (path != null && path.isFile() && path.canRead()) 384 return path; 385 else 386 return null; 387 } 388 389 392 public void killPage(HttpServletRequest request, 393 HttpServletResponse response, 394 Page page) 395 { 396 _manager.killPage(request, response, page); 397 } 398 399 407 private boolean forwardErrorPage(HttpServletRequest req, 408 HttpServletResponse res, 409 Throwable e, String errorPage) 410 throws ServletException, IOException 411 { 412 req.setAttribute("javax.servlet.jsp.jspException", e); 413 req.setAttribute("javax.servlet.error.exception_type", e); 414 req.setAttribute("javax.servlet.error.request_uri", 415 req.getRequestURI()); 416 417 if (res instanceof CauchoResponse) { 418 CauchoResponse cauchoResponse = (CauchoResponse) res; 419 cauchoResponse.killCache(); 420 cauchoResponse.setNoCache(true); 421 } 422 423 RequestDispatcher rd = req.getRequestDispatcher(errorPage); 424 425 if (rd == null) 426 return false; 427 428 rd.forward(req, res); 429 430 return true; 431 } 432 433 public void destroy() 434 { 435 _manager.destroy(); 436 437 log.finer(_config.getServletName() + " destroy"); 438 } 439 } 440 | Popular Tags |