1 2 24 25 26 27 28 29 package com.lutris.appserver.server.httpPresentation; 30 31 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.util.Enumeration ; 35 36 import javax.servlet.Servlet ; 37 import javax.servlet.ServletContext ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.ServletRequest ; 40 import javax.servlet.http.Cookie ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 44 import com.lutris.appserver.server.Application; 45 import com.lutris.appserver.server.ApplicationException; 46 import com.lutris.appserver.server.StandardAppUtil; 47 import com.lutris.appserver.server.session.Session; 48 import com.lutris.appserver.server.user.User; 49 import com.lutris.appserver.server.user.UserImpl; 50 import com.lutris.logging.LogChannel; 51 import com.lutris.logging.Logger; 52 import com.lutris.util.ConfigException; 53 60 public class HttpPresentationManager { 61 62 66 private final int maxRedirectErrorLoops = 25; 67 68 71 private PresentationLoader loader; 72 73 76 private Application application; 77 78 81 private LogChannel logChannel; 82 83 86 private boolean debugLoggingEnabled; 87 88 91 private long nextRequestId = 0; 92 93 94 100 private Servlet servlet; 101 102 107 private ServletContext servletContext; 108 109 private long limitMilis = -11; 110 111 private boolean initSessionUser = false; 112 113 private String PARAM_LIMIT_MILIS = "PresentationManager.RequestExecutionLimit"; 114 115 private String PARAM_SESSION_USER = "PresentationManager.InitSessionUser"; 116 117 118 133 public HttpPresentationManager(String appPresentationPrefix, 134 Application presApplication, 135 ClassLoader applicationClassLoader, 136 boolean cacheClasses, 137 boolean cacheFiles) 138 throws HttpPresentationException { 139 140 application = presApplication; 141 logChannel = application.getLogChannel(); 142 if (logChannel != null) { 143 debugLoggingEnabled = logChannel.isEnabled(Logger.DEBUG); 144 } 145 loader = new PresentationLoader(appPresentationPrefix, 146 applicationClassLoader, 147 cacheClasses, 148 cacheFiles, 149 logChannel); 150 } 151 152 153 154 155 156 197 public void Run(HttpPresentationRequest request, 198 HttpPresentationResponse response) 199 throws HttpPresentationException, IOException { 200 201 long requestId; 202 203 synchronized (this) { 204 requestId = nextRequestId++; 205 } 206 207 208 HttpPresentationComms comms = 211 new HttpPresentationComms (request, response, application); 212 213 220 221 226 String presObjPath = request.getPresentationObjectRelativePath(); 227 228 if (presObjPath == null) { 229 presObjPath = "/"; 232 } 233 234 String requestedPrseObjPath = presObjPath; 235 int loopCount = 0; 236 237 Throwable exception = null; 239 240 presLoop: 241 while (true) { 242 logPresObjRun(comms, presObjPath, exception, 243 requestedPrseObjPath, requestId); 244 loopCount++; 245 try { 246 if (exception == null) { 247 if (runRequestPreprocessor(comms, requestId, 248 presObjPath)) { 249 break presLoop; } 251 } 252 runPresentationObj(comms, presObjPath, exception, application); 253 break presLoop; } catch (PageRedirectException except) { 255 presObjPath = PageRedirect.handler(comms, presObjPath, except, logChannel, requestId); 256 if (presObjPath == null) { 257 break presLoop; } 259 exception = null; } catch (PageUnauthorizedException except) { 261 ExceptionHandler.sendUnauthorizedPage(comms, 262 logChannel, 263 requestId, 264 application, 265 presObjPath, 266 except); 267 268 break presLoop; } catch (Throwable except) { 270 logChannel.write(Logger.DEBUG, "RID:" + requestId 271 + ": exception running presentation" 272 + presObjPath, 273 except); 274 if (HttpPresentationIOException.isClientIOException(except)) { 276 ExceptionHandler.logDisplayException(comms, logChannel, requestId, 279 application, except, request.getPathInfo()); 280 try { 282 runRequestPostProcessor(comms, requestId, presObjPath); 283 } catch (Throwable e) { 284 logChannel.write(Logger.DEBUG, "RID:" + requestId 286 + ": Request Post Proecessor exception: " 287 + presObjPath + ": " + e); 288 } 289 return; } 291 292 String exceptpresObjPath = presObjPath; 294 presObjPath = 295 ExceptionHandler.getErrorHandler(presObjPath, 296 requestedPrseObjPath, 297 (exception != null), 298 loader, 299 logChannel, 300 requestId); 301 if (presObjPath == null) { 302 ExceptionHandler.processUnhandledException(comms, 304 logChannel, 305 requestId, 306 application, 307 requestedPrseObjPath, 308 except); 309 break presLoop; } 311 exception = except; } 313 if (loopCount > maxRedirectErrorLoops) { 314 ExceptionHandler.maxRedirectsReached(comms, 315 logChannel, 316 requestId, 317 maxRedirectErrorLoops, 318 application, 319 presObjPath); 320 break presLoop; } 322 } 323 324 325 try { 326 response.flush(); 327 runRequestPostProcessor(comms, requestId, presObjPath); 328 } catch (Throwable except) { 329 ExceptionHandler.logDisplayException(comms, logChannel, requestId, application, except, 332 request.getPathInfo()); 333 } 334 } 335 336 337 340 private void logPresObjRun(HttpPresentationComms comms, 341 String presObjPath, 342 Throwable exception, 343 String requestedPrseObjPath, 344 long requestId) throws HttpPresentationException { 345 if (debugLoggingEnabled) { 346 if (exception != null) { 347 logChannel.write(Logger.DEBUG, "RID:" + requestId 348 + ": presentation run exception handler: " 349 + presObjPath + " for:\n" 350 + " " + exception.getClass().getName() 351 + ": " + exception.getMessage() 352 + "\nwhile accessing: " 353 + requestedPrseObjPath); 354 } else if (comms.request.getMethod().equals("HEAD")) { 355 logChannel.write(Logger.DEBUG, "RID:" + requestId 356 + ": presentation head : " + presObjPath); 357 } else { 358 logChannel.write(Logger.DEBUG, "RID:" + requestId 359 + ": presentation run: " + presObjPath); 360 } 361 } 362 } 363 364 365 369 public boolean servletRequestPreprocessor(Servlet me, 370 ServletContext context, 371 HttpServletRequest request, 372 HttpServletResponse response) 373 throws ServletException , IOException { 374 if (application != null) 375 return application.servletRequestPreprocessor( 376 me, context, request, response); 377 else 378 return false; 379 } 380 381 382 383 386 private boolean runRequestPreprocessor(HttpPresentationComms comms, 387 long requestId, 388 String presObjPath) throws Exception { 389 String checkPath = presObjPath.trim(); 391 if(checkPath.length() > 6){ checkPath = checkPath.substring(checkPath.length()-6,checkPath.length()); 393 if(checkPath.equalsIgnoreCase(".class")){ 394 comms.response.sendError(501, "Enhydra application: Access forbidden, *.class resource not alowed!"); 396 return true; 397 } 398 } 399 if (application.requestPreprocessor(comms)) { 401 if (debugLoggingEnabled) { 402 logChannel.write(Logger.DEBUG, "RID:" + requestId 403 + ": application.requestPreprocessor finished request: " 404 + presObjPath); 405 } 406 return true; 407 } 408 return false; 409 } 410 411 414 private void runRequestPostProcessor(HttpPresentationComms comms, 415 long requestId, 416 String presObjPath) 417 throws Exception { 418 application.requestPostProcessor(comms); 419 } 420 421 436 private void runPresentationObj(HttpPresentationComms comms, 437 String urlPath, 438 Throwable exception, 439 Application application) throws Throwable { 440 441 HttpPresentation presObj; 442 long milis = 0; 443 comms.exception = exception; 444 445 try { 446 initSessionUser = application.getConfig().getBoolean(PARAM_SESSION_USER, false); 447 } catch (ConfigException ex) { 448 logChannel.write(Logger.WARNING, 449 "Couldn't get "+ PARAM_SESSION_USER +" parameter",ex); 450 } 451 if (limitMilis < -9) { 452 try { 453 limitMilis = application.getConfig().getInt(PARAM_LIMIT_MILIS, -1); 454 } catch (ConfigException e) { 455 logChannel.write(Logger.WARNING, 456 "Couldn't get "+ PARAM_LIMIT_MILIS +" parameter", 457 e); 458 } finally { 459 if (limitMilis < 0) limitMilis = -1; 460 } 461 } 462 463 try { 466 presObj = loader.loadPresentation(urlPath); 467 } catch (ClassNotFoundException noClass) { 468 if (exception != null) { 469 throw exception; } else { 471 throw noClass; 472 } 473 } 474 475 if (0 <= limitMilis) { 476 milis = System.currentTimeMillis(); 477 } 478 479 presObj.run(comms); 480 481 if (initSessionUser){ 482 try{ 484 User userObj = comms.session.getUser(); 485 String userName = null; 486 if ( null==userObj ){ 487 userName = comms.request.getRemoteUser(); 488 if(null==userName){ 489 userName = comms.request.getHeader(":remoteuser"); 490 } 491 if (null == userName){ 492 userName = "ANONYMOUS"; 493 } 494 userObj = new UserImpl(userName); 495 comms.session.setUser(userObj); 496 } 497 }catch(NullPointerException ex){} 498 } 499 500 if (0 <= limitMilis) { 501 502 milis = System.currentTimeMillis() - milis; 503 505 String user = comms.request.getRemoteUser(); 506 if(null==user) 507 user = comms.request.getHeader(":remoteuser"); 508 509 Cookie [] c = comms.request.getCookies(); 510 if (null == user) 511 user = "ANONYMOUS"; 512 user += "("; 513 if (c != null) { 514 for (int n = 0; n < c.length; ++n) { 515 user += c[n].getName()+"="+c[n].getValue(); 516 if (c.length - 1 != n) { 517 user += ", "; 518 } 519 } 520 } 521 user += ")"; 522 if (0 == limitMilis || milis > limitMilis) { 523 String params =""; 524 for (Enumeration e = comms.request.getParameterNames(); 525 e.hasMoreElements();) { 526 String pName =(String )e.nextElement(); 527 params +="&"+ pName +"="+ comms.request.getParameter(pName); 528 } 529 logChannel.write(Logger.WARNING, comms.request.getMethod() 530 +" " 531 + urlPath 532 + params 533 +" requested by " 534 + user 535 +" from " 536 + comms.request.getRemoteHost() 537 +"[" 538 + comms.request.getRemoteAddr() 539 +"] took " 540 + milis 541 +"ms to run"); 542 } 543 544 } 545 } 546 547 548 553 public boolean isPOCacheEnabled() { 554 return loader.isPOCacheEnabled(); 555 } 556 557 562 public int sizeofPOCache() { 563 return loader.sizeofPOCache(); 564 } 565 566 571 public boolean isResourceCacheEnabled() { 572 return loader.isResourceCacheEnabled(); 573 } 574 575 580 public int sizeofResourceCache() { 581 return loader.sizeofResourceCache(); 582 } 583 584 596 public InputStream getAppFileAsStream(String appFileName) 597 throws IOException , HttpPresentationException { 598 return loader.getAppFileAsStream(appFileName); 599 } 600 601 607 public boolean isPresentationRequest(HttpPresentationRequest request) 608 throws HttpPresentationException { 609 return loader.isPresentationRequest(request); 610 } 611 612 613 628 public Session getSession(ServletRequest request) { 629 if (application == null) 630 return null; 631 Session s = null; 632 try { 633 s = StandardAppUtil.getRequestSession(request, application); 634 } catch (ApplicationException e) { 635 } 636 return s; 637 } 638 639 640 641 649 650 651 663 public void setServletAndContext(Servlet servlet, 664 ServletContext servletContext) { 665 this.servlet = servlet; 666 this.servletContext = servletContext; 667 } 668 669 670 680 public Servlet getServlet() { 681 return this.servlet; 682 } 683 684 685 696 public ServletContext getServletContext() { 697 return this.servletContext; 698 } 699 700 703 public void flushCache() { 704 loader.flushCache(); 705 } 706 707 708 public void addMimeType(String mimeType, String extension) { 709 loader.addMimeType(mimeType, extension); 710 } 711 712 717 public ClassLoader getAppClassLoader() { 718 return loader.getAppClassLoader(); 719 } 720 } 721
| Popular Tags
|