1 2 24 25 package com.lutris.appserver.server.httpPresentation.servlet; 26 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 import java.io.StringWriter ; 30 import java.util.Enumeration ; 31 32 import javax.servlet.http.Cookie ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.enhydra.util.jivan.JivanSimpleXMLObjectImpl; 36 import org.enhydra.xml.dom.DOMStats; 37 import org.enhydra.xml.io.DOMFormatter; 38 import org.enhydra.xml.io.Encodings; 39 import org.enhydra.xml.io.OutputOptions; 40 import org.enhydra.xml.io.URLRewriter; 41 import org.enhydra.xml.xmlc.XMLObject; 42 import org.w3c.dom.html.HTMLDocument; 43 44 import com.lutris.appserver.server.StandardAppUtil; 45 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 46 import com.lutris.appserver.server.httpPresentation.HttpPresentationIOException; 47 import com.lutris.appserver.server.httpPresentation.HttpPresentationOutputStream; 48 import com.lutris.appserver.server.httpPresentation.HttpPresentationResponse; 49 import com.lutris.appserver.server.session.SessionException; 50 import com.lutris.appserver.server.session.SessionManager; 51 import com.lutris.util.StringEnum; 52 53 57 public class ServletHttpPresentationResponse 58 implements HttpPresentationResponse { 60 63 private HttpServletResponse response; 64 private HttpPresentationOutputStream outputStream = null; 66 69 private static final String defaultEncoding = System.getProperty("file.encoding"); 70 71 74 private String encoding = null; 75 76 79 private String sessionKey = null; 80 81 84 private SessionManager sessionManager = null; 85 86 89 private boolean sessionIdCookie = true; 90 91 94 private boolean sessionIdUrl = true; 95 96 99 private PrintWriter domStatsLogWriter; 100 101 106 protected ServletHttpPresentationResponse(HttpServletResponse response, 107 PrintWriter domStatsLogWriter) { 108 this.response = response; 109 this.domStatsLogWriter = domStatsLogWriter; 110 } 111 112 115 public HttpServletResponse getHttpServletResponse() { 116 return this.response; 117 } 118 119 124 public void setContentLength(int len) 125 throws HttpPresentationException { 126 127 response.setContentLength(len); 128 } 129 130 131 136 public void setContentType(String type) 137 throws HttpPresentationException { 138 139 response.setContentType(type); 140 } 141 142 143 146 public HttpPresentationOutputStream getOutputStream() 147 throws HttpPresentationException { 148 149 if (outputStream == null) { 150 try { 151 outputStream = new ServletHttpPresentationOutputStream 152 (this, response.getOutputStream()); 153 } catch (IOException except) { 154 throw new HttpPresentationException(new HttpPresentationIOException(except)); 158 } 159 } 160 return outputStream; 161 } 162 163 164 170 public void addCookie(Cookie cookie) 171 throws HttpPresentationException { 172 response.addCookie(cookie); 173 } 174 175 176 181 public boolean containsHeader(String name) 182 throws HttpPresentationException { 183 184 return response.containsHeader(name); 185 } 186 187 188 194 public void setStatus(int sc, String sm) 195 throws HttpPresentationException { 196 197 response.setStatus(sc, sm); 198 } 199 200 201 206 public void setStatus(int sc) 207 throws HttpPresentationException { 208 209 response.setStatus(sc); 210 } 211 212 213 223 public void setHeader(String name, String value) 224 throws HttpPresentationException { 225 226 response.setHeader(name, value); 227 } 228 229 230 240 public void setIntHeader(String name, int value) 241 throws HttpPresentationException { 242 243 response.setIntHeader(name, value); 244 } 245 246 247 258 public void setDateHeader(String name, long date) 259 throws HttpPresentationException { 260 261 response.setDateHeader(name, date); 262 } 263 264 265 272 public void sendError(int sc, String msg) 273 throws HttpPresentationException { 274 275 try { 276 response.sendError(sc, msg); 277 } catch (IOException except) { 278 throw new HttpPresentationException(new HttpPresentationIOException(except)); 282 } 283 } 284 285 291 public void sendError(int sc) 292 throws HttpPresentationException { 293 294 try { 295 response.sendError(sc); 296 } catch (IOException except) { 297 throw new HttpPresentationException(new HttpPresentationIOException(except)); 301 } 302 } 303 304 308 public void flush() throws HttpPresentationException { 309 310 try { 311 response.flushBuffer(); 315 } catch (IOException except) { 316 throw new HttpPresentationException(new HttpPresentationIOException(except)); 320 } 321 } 322 323 330 public String encodeUrl(String url) { 331 return response.encodeUrl(url); 332 } 333 334 345 public String encodeRedirectUrl(String url) { 346 return response.encodeUrl(url); 347 } 348 349 350 351 352 355 356 363 public int getContentLength() { 364 return -1; 367 } 368 369 370 377 public String getContentType() { 378 return null; 381 } 382 383 384 391 public int getStatusCode() { 392 return -1; 395 } 396 397 398 405 public String getStatusMessage() { 406 return null; 409 } 410 411 412 420 public Cookie [] getCookies() { 421 return new Cookie [0]; 425 } 426 427 428 438 public String getHeader(String name) { 439 return ""; 443 } 444 445 446 454 public Enumeration getHeaderNames() { 455 return new StringEnum(new String [0]); 459 } 460 461 462 470 public int getTotalBytes() { 471 return -1; 475 } 476 477 482 public String getResponseData() { 483 return null; 484 } 485 486 491 public void setEncoding(String enc) { 492 encoding = enc; 493 } 494 495 498 public String getEncoding() { 499 return encoding; 500 } 501 502 508 public void setSessionKey(String sessionKey) { 509 this.sessionKey = sessionKey; 510 } 511 512 515 public void setSessionManager(SessionManager sessionManager) { 516 this.sessionManager = sessionManager; 517 } 518 519 524 public boolean isSessionIdCookieRequired() 525 throws HttpPresentationException { 526 return sessionIdCookie; 527 } 528 529 534 public void setSessionIdCookieRequired(boolean sessionIdCookie) 535 throws HttpPresentationException { 536 this.sessionIdCookie = sessionIdCookie; 537 } 538 539 545 public boolean isSessionIdEncodeUrlRequired() 546 throws HttpPresentationException { 547 return sessionIdUrl; 548 } 549 554 public void setSessionIdEncodeUrlRequired(boolean sessionIdUrl) 555 throws HttpPresentationException { 556 this.sessionIdUrl = sessionIdUrl; 557 } 558 559 563 private boolean shouldEncodeSessionKey() throws HttpPresentationException { 564 try { 565 return (sessionIdUrl && (sessionManager != null) && (sessionKey != null) 566 && sessionManager.sessionExists(sessionKey) 567 && !(sessionManager).getEncodeUrlState().equalsIgnoreCase(SessionManager.ENCODE_URL_NEVER)); 568 574 } catch (SessionException except) { 575 throw new HttpPresentationException(except); 576 } 577 } 578 579 585 private void setupEncoding(XMLObject document, 586 OutputOptions options) 587 throws HttpPresentationException { 588 589 if (options.getEncoding() == null) { 591 String outputEncoding = encoding; if (outputEncoding == null) { 593 outputEncoding = document.getEncoding(); 595 } 596 if (outputEncoding == null) { 597 outputEncoding = defaultEncoding; 598 } 599 options.setEncoding(outputEncoding); 600 } 601 } 602 603 615 public OutputOptions createOutputOptions(XMLObject document) 616 throws HttpPresentationException { 617 618 OutputOptions options = DOMFormatter.getDefaultOutputOptions(document); 619 620 setupEncoding(document, options); 621 622 options.setMIMEType(document.getMIMEType()); 624 625 boolean encodeSessionKey = shouldEncodeSessionKey(); 627 if (encodeSessionKey) { 628 options.setURLRewriter(new URLRewriter() { 629 public String rewriteURL(String urlAttrValue) { 630 if (StandardAppUtil.pointsToPO(urlAttrValue)) { 631 return StandardAppUtil.encodeUrl(urlAttrValue, sessionKey); 632 } else { 633 return urlAttrValue; 634 } 635 } 636 }); 637 } 638 return options; 639 } 640 641 644 private void outputDocumentBytes(byte[] docBytes, 645 String mimeEncoding, 646 String mimeType) 647 throws HttpPresentationException, IOException { 648 649 setContentLength(docBytes.length); 651 if (mimeType != null) { 652 if (mimeEncoding != null) { 653 setContentType(mimeType + "; charset=" + mimeEncoding); 654 } else { 655 setContentType(mimeType); 656 } 657 } 658 659 if (getHeader("Cache-Control") == null) { 661 setHeader("Cache-Control", "no-cache"); 662 } 663 if (getHeader("Expires") == null) { 664 setHeader("Expires", "0"); 665 } 666 667 getOutputStream().write(docBytes); 669 getOutputStream().flush(); 670 } 671 672 677 private void outputDocument(XMLObject document, 678 OutputOptions outputOptions) 679 throws HttpPresentationException, IOException { 680 681 if(document instanceof JivanSimpleXMLObjectImpl) { outputDocumentBytes(((JivanSimpleXMLObjectImpl)document).toByteDocument(), 685 outputOptions.getMIMEEncoding(), 686 outputOptions.getMIMEType()); 687 } 688 else { 689 DOMFormatter formatter = new DOMFormatter(outputOptions); 690 outputDocumentBytes(formatter.toBytes(document), 691 outputOptions.getMIMEEncoding(), 692 outputOptions.getMIMEType()); 693 } 694 if (domStatsLogWriter != null) { 696 StringWriter buf = new StringWriter (2048); 698 PrintWriter writer = new PrintWriter (buf); 699 DOMStats.printStats("Write " + document.getClass().getName(), 700 document, 0, writer); 701 writer.flush(); 702 domStatsLogWriter.println(buf.toString()); 703 } 704 } 705 706 715 public void writeDOM(OutputOptions outputOptions, 716 XMLObject document) throws HttpPresentationException { 717 try { 718 OutputOptions options = new OutputOptions(outputOptions); 720 setupEncoding(document, options); 721 outputDocument(document, options); 722 } catch (IOException except) { 723 throw new HttpPresentationException(except); 724 } 725 } 726 727 734 public void writeDOM(XMLObject document) throws HttpPresentationException { 735 736 OutputOptions outputOptions = createOutputOptions(document); 737 746 outputOptions.setEnableXHTMLCompatibility(true); 747 try { 748 outputDocument(document, outputOptions); 749 } catch (IOException except) { 750 throw new HttpPresentationException(except); 751 } 752 } 753 754 758 public void writeHTML(String html) throws HttpPresentationException { 759 byte bytes[]; 760 761 String mimeEncoding = null; 763 if (encoding != null) { 764 mimeEncoding = Encodings.getEncodings().getMIMEPreferred(encoding); 765 if (mimeEncoding == null) { 766 mimeEncoding = encoding; 767 } 768 } 769 770 try { 771 if (encoding != null) { 772 bytes = html.getBytes(encoding); 773 } else { 774 bytes = html.getBytes(); 775 } 776 outputDocumentBytes(bytes, mimeEncoding, "text/html"); 777 } catch (IOException except) { 778 throw new HttpPresentationException(except); 779 } 780 } 781 782 790 public void writeHTML(HTMLDocument doc) throws HttpPresentationException { 791 writeDOM((XMLObject)doc); 792 } 793 } 794 | Popular Tags |