1 5 6 13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers; 14 15 16 import javax.servlet.ServletOutputStream ; 17 import javax.servlet.http.HttpServletResponse ; 18 import javax.servlet.http.HttpServletResponseWrapper ; 19 import org.exoplatform.services.portletcontainer.helper.URLEncoder; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.CharArrayWriter ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 25 public class CustomResponseWrapper extends HttpServletResponseWrapper { 26 27 private PrintWriter tmpWriter; 28 private ByteArrayOutputStream output; 29 private ByteArrayServletOutputStream servletOutput; 30 private URLEncoder urlEncoder_ ; 31 private CharArrayWriter charArrayWriter; 32 private boolean writerAlreadyCalled; 33 private boolean outputStreamAlreadyCalled; 34 35 36 public CustomResponseWrapper(HttpServletResponse httpServletResponse) { 37 super(httpServletResponse); 38 } 39 40 public void fillResponseWrapper(HttpServletResponse httpServletResponse) { 41 super.setResponse(httpServletResponse); 42 output = new ByteArrayOutputStream (); 43 servletOutput = new ByteArrayServletOutputStream(output); 44 45 charArrayWriter = new CharArrayWriter (); 46 tmpWriter = new PrintWriter (charArrayWriter); 47 writerAlreadyCalled = false; 48 outputStreamAlreadyCalled = false; 49 } 50 51 public void emptyResponseWrapper() { 52 output = null; 53 tmpWriter = null; 54 servletOutput = null; 55 writerAlreadyCalled = false; 56 outputStreamAlreadyCalled = false; 57 } 58 59 public void finalize() throws Throwable { 60 super.finalize(); 61 servletOutput.close(); 62 output.close(); 63 tmpWriter.close(); 64 } 65 66 public char[] getPortletContent() { 67 return charArrayWriter.toCharArray(); 68 } 69 70 public PrintWriter getWriter() throws IOException { 71 if(outputStreamAlreadyCalled) 72 throw new IllegalStateException ("the PrintWriter object has already been called"); 73 writerAlreadyCalled = true; 74 return tmpWriter; 75 } 76 77 public ServletOutputStream getOutputStream() throws IOException { 78 if(writerAlreadyCalled) 79 throw new IllegalStateException ("the output stream has already been called"); 80 outputStreamAlreadyCalled = true; 81 return servletOutput; 82 } 83 84 public byte[] toByteArray() { 85 return output.toByteArray(); 86 } 87 88 public void flushBuffer() throws IOException { 89 tmpWriter.flush(); 90 servletOutput.flush(); 91 } 92 93 public void reset() { 94 output.reset(); 95 } 96 97 public void close() throws IOException { 98 tmpWriter.close(); 99 } 100 101 public int getBufferSize() { 102 return 0; 103 } 104 105 public String encodeURL(String url) { 106 if (urlEncoder_ == null) return super.encodeURL(url) ; 107 return urlEncoder_.encodeURL(url) ; 108 } 109 110 public void setURLEncoder(URLEncoder encoder) { 111 urlEncoder_ = encoder ; 112 } 113 114 private static class ByteArrayServletOutputStream extends ServletOutputStream { 115 ByteArrayOutputStream baos; 116 public ByteArrayServletOutputStream(ByteArrayOutputStream baos) { 117 this.baos = baos; 118 } 119 public void write(int i) throws IOException { 120 baos.write(i); 121 } 122 } 123 } | Popular Tags |