1 9 package com.sslexplorer.core.filters; 10 11 import java.io.IOException ; 12 import java.io.OutputStreamWriter ; 13 import java.io.PrintWriter ; 14 15 import javax.servlet.ServletOutputStream ; 16 import javax.servlet.http.HttpServletResponse ; 17 import javax.servlet.http.HttpServletResponseWrapper ; 18 19 public class GZIPResponseWrapper extends HttpServletResponseWrapper { 20 protected HttpServletResponse origResponse = null; 21 protected ServletOutputStream stream = null; 22 protected PrintWriter writer = null; 23 protected boolean compress = true; 24 25 public GZIPResponseWrapper(HttpServletResponse response) { 26 super(response); 27 origResponse = response; 28 } 29 30 public ServletOutputStream createOutputStream() throws IOException { 31 return compress ? (new GZIPResponseStream(origResponse)) : origResponse.getOutputStream(); 32 } 33 34 public void setCompress(boolean compress) throws IOException { 35 if (stream != null) { 36 throw new IOException ("Cannot set compress after stream has been set."); 37 } 38 this.compress = compress; 39 } 40 41 public void finishResponse() { 42 try { 43 if (writer != null) { 44 writer.close(); 45 } else { 46 if (stream != null) { 47 stream.close(); 48 } 49 } 50 } catch (IOException e) {} 51 } 52 53 public void flushBuffer() throws IOException { 54 stream.flush(); 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 return (stream); 65 } 66 67 public PrintWriter getWriter() throws IOException { 68 if (writer != null) { 69 return (writer); 70 } 71 72 if (stream != null) { 73 throw new IllegalStateException ("getOutputStream() has already been called!"); 74 } 75 76 stream = createOutputStream(); 77 writer = new PrintWriter (new OutputStreamWriter (stream, System.getProperty("sslexplorer.encoding", "UTF-8"))); 78 return (writer); 79 } 80 81 public void setContentLength(int length) {} 82 } 83 | Popular Tags |