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