1 3 package jodd.servlet.filter; 4 5 import java.io.IOException ; 6 import java.io.OutputStreamWriter ; 7 import java.io.PrintWriter ; 8 9 import javax.servlet.ServletOutputStream ; 10 import javax.servlet.http.HttpServletResponse ; 11 import javax.servlet.http.HttpServletResponseWrapper ; 12 13 17 18 public class GzipResponseWrapper extends HttpServletResponseWrapper { 19 20 26 public GzipResponseWrapper(HttpServletResponse response) { 27 super(response); 28 origResponse = response; 29 } 30 31 32 33 36 protected HttpServletResponse origResponse = null; 37 38 42 protected ServletOutputStream stream = null; 43 44 48 protected PrintWriter writer = null; 49 50 53 protected int threshold = 0; 54 55 58 protected String contentType = null; 59 60 62 63 68 public void setContentType(String contentType) { 69 this.contentType = contentType; 70 origResponse.setContentType(contentType); 71 } 72 73 78 public void setCompressionThreshold(int threshold) { 79 this.threshold = threshold; 80 } 81 82 90 public ServletOutputStream createOutputStream() throws IOException { 91 GzipResponseStream gzstream = new GzipResponseStream(origResponse); 92 gzstream.setBuffer(threshold); 93 return gzstream; 94 } 95 96 97 100 public void finishResponse() { 101 try { 102 if (writer != null) { 103 writer.close(); 104 } else { 105 if (stream != null) 106 stream.close(); 107 } 108 } catch (IOException e) { 109 } 111 } 112 113 115 116 122 public void flushBuffer() throws IOException { 123 if (stream != null) { 124 stream.flush(); 125 } 126 } 127 128 138 public ServletOutputStream getOutputStream() throws IOException { 139 140 if (writer != null) { 141 throw new IllegalStateException ("getWriter() has already been called for this response"); 142 } 143 if (stream == null) { 144 stream = createOutputStream(); 145 } 146 return(stream); 147 148 } 149 150 160 public PrintWriter getWriter() throws IOException { 161 162 if (writer != null) { 163 return writer; 164 } 165 166 if (stream != null) { 167 throw new IllegalStateException ("getOutputStream() has already been called for this response"); 168 } 169 170 stream = createOutputStream(); 171 172 String charEnc = origResponse.getCharacterEncoding(); 173 if (charEnc != null) { 174 writer = new PrintWriter (new OutputStreamWriter (stream, charEnc)); 175 } else { 176 writer = new PrintWriter (stream); 177 } 178 return(writer); 179 } 180 181 182 public void setContentLength(int length) { 183 } 184 185 } 186 | Popular Tags |