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