1 13 package info.magnolia.cms.servlets; 14 15 import info.magnolia.cms.Aggregator; 16 import info.magnolia.cms.Dispatcher; 17 import info.magnolia.cms.beans.config.ConfigLoader; 18 import info.magnolia.cms.beans.config.VirtualMap; 19 import info.magnolia.cms.beans.runtime.Cache; 20 import info.magnolia.cms.core.CacheHandler; 21 import info.magnolia.cms.core.CacheProcess; 22 import info.magnolia.cms.core.Path; 23 import info.magnolia.cms.security.Permission; 24 import info.magnolia.cms.security.SessionAccessControl; 25 26 import java.io.BufferedReader ; 27 import java.io.IOException ; 28 import java.io.UnsupportedEncodingException ; 29 import java.security.Principal ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 35 import javax.jcr.RepositoryException; 36 import javax.servlet.RequestDispatcher ; 37 import javax.servlet.ServletInputStream ; 38 import javax.servlet.http.Cookie ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import javax.servlet.http.HttpSession ; 43 44 import org.apache.commons.lang.StringUtils; 45 import org.apache.log4j.Logger; 46 47 48 55 public class EntryServlet extends HttpServlet { 56 57 60 public static final String INTERCEPT = "mgnlIntercept"; 62 65 private static final long serialVersionUID = 222L; 66 67 70 private static Logger log = Logger.getLogger(EntryServlet.class); 71 72 75 private static final String REQUEST_INTERCEPTOR = "/RequestInterceptor"; 77 82 public long getLastModified(HttpServletRequest request) { 83 return info.magnolia.cms.beans.runtime.Cache.getCreationTime(request); 84 } 85 86 95 protected boolean allowCaching(HttpServletRequest req) { 96 return true; 97 } 98 99 105 public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { 106 107 if (ConfigLoader.isBootstrapping()) { 108 res.getWriter().write("Magnolia bootstrapping has failed, check bootstrap.log in magnolia/logs"); return; 111 } 112 113 try { 114 if (isAuthorized(req, res)) { 115 116 boolean cacheable = allowCaching(req); 118 119 if (cacheable && Cache.isCached(req)) { 121 if (CacheHandler.streamFromCache(req, res)) { 122 return; } 124 } 125 if (redirect(req, res)) { 126 127 if (cacheable) { 129 this.cacheRequest(req); 130 } 131 return; 132 } 133 intercept(req, res); 134 135 Aggregator aggregator = new Aggregator(req, res); 137 boolean success = aggregator.collect(); 138 if (success) { 139 try { 140 Dispatcher.dispatch(req, res, getServletContext()); 141 } 142 catch (Exception e) { 143 log.error(e.getMessage(), e); 144 } 145 this.cacheRequest(req); 146 } 147 else { 148 if (log.isDebugEnabled()) { 149 log.debug("Resource not found, redirecting request for [" + req.getRequestURI() + "] to 404 URI"); } 152 153 if (!res.isCommitted()) { 154 res.sendError(HttpServletResponse.SC_NOT_FOUND); 155 } 156 else { 157 log.info("Unable to redirect to 404 page, response is already committed"); } 159 } 160 } 161 } 162 catch (RepositoryException e) { 163 log.error(e.getMessage(), e); 164 } 165 catch (RuntimeException e) { 166 log.error(e.getMessage(), e); 167 } 168 } 169 170 176 public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { 177 doGet(req, res); 178 } 179 180 187 protected boolean isAuthorized(HttpServletRequest req, HttpServletResponse res) throws IOException { 188 if (SessionAccessControl.getAccessManager(req) != null) { 189 String path = StringUtils.substringBefore(Path.getURI(req), "."); if (!SessionAccessControl.getAccessManager(req).isGranted(path, Permission.READ)) { 191 res.sendError(HttpServletResponse.SC_FORBIDDEN); 192 } 193 } 194 return true; 195 } 196 197 203 private boolean redirect(HttpServletRequest request, HttpServletResponse response) { 204 String uri = this.getURIMap(request); 205 if (StringUtils.isNotEmpty(uri)) { 206 try { 207 Path.resetURI(request); 209 request.getRequestDispatcher(uri).forward(request, response); 210 } 211 catch (Exception e) { 212 log.error("Failed to forward - " + uri); log.error(e.getMessage(), e); 214 } 215 return true; 216 } 217 return false; 218 } 219 220 225 private void intercept(HttpServletRequest request, HttpServletResponse response) { 226 if (request.getParameter(INTERCEPT) != null) { 227 try { 228 request.getRequestDispatcher(REQUEST_INTERCEPTOR).include(request, response); 229 } 230 catch (Exception e) { 231 log.error("Failed to Intercept"); log.error(e.getMessage(), e); 233 } 234 } 235 } 236 237 241 private String getURIMap(HttpServletRequest request) { 242 return VirtualMap.getURIMapping(StringUtils.substringAfter(request.getRequestURI(), request.getContextPath())); 243 } 244 245 249 private void cacheRequest(HttpServletRequest request) { 250 if (!Cache.isInCacheProcess(request) && info.magnolia.cms.beans.config.Cache.isCacheable(request)) { 251 CacheProcess cache = new CacheProcess(new ClonedRequest(request)); 252 cache.start(); 253 } 254 } 255 256 259 private static class ClonedRequest implements HttpServletRequest { 260 261 264 private Map attributes = new HashMap (); 265 266 269 private Map headers = new HashMap (); 270 271 274 private Map parameters; 275 276 279 private String uri; 280 281 284 private String contextPath; 285 286 289 private String characterEncoding; 290 291 294 private int serverPort; 295 296 299 private String scheme; 300 301 304 private String serverName; 305 306 309 private String method; 310 311 315 public ClonedRequest(HttpServletRequest originalRequest) { 316 this.contextPath = originalRequest.getContextPath(); 317 this.uri = originalRequest.getRequestURI(); 319 this.characterEncoding = originalRequest.getCharacterEncoding(); 320 this.attributes.put(Aggregator.EXTENSION, originalRequest.getAttribute(Aggregator.EXTENSION)); 322 this.attributes.put(Aggregator.ACTPAGE, originalRequest.getAttribute(Aggregator.ACTPAGE)); 323 324 String authHeader = originalRequest.getHeader("Authorization"); if (authHeader != null) { 327 this.headers.put("Authorization", authHeader); } 329 330 this.serverPort = originalRequest.getServerPort(); 332 this.scheme = originalRequest.getScheme(); 333 this.serverName = originalRequest.getServerName(); 334 this.method = originalRequest.getMethod(); 335 this.parameters = originalRequest.getParameterMap(); 336 } 337 338 341 public String getRequestURI() { 342 return uri; 343 } 344 345 348 public String getHeader(String key) { 349 return (String ) this.headers.get(key); 350 } 351 352 355 public Object getAttribute(String key) { 356 return attributes.get(key); 357 } 358 359 363 public String getContextPath() { 364 return this.contextPath; 365 } 366 367 370 public String getCharacterEncoding() { 371 return this.characterEncoding; 372 } 373 374 377 public String getScheme() { 378 return this.scheme; 379 } 380 381 384 public String getServerName() { 385 return this.serverName; 386 } 387 388 391 public int getServerPort() { 392 return this.serverPort; 393 } 394 395 398 public String getMethod() { 399 return this.method; 400 } 401 402 405 public Map getParameterMap() { 406 return this.parameters; 407 } 408 409 412 public String getParameter(String s) { 413 return (String ) this.parameters.get(s); 414 } 415 416 420 public StringBuffer getRequestURL() { 421 throw new UnsupportedOperationException (); 422 } 423 424 428 public String getServletPath() { 429 throw new UnsupportedOperationException (); 430 } 431 432 436 public HttpSession getSession(boolean b) { 437 throw new UnsupportedOperationException (); 438 } 439 440 444 public HttpSession getSession() { 445 throw new UnsupportedOperationException (); 446 } 447 448 452 public boolean isRequestedSessionIdValid() { 453 throw new UnsupportedOperationException (); 454 } 455 456 460 public boolean isRequestedSessionIdFromCookie() { 461 throw new UnsupportedOperationException (); 462 } 463 464 468 public boolean isRequestedSessionIdFromURL() { 469 throw new UnsupportedOperationException (); 470 } 471 472 476 public boolean isRequestedSessionIdFromUrl() { 477 throw new UnsupportedOperationException (); 478 } 479 480 484 public String getAuthType() { 485 throw new UnsupportedOperationException (); 486 } 487 488 492 public Cookie [] getCookies() { 493 throw new UnsupportedOperationException (); 494 } 495 496 500 public long getDateHeader(String s) { 501 throw new UnsupportedOperationException (); 502 } 503 504 508 public Enumeration getHeaders(String s) { 509 throw new UnsupportedOperationException (); 510 } 511 512 516 public Enumeration getHeaderNames() { 517 throw new UnsupportedOperationException (); 518 } 519 520 524 public int getIntHeader(String s) { 525 throw new UnsupportedOperationException (); 526 } 527 528 532 public String getPathInfo() { 533 throw new UnsupportedOperationException (); 534 } 535 536 540 public String getPathTranslated() { 541 throw new UnsupportedOperationException (); 542 } 543 544 548 public String getQueryString() { 549 throw new UnsupportedOperationException (); 550 } 551 552 556 public String getRemoteUser() { 557 throw new UnsupportedOperationException (); 558 } 559 560 564 public boolean isUserInRole(String s) { 565 throw new UnsupportedOperationException (); 566 } 567 568 572 public Principal getUserPrincipal() { 573 throw new UnsupportedOperationException (); 574 } 575 576 580 public String getRequestedSessionId() { 581 throw new UnsupportedOperationException (); 582 } 583 584 588 public Enumeration getAttributeNames() { 589 throw new UnsupportedOperationException (); 590 } 591 592 596 public void setCharacterEncoding(String s) throws UnsupportedEncodingException { 597 throw new UnsupportedOperationException (); 598 } 599 600 604 public int getContentLength() { 605 throw new UnsupportedOperationException (); 606 } 607 608 612 public String getContentType() { 613 throw new UnsupportedOperationException (); 614 } 615 616 620 public ServletInputStream getInputStream() throws IOException { 621 throw new UnsupportedOperationException (); 622 } 623 624 628 public Enumeration getParameterNames() { 629 throw new UnsupportedOperationException (); 630 } 631 632 636 public String [] getParameterValues(String s) { 637 throw new UnsupportedOperationException (); 638 } 639 640 644 public String getProtocol() { 645 throw new UnsupportedOperationException (); 646 } 647 648 652 public BufferedReader getReader() throws IOException { 653 throw new UnsupportedOperationException (); 654 } 655 656 660 public String getRemoteAddr() { 661 throw new UnsupportedOperationException (); 662 } 663 664 668 public String getRemoteHost() { 669 throw new UnsupportedOperationException (); 670 } 671 672 676 public void setAttribute(String s, Object o) { 677 this.attributes.put(s, o); 678 } 679 680 684 public void removeAttribute(String s) { 685 this.attributes.remove(s); 686 } 687 688 692 public Locale getLocale() { 693 throw new UnsupportedOperationException (); 694 } 695 696 700 public Enumeration getLocales() { 701 throw new UnsupportedOperationException (); 702 } 703 704 708 public boolean isSecure() { 709 throw new UnsupportedOperationException (); 710 } 711 712 716 public RequestDispatcher getRequestDispatcher(String s) { 717 throw new UnsupportedOperationException (); 718 } 719 720 724 public String getRealPath(String s) { 725 throw new UnsupportedOperationException (); 726 } 727 728 732 public int getRemotePort() { 733 throw new UnsupportedOperationException (); 734 } 735 736 740 public String getLocalName() { 741 throw new UnsupportedOperationException (); 742 } 743 744 748 public String getLocalAddr() { 749 throw new UnsupportedOperationException (); 750 } 751 752 756 public int getLocalPort() { 757 throw new UnsupportedOperationException (); 758 } 759 760 } 761 762 } 763 | Popular Tags |