1 48 49 50 package com.caucho.portal.generic; 51 52 import javax.portlet.PortletContext; 53 import javax.portlet.PortletRequest; 54 import javax.portlet.PortletSecurityException; 55 import javax.portlet.PortletSession; 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 import javax.servlet.http.HttpSession ; 59 import java.io.BufferedReader ; 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.io.OutputStream ; 63 import java.io.PrintWriter ; 64 import java.io.UnsupportedEncodingException ; 65 import java.security.Principal ; 66 import java.util.Enumeration ; 67 import java.util.LinkedHashSet ; 68 import java.util.Locale ; 69 import java.util.Set ; 70 71 72 75 public class HttpPortletConnection 76 extends PortletConnection 77 { 78 81 final static public String HTTP_SERVLET_REQUEST 82 = HttpPortletRequestDispatcher.HTTP_SERVLET_REQUEST; 83 84 87 final static public String HTTP_SERVLET_RESPONSE 88 = HttpPortletRequestDispatcher.HTTP_SERVLET_RESPONSE; 89 90 public static HttpServletRequest getHttpRequest(PortletRequest request) 91 { 92 return (HttpServletRequest ) request.getAttribute(HTTP_SERVLET_REQUEST); 93 } 94 95 public static HttpServletResponse getHttpResponse(PortletRequest request) 96 { 97 return (HttpServletResponse ) request.getAttribute(HTTP_SERVLET_RESPONSE); 98 } 99 100 private PortletContext _portletContext; 101 private HttpServletRequest _httpRequest; 102 private HttpServletResponse _httpResponse; 103 104 private Object _oldHttpRequest; 105 private Object _oldHttpResponse; 106 107 private String _servletUrl; 108 109 private MapBasedInvocationFactory _createdInvocationFactory; 110 111 private HttpPortletSession _portletSession; 112 113 private Set <Locale > _clientLocales; 114 private Set <String > _clientCharacterEncodings; 115 private Set <String > _clientContentTypes; 116 117 private boolean _isLocaleEstablished; 118 private boolean _isContentTypeEstablished; 119 120 public HttpPortletConnection() 121 { 122 } 123 124 public void start(Portal portal, 125 PortletContext portletContext, 126 HttpServletRequest httpRequest, 127 HttpServletResponse httpResponse, 128 boolean useParameters) 129 { 130 if (_createdInvocationFactory != null) 131 throw new IllegalStateException ("missing finish?"); 132 133 _createdInvocationFactory = new MapBasedInvocationFactory(); 134 135 if (useParameters) 136 _createdInvocationFactory.start(httpRequest.getParameterMap()); 137 else 138 _createdInvocationFactory.start(null); 139 140 start(portal, 141 portletContext, 142 httpRequest, 143 httpResponse, 144 _createdInvocationFactory); 145 } 146 147 public void start(Portal portal, 148 PortletContext portletContext, 149 HttpServletRequest httpRequest, 150 HttpServletResponse httpResponse, 151 InvocationFactory invocationFactory) 152 { 153 super.start(portal, invocationFactory); 154 155 _portletContext = portletContext; 156 _httpRequest = httpRequest; 157 _httpResponse = httpResponse; 158 159 _oldHttpRequest = _httpRequest.getAttribute(HTTP_SERVLET_REQUEST); 160 161 if (_oldHttpRequest != null) 162 _oldHttpResponse=_httpRequest.getAttribute(HTTP_SERVLET_RESPONSE); 163 164 _httpRequest.setAttribute(HTTP_SERVLET_REQUEST, _httpRequest); 165 _httpRequest.setAttribute(HTTP_SERVLET_RESPONSE, _httpResponse); 166 167 _servletUrl = makeServletUrl(_httpRequest); 168 } 169 170 171 protected String makeServletUrl(HttpServletRequest request) 172 { 173 String scheme = request.getScheme(); 174 String serverName = request.getServerName(); 175 int port = request.getServerPort(); 176 177 if (port == 80 && scheme.equals("http")) 178 port = -1; 179 180 if (port == 443 && scheme.equals("https")) 181 port = -1; 182 183 String contextPath 184 = (String ) request.getAttribute("javax.servlet.include.context_path"); 185 186 String servletPath; 187 188 if (contextPath == null) { 189 contextPath = request.getContextPath(); 190 servletPath = request.getServletPath(); 191 } 192 else { 193 servletPath = (String ) request.getAttribute("javax.servlet.include.servlet_path"); 194 } 195 196 StringBuffer buf = new StringBuffer (256); 197 198 buf.append(scheme); 199 buf.append("://"); 200 buf.append(serverName); 201 if (port > 0) { 202 buf.append(':'); 203 buf.append(port); 204 } 205 buf.append(contextPath); 206 buf.append(servletPath); 207 208 return buf.toString(); 209 } 210 211 public void finish() 212 { 213 int expirationCache = getExpirationCache(); 214 215 if (expirationCache == 0) { 216 _httpResponse.setHeader( "Cache-Control", 217 "no-cache,post-check=0,pre-check=0" ); 218 219 _httpResponse.setHeader("Pragma", "no-cache"); 220 _httpResponse.setHeader("Expires", "Thu,01Dec199416:00:00GMT"); 221 } 222 else { 223 if (isPrivate()) { 224 _httpResponse.setHeader( "Cache-Control", 225 "private,max-age=" + expirationCache ); 226 } 227 else { 228 _httpResponse.setHeader( "Cache-Control", 229 "max-age=" + expirationCache ); 230 } 231 } 232 233 _isLocaleEstablished = false; 234 _isContentTypeEstablished = false; 235 236 _clientLocales = null; 237 _clientCharacterEncodings = null; 238 _clientContentTypes = null; 239 240 PortletContext portletContext = _portletContext; 241 MapBasedInvocationFactory createdInvocationFactory 242 = _createdInvocationFactory; 243 HttpServletRequest httpRequest = _httpRequest; 244 Object oldHttpRequest = _oldHttpRequest; 245 HttpServletResponse httpResponse = _httpResponse; 246 Object oldHttpResponse = _oldHttpResponse; 247 HttpPortletSession portletSession = _portletSession; 248 249 _servletUrl = null; 250 251 _portletContext = null; 252 _portletSession = null; 253 _oldHttpRequest = null; 254 _oldHttpResponse = null; 255 _createdInvocationFactory = null; 256 _httpRequest = null; 257 _httpResponse = null; 258 259 httpRequest.setAttribute(HTTP_SERVLET_RESPONSE, oldHttpResponse); 260 httpRequest.setAttribute(HTTP_SERVLET_REQUEST, oldHttpRequest); 261 262 super.finish(); 263 264 if (portletSession != null) 265 portletSession.finish(); 266 267 if (createdInvocationFactory != null) 268 createdInvocationFactory.finish(); 269 } 270 271 public HttpServletRequest getHttpRequest() 272 { 273 return _httpRequest; 274 } 275 276 public HttpServletResponse getHttpResponse() 277 { 278 return _httpResponse; 279 } 280 281 282 293 public Set <String > getClientContentTypes() 294 { 295 if (_clientContentTypes != null) 296 return _clientContentTypes; 297 298 _clientContentTypes = HttpUtil.getHeaderElements(getProperty("Accept")); 299 300 return _clientContentTypes; 301 } 302 303 316 public Set <Locale > getClientLocales() 317 { 318 if (_clientLocales != null) 319 return _clientLocales; 320 321 _clientLocales = new LinkedHashSet <Locale >(); 322 _clientLocales.add(_httpRequest.getLocale()); 323 324 Enumeration en = _httpRequest.getLocales(); 325 326 while (en.hasMoreElements()) { 327 _clientLocales.add( (Locale ) en.nextElement() ); 328 } 329 330 331 return _clientLocales; 332 } 333 334 345 public Set <String > getClientCharacterEncodings() 346 { 347 if (_clientCharacterEncodings != null) 348 return _clientCharacterEncodings; 349 350 _clientCharacterEncodings 351 = HttpUtil.getHeaderElements(getProperty("Accept-Charset")); 352 353 return _clientCharacterEncodings; 354 } 355 356 public String resolveURL(String url) 357 { 358 StringBuffer buf = new StringBuffer (256); 359 appendUrlPrefix(_httpRequest, buf); 360 buf.append(url); 361 return buf.toString(); 362 } 363 364 376 public String resolveURL(String url, boolean secure) 377 throws PortletSecurityException 378 { 379 if (secure == false) 380 return resolveURL(url); 381 else { 382 StringBuffer buf = new StringBuffer (256); 383 384 appendSecureUrlPrefix(_httpRequest, buf); 385 buf.append(url); 386 return buf.toString(); 387 } 388 } 389 390 private String appendUrlPrefix(HttpServletRequest request, StringBuffer buf) 391 { 392 buf.append(_servletUrl); 393 return buf.toString(); 394 } 395 396 private String appendSecureUrlPrefix( HttpServletRequest request, 397 StringBuffer buf ) 398 throws PortletSecurityException 399 { 400 402 if (request.isSecure()) 403 return appendUrlPrefix(request, buf); 404 else 405 throw new PortletSecurityException("cannot make url secure"); 406 } 407 408 public boolean handleConstraintFailure( Constraint constraint, 409 int failureCode ) 410 throws IOException 411 { 412 if (failureCode == Constraint.SC_FORBIDDEN) { 413 _httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 414 } 415 else { 416 _httpResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); 417 } 418 419 return true; 420 } 421 422 public boolean handleException(Exception exception) 423 { 424 return false; 425 } 426 427 431 public boolean canGuaranteeIntegrity() 432 { 433 return isSecure(); 434 } 435 436 440 public boolean canGuaranteeConfidentiality() 441 { 442 return isSecure(); 443 } 444 445 448 public Object getAttribute(String name) 449 { 450 return _httpRequest.getAttribute(name); 451 } 452 453 456 public void setAttribute(String name, Object o) 457 { 458 _httpRequest.setAttribute(name,o); 459 } 460 461 464 public void removeAttribute(String name) 465 { 466 _httpRequest.removeAttribute(name); 467 } 468 469 472 public Enumeration getAttributeNames() 473 { 474 return _httpRequest.getAttributeNames(); 475 } 476 477 public PortletSession getPortletSession(boolean create) 478 { 479 if (_portletSession != null) 480 return _portletSession; 481 482 HttpSession httpSession = _httpRequest.getSession(create); 483 484 if (httpSession != null) { 485 _portletSession = new HttpPortletSession(); 487 _portletSession.start(_portletContext, httpSession); 488 } 489 490 return _portletSession; 491 } 492 493 public String getScheme() 494 { 495 return _httpRequest.getScheme(); 496 } 497 498 public String getServerName() 499 { 500 return _httpRequest.getServerName(); 501 } 502 503 public int getServerPort() 504 { 505 return _httpRequest.getServerPort(); 506 } 507 508 public String getContextPath() 509 { 510 return _httpRequest.getContextPath(); 511 } 512 513 public String getAuthType() 514 { 515 String authType = _httpRequest.getAuthType(); 516 517 if (authType == null) 518 return null; 519 else if (authType == HttpServletRequest.BASIC_AUTH) 520 return PortletRequest.BASIC_AUTH; 521 525 else if (authType == HttpServletRequest.DIGEST_AUTH) 526 return PortletRequest.DIGEST_AUTH; 527 else if (authType == HttpServletRequest.FORM_AUTH) 528 return PortletRequest.FORM_AUTH; 529 else if (authType.equals(HttpServletRequest.BASIC_AUTH)) 530 return PortletRequest.BASIC_AUTH; 531 else if (authType.equals("CLIENT_CERT")) return PortletRequest.CLIENT_CERT_AUTH; 533 else if (authType.equals(HttpServletRequest.DIGEST_AUTH)) 534 return PortletRequest.DIGEST_AUTH; 535 else if (authType.equals(HttpServletRequest.FORM_AUTH)) 536 return PortletRequest.FORM_AUTH; 537 else 538 return authType; 539 } 540 541 public boolean isSecure() 542 { 543 return _httpRequest.isSecure(); 544 } 545 546 public String getRequestedSessionId() 547 { 548 return _httpRequest.getRequestedSessionId(); 549 } 550 551 public boolean isRequestedSessionIdValid() 552 { 553 return _httpRequest.isRequestedSessionIdValid(); 554 } 555 556 public String getRemoteUser() 557 { 558 return _httpRequest.getRemoteUser(); 559 } 560 561 public Principal getUserPrincipal() 562 { 563 return _httpRequest.getUserPrincipal(); 564 } 565 566 public boolean isUserInRole(String role) 567 { 568 return _httpRequest.isUserInRole(role); 569 } 570 571 public String getProperty(String propertyName) 572 { 573 return _httpRequest.getHeader(propertyName); 574 } 575 576 public Enumeration getProperties(String propertyName) 577 { 578 return _httpRequest.getHeaders(propertyName); 579 } 580 581 public Enumeration getPropertyNames() 582 { 583 return _httpRequest.getHeaderNames(); 584 } 585 586 public String getSubmitContentType() 587 { 588 return _httpRequest.getContentType(); 589 } 590 591 public int getSubmitContentLength() 592 { 593 return _httpRequest.getContentLength(); 594 } 595 596 public InputStream getSubmitInputStream() 597 throws IOException 598 { 599 return _httpRequest.getInputStream(); 600 } 601 602 public void setSubmitCharacterEncoding(String enc) 603 throws UnsupportedEncodingException , IllegalStateException 604 { 605 _httpRequest.setCharacterEncoding(enc); 606 } 607 608 public String getSubmitCharacterEncoding() 609 { 610 return _httpRequest.getCharacterEncoding(); 611 } 612 613 public BufferedReader getSubmitReader() 614 throws UnsupportedEncodingException , IOException 615 { 616 return _httpRequest.getReader(); 617 } 618 619 628 public String encodeURL(String location) 629 { 630 int slash = location.indexOf('/'); 631 int colon = location.indexOf(':'); 632 633 if (colon == -1 || slash < colon ) { 634 635 String scheme = _httpRequest.getScheme(); 636 String serverName = _httpRequest.getServerName(); 637 int port = _httpRequest.getServerPort(); 638 639 if (port == 80 && scheme.equals("http")) 640 port = -1; 641 642 if (port == 443 && scheme.equals("https")) 643 port = -1; 644 645 String contextPath = (String ) _httpRequest.getAttribute("javax.servlet.include.context_path"); 646 String servletPath = null; 647 648 if (contextPath == null) { 649 contextPath = _httpRequest.getContextPath(); 650 servletPath = _httpRequest.getServletPath(); 651 } 652 653 StringBuffer buf = new StringBuffer (); 654 655 buf.append(scheme); 656 buf.append("://"); 657 buf.append(serverName); 658 659 if (port > 0) { 660 buf.append(':'); 661 buf.append(port); 662 } 663 664 buf.append(contextPath); 665 666 if ( slash != 0 ) { 667 668 if (servletPath == null) 669 servletPath = (String ) _httpRequest.getAttribute("javax.servlet.include.servlet_path"); 670 671 buf.append(servletPath); 672 673 buf.append('/'); 674 675 buf.append(location); 676 } 677 else { 678 buf.append(location); 679 } 680 681 682 location = buf.toString(); 683 } 684 685 return _httpResponse.encodeURL(location); 686 } 687 688 public void sendRedirect(String location) 689 throws IOException 690 { 691 String url = _httpResponse.encodeRedirectURL(location); 692 693 _httpResponse.sendRedirect(url); 694 } 695 696 public void setProperty(String name, String value) 697 { 698 _httpResponse.setHeader(name, value); 699 } 700 701 public void addProperty(String name, String value) 702 { 703 _httpResponse.addHeader(name, value); 704 } 705 706 public void setContentType(String contentType) 707 { 708 _isContentTypeEstablished = true; 709 _httpResponse.setContentType(contentType); 710 } 711 712 716 public String getContentType() 717 { 718 if (_isContentTypeEstablished) 719 return _httpResponse.getContentType(); 720 else 721 return null; 722 } 723 724 public void setLocale(Locale locale) 725 { 726 _isLocaleEstablished = true; 727 _httpResponse.setLocale(locale); 728 } 729 730 734 public Locale getLocale() 735 { 736 if (_isLocaleEstablished) 737 return _httpResponse.getLocale(); 738 else 739 return null; 740 } 741 742 public void setBufferSize(int size) 743 { 744 _httpResponse.setBufferSize(size); 745 } 746 747 public int getBufferSize() 748 { 749 return _httpResponse.getBufferSize(); 750 } 751 752 public void flushBuffer() 753 throws IOException 754 { 755 _httpResponse.flushBuffer(); 756 } 757 758 public void resetBuffer() 759 { 760 _httpResponse.resetBuffer(); 761 } 762 763 public void reset() 764 { 765 _httpResponse.reset(); 766 } 767 768 public boolean isCommitted() 769 { 770 return _httpResponse.isCommitted(); 771 } 772 773 public OutputStream getOutputStream() 774 throws IOException 775 { 776 return _httpResponse.getOutputStream(); 777 } 778 779 public String getCharacterEncoding() 780 { 781 return _httpResponse.getCharacterEncoding(); 782 } 783 784 public void setCharacterEncoding(String enc) 785 throws UnsupportedEncodingException 786 { 787 _httpResponse.setCharacterEncoding(enc); 788 } 789 790 public PrintWriter getWriter() 791 throws IOException 792 { 793 return _httpResponse.getWriter(); 794 } 795 796 } 797 798 | Popular Tags |