1 48 49 50 package com.caucho.portal.generic; 51 52 import javax.portlet.PortletException; 53 import javax.portlet.PortletRequestDispatcher; 54 import javax.portlet.RenderRequest; 55 import javax.portlet.RenderResponse; 56 import javax.servlet.RequestDispatcher ; 57 import javax.servlet.ServletContext ; 58 import javax.servlet.ServletException ; 59 import javax.servlet.ServletInputStream ; 60 import javax.servlet.ServletOutputStream ; 61 import javax.servlet.http.Cookie ; 62 import javax.servlet.http.HttpServletRequest ; 63 import javax.servlet.http.HttpServletResponse ; 64 import javax.servlet.http.HttpSession ; 65 import java.io.BufferedReader ; 66 import java.io.IOException ; 67 import java.io.OutputStream ; 68 import java.io.PrintWriter ; 69 import java.io.UnsupportedEncodingException ; 70 import java.security.Principal ; 71 import java.util.Collections ; 72 import java.util.Enumeration ; 73 import java.util.HashMap ; 74 import java.util.Locale ; 75 import java.util.Map ; 76 import java.util.logging.Logger ; 77 78 83 public class HttpPortletRequestDispatcher 84 implements PortletRequestDispatcher 85 { 86 static protected final Logger log = 87 Logger.getLogger(HttpPortletRequestDispatcher.class.getName()); 88 89 92 final static public String HTTP_SERVLET_REQUEST 93 = "com.caucho.portal.generic.HttpServletRequest"; 94 95 98 final static public String HTTP_SERVLET_RESPONSE 99 = "com.caucho.portal.generic.HttpServletResponse"; 100 101 102 private RequestDispatcher _dispatcher; 103 private Map <String , String []> _parameterMap; 104 105 private HttpPortletRequestWrapper _wrappedRequest 106 = new HttpPortletRequestWrapper(); 107 108 private HttpPortletResponseWrapper _wrappedResponse 109 = new HttpPortletResponseWrapper(); 110 111 public HttpPortletRequestDispatcher() 112 { 113 } 114 115 118 public boolean startWithPath(ServletContext servletContext, String path) 119 { 120 _dispatcher = servletContext.getRequestDispatcher(path); 121 122 if (_dispatcher == null) 123 return false; 124 125 int paramIndex = path.indexOf('?'); 126 127 if (paramIndex != -1) { 128 Map <String , String []> parameterMap = new HashMap <String , String []>(); 129 path = HttpUtil.extractParameters(parameterMap, path, paramIndex); 130 _parameterMap = parameterMap; 131 } 132 133 134 return true; 135 } 136 137 140 public boolean startWithName(ServletContext servletContext, String name) 141 { 142 _dispatcher = servletContext.getNamedDispatcher(name); 143 144 return _dispatcher != null; 145 } 146 147 public void finish() 148 { 149 _dispatcher = null; 150 _parameterMap = null; 151 } 152 153 public void include(RenderRequest renderRequest, 154 RenderResponse renderResponse) 155 throws PortletException, IOException 156 { 157 HttpServletRequest httpRequest = 158 (HttpServletRequest ) renderRequest.getAttribute(HTTP_SERVLET_REQUEST); 159 160 if (httpRequest == null) 161 throw new PortletException( 162 "HttpServletRequest not found in request attribute " 163 + HTTP_SERVLET_REQUEST); 164 165 HttpServletResponse httpResponse = 166 (HttpServletResponse ) renderRequest.getAttribute(HTTP_SERVLET_RESPONSE); 167 168 if (httpResponse == null) 169 throw new PortletException( 170 "HttpServletResponse not found in request attribute " 171 + HTTP_SERVLET_RESPONSE); 172 173 _wrappedRequest.start(httpRequest, renderRequest, _parameterMap); 174 _wrappedResponse.start(httpResponse, renderResponse); 175 176 try { 177 _dispatcher.include(_wrappedRequest, _wrappedResponse); 178 } 179 catch (ServletException ex) { 180 throw new PortletException(ex); 181 } 182 finally { 183 _wrappedResponse.finish(); 184 _wrappedRequest.finish(); 185 } 186 } 187 188 192 static protected class HttpPortletRequestWrapper 193 implements HttpServletRequest 194 { 195 private RenderRequest _renderRequest; 196 private HttpServletRequest _httpRequest; 197 private Map <String , String []> _parameterMap; 198 199 private Map <String , String []> _fullParameterMap; 200 201 public HttpPortletRequestWrapper() 202 { 203 } 204 205 public void start(HttpServletRequest httpRequest, 206 RenderRequest renderRequest, 207 Map <String , String []> parameterMap) 208 209 { 210 _httpRequest = httpRequest; 211 _renderRequest = renderRequest; 212 _parameterMap = parameterMap; 213 } 214 215 public void finish() 216 { 217 _renderRequest = null; 218 _httpRequest = null; 219 _parameterMap = null; 220 _fullParameterMap = null; 221 } 222 223 public String getParameter(String name) 224 { 225 String [] values = getParameterValues(name); 226 227 return values == null || values.length == 0 ? null : values[0]; 228 } 229 230 public String []getParameterValues(String name) 231 { 232 if (_fullParameterMap != null) 233 return _fullParameterMap.get(name); 234 235 String [] values = null; 236 237 if (_parameterMap != null) 238 values = _parameterMap.get(name); 239 240 if (values == null) 241 values = _renderRequest.getParameterValues(name); 242 243 return values; 244 } 245 246 public Map getParameterMap() 247 { 248 if (_fullParameterMap != null) 249 return _fullParameterMap; 250 251 if (_parameterMap == null) { 252 _fullParameterMap = _renderRequest.getParameterMap(); 253 } 254 else { 255 _fullParameterMap = new HashMap <String , String []>(); 256 _fullParameterMap.putAll(_renderRequest.getParameterMap()); 257 _fullParameterMap.putAll(_parameterMap); 258 } 259 260 return _fullParameterMap; 261 } 262 263 public Enumeration getParameterNames() 264 { 265 return Collections.enumeration(getParameterMap().keySet()); 266 } 267 268 public String getScheme() 269 { 270 return _renderRequest.getScheme(); 271 } 272 public String getServerName() 273 { 274 return _renderRequest.getServerName(); 275 } 276 public int getServerPort() 277 { 278 return _renderRequest.getServerPort(); 279 } 280 public Object getAttribute(String name) 281 { 282 return _renderRequest.getAttribute(name); 283 } 284 public void setAttribute(String name, Object o) 285 { 286 _renderRequest.setAttribute(name, o); 287 } 288 public Enumeration getAttributeNames() 289 { 290 return _renderRequest.getAttributeNames(); 291 } 292 public void removeAttribute(String name) 293 { 294 _renderRequest.removeAttribute(name); 295 } 296 public Locale getLocale() 297 { 298 return _renderRequest.getLocale(); 299 } 300 public Enumeration getLocales() 301 { 302 return _renderRequest.getLocales(); 303 } 304 public boolean isSecure() 305 { 306 return _renderRequest.isSecure(); 307 } 308 public String getAuthType() 309 { 310 return _renderRequest.getAuthType(); 311 } 312 public String getRequestedSessionId() 313 { 314 return _renderRequest.getRequestedSessionId(); 315 } 316 public boolean isRequestedSessionIdValid() 317 { 318 return _renderRequest.isRequestedSessionIdValid(); 319 } 320 public String getRemoteUser() 321 { 322 return _renderRequest.getRemoteUser(); 323 } 324 public boolean isUserInRole(String role) 325 { 326 return _renderRequest.isUserInRole(role); 327 } 328 public Principal getUserPrincipal() 329 { 330 return _renderRequest.getUserPrincipal(); 331 } 332 333 public String getHeader(String name) 334 { 335 return _httpRequest.getHeader(name); 336 } 337 public Enumeration getHeaders(String name) 338 { 339 return _httpRequest.getHeaders(name); 340 } 341 public Enumeration getHeaderNames() 342 { 343 return _httpRequest.getHeaderNames(); 344 } 345 public int getIntHeader(String name) 346 { 347 return _httpRequest.getIntHeader(name); 348 } 349 public long getDateHeader(String name) 350 { 351 return _httpRequest.getDateHeader(name); 352 } 353 public Cookie []getCookies() 354 { 355 return _httpRequest.getCookies(); 356 } 357 358 public String getProtocol() 359 { 360 return null; 361 } 362 public String getRealPath(String uri) 363 { 364 return null; 365 } 366 public StringBuffer getRequestURL() 367 { 368 return null; 369 } 370 public String getRemoteAddr() 371 { 372 return null; 373 } 374 public String getRemoteHost() 375 { 376 return null; 377 } 378 public String getCharacterEncoding() 379 { 380 return null; 381 } 382 public void setCharacterEncoding(String encoding) 383 throws UnsupportedEncodingException 384 { 385 } 386 public ServletInputStream getInputStream() 387 throws IOException 388 { 389 return null; 390 } 391 public BufferedReader getReader() 392 throws IOException , IllegalStateException 393 { 394 return null; 395 } 396 public int getContentLength() 397 { 398 return 0; 399 } 400 401 public String getMethod() 402 { 403 return "GET"; 404 } 405 406 public int getRemotePort() 407 { 408 return _httpRequest.getRemotePort(); 409 } 410 public String getLocalAddr() 411 { 412 return _httpRequest.getLocalAddr(); 413 } 414 public String getLocalName() 415 { 416 return _httpRequest.getLocalName(); 417 } 418 public int getLocalPort() 419 { 420 return _httpRequest.getLocalPort(); 421 } 422 public String getContentType() 423 { 424 return _httpRequest.getContentType(); 425 } 426 public RequestDispatcher getRequestDispatcher(String uri) 427 { 428 return _httpRequest.getRequestDispatcher(uri); 429 } 430 public String getRequestURI() 431 { 432 return _httpRequest.getRequestURI(); 433 } 434 public String getContextPath() 435 { 436 return _httpRequest.getContextPath(); 437 } 438 public String getServletPath() 439 { 440 return _httpRequest.getServletPath(); 441 } 442 public String getPathInfo() 443 { 444 return _httpRequest.getPathInfo(); 445 } 446 public String getPathTranslated() 447 { 448 return _httpRequest.getPathTranslated(); 449 } 450 public String getQueryString() 451 { 452 return _httpRequest.getQueryString(); 453 } 454 public HttpSession getSession(boolean create) 455 { 456 return _httpRequest.getSession(create); 457 } 458 public HttpSession getSession() 459 { 460 return getSession(true); 461 } 462 public boolean isRequestedSessionIdFromCookie() 463 { 464 return _httpRequest.isRequestedSessionIdFromCookie(); 465 } 466 public boolean isRequestedSessionIdFromURL() 467 { 468 return _httpRequest.isRequestedSessionIdFromURL(); 469 } 470 473 public boolean isRequestedSessionIdFromUrl() 474 { 475 return _httpRequest.isRequestedSessionIdFromUrl(); 476 } 477 } 478 479 static protected class HttpPortletResponseWrapper 480 implements HttpServletResponse 481 { 482 private HttpServletResponse _httpResponse; 483 private RenderResponse _renderResponse; 484 private ServletOutputStreamWrapper _servletOutputStream; 485 486 public HttpPortletResponseWrapper() 487 { 488 } 489 490 public void start(HttpServletResponse httpResponse, 491 RenderResponse renderResponse) 492 493 { 494 _httpResponse = httpResponse; 495 _renderResponse = renderResponse; 496 } 497 498 public void finish() 499 { 500 _servletOutputStream = null; 501 _renderResponse = null; 502 _httpResponse = null; 503 } 504 505 public void setBufferSize(int size) 506 { 507 _renderResponse.setBufferSize(size); 508 } 509 510 public int getBufferSize() 511 { 512 return _renderResponse.getBufferSize(); 513 } 514 515 public void flushBuffer() 516 throws IOException 517 { 518 _renderResponse.flushBuffer(); 519 } 520 521 public void resetBuffer() 522 { 523 _renderResponse.resetBuffer(); 524 } 525 526 public void reset() 527 { 528 _renderResponse.reset(); 529 } 530 531 public boolean isCommitted() 532 { 533 return _renderResponse.isCommitted(); 534 } 535 536 public ServletOutputStream getOutputStream() 537 throws IOException 538 { 539 if (_servletOutputStream == null) { 540 OutputStream portletOutputStream 541 = _renderResponse.getPortletOutputStream(); 542 543 _servletOutputStream 544 = new ServletOutputStreamWrapper(portletOutputStream); 545 } 546 547 return _servletOutputStream; 548 } 549 550 public String getCharacterEncoding() 551 { 552 return _renderResponse.getCharacterEncoding(); 553 } 554 555 public String getContentType() 556 { 557 return _renderResponse.getContentType(); 558 } 559 public void setCharacterEncoding(String enc) 560 { 561 } 562 563 public PrintWriter getWriter() 564 throws IOException 565 { 566 return _renderResponse.getWriter(); 567 } 568 569 public String encodeURL(String path) 570 { 571 return _renderResponse.encodeURL(path); 572 } 573 574 public String encodeUrl(String path) 575 { 576 return _renderResponse.encodeURL(path); 577 } 578 579 public Locale getLocale() 580 { 581 return _renderResponse.getLocale(); 582 } 583 584 public String encodeRedirectURL(String url) 585 { 586 return null; 587 } 588 public String encodeRedirectUrl(String url) 589 { 590 return null; 591 } 592 public void setContentType(String type) 593 { 594 } 595 public void setContentLength(int len) 596 { 597 } 598 public void setLocale(Locale locale) 599 { 600 } 601 public void addCookie(Cookie cookie) 602 { 603 } 604 public void sendError(int sc) 605 throws IOException 606 { 607 } 608 public void sendError(int sc, String msg) 609 throws IOException 610 { 611 } 612 public void sendRedirect(String location) 613 throws IOException 614 { 615 } 616 public void setDateHeader(String name, long date) 617 { 618 } 619 public void addDateHeader(String name, long date) 620 { 621 } 622 public void setHeader(String name, String value) 623 { 624 } 625 public void addHeader(String name, String value) 626 { 627 } 628 public void setIntHeader(String name, int value) 629 { 630 } 631 public void addIntHeader(String name, int value) 632 { 633 } 634 public boolean containsHeader(String name) 635 { 636 return false; 637 } 638 public void setStatus(int sc) 639 { 640 } 641 public void setStatus(int sc, String msg) 642 { 643 } 644 } 645 646 static final protected class ServletOutputStreamWrapper 647 extends ServletOutputStream 648 { 649 private OutputStream _out; 650 651 public ServletOutputStreamWrapper(OutputStream out) 652 { 653 _out = out; 654 } 655 656 public void write(int b) 657 throws IOException 658 { 659 _out.write(b); 660 } 661 662 public void write(byte b[], int off, int len) 663 throws IOException 664 { 665 _out.write(b, off, len); 666 } 667 668 public void write(byte b[]) 669 throws IOException 670 { 671 _out.write(b); 672 } 673 674 public void flush() 675 throws IOException 676 { 677 _out.flush(); 678 } 679 680 public void close() 681 throws IOException 682 { 683 _out.close(); 684 } 685 } 686 } 687 688 689 | Popular Tags |