1 package org.appfuse.webapp.filter; 2 3 import java.io.IOException ; 4 import java.io.OutputStreamWriter ; 5 import java.io.PrintWriter ; 6 7 import javax.servlet.ServletOutputStream ; 8 import javax.servlet.http.HttpServletResponse ; 9 import javax.servlet.http.HttpServletResponseWrapper ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 15 20 public class GZIPResponseWrapper extends HttpServletResponseWrapper { 21 private transient final Log log = LogFactory.getLog(GZIPResponseWrapper.class); 22 protected HttpServletResponse origResponse = null; 23 protected ServletOutputStream stream = null; 24 protected PrintWriter writer = null; 25 protected int error = 0; 26 27 public GZIPResponseWrapper(HttpServletResponse response) { 28 super(response); 29 origResponse = response; 30 } 31 32 public ServletOutputStream createOutputStream() throws IOException { 33 return (new GZIPResponseStream(origResponse)); 34 } 35 36 public void finishResponse() { 37 try { 38 if (writer != null) { 39 writer.close(); 40 } else { 41 if (stream != null) { 42 stream.close(); 43 } 44 } 45 } catch (IOException e) { 46 } 47 } 48 49 public void flushBuffer() throws IOException { 50 if (stream != null) { 51 stream.flush(); 52 } 53 } 54 55 public ServletOutputStream getOutputStream() throws IOException { 56 if (writer != null) { 57 throw new IllegalStateException ("getWriter() has already been called!"); 58 } 59 60 if (stream == null) { 61 stream = createOutputStream(); 62 } 63 64 return (stream); 65 } 66 67 public PrintWriter getWriter() throws IOException { 68 if (this.origResponse != null && this.origResponse.isCommitted()) { 71 return super.getWriter(); 72 } 73 74 if (writer != null) { 75 return (writer); 76 } 77 78 if (stream != null) { 79 throw new IllegalStateException ("getOutputStream() has already been called!"); 80 } 81 82 stream = createOutputStream(); 83 writer = 84 new PrintWriter (new OutputStreamWriter (stream, 85 origResponse.getCharacterEncoding())); 86 87 return (writer); 88 } 89 90 public void setContentLength(int length) { 91 } 92 93 96 public void sendError(int error, String message) throws IOException { 97 super.sendError(error, message); 98 this.error = error; 99 100 if (log.isDebugEnabled()) { 101 log.debug("sending error: " + error + " [" + message + "]"); 102 } 103 } 104 } 105 | Popular Tags |