| .. | .. |
|---|
| 1 | +package net.curisit.securis.utils; |
|---|
| 2 | + |
|---|
| 3 | +import java.io.IOException; |
|---|
| 4 | +import java.io.OutputStreamWriter; |
|---|
| 5 | +import java.io.PrintWriter; |
|---|
| 6 | +import java.util.zip.GZIPOutputStream; |
|---|
| 7 | + |
|---|
| 8 | +import javax.servlet.ServletOutputStream; |
|---|
| 9 | +import javax.servlet.WriteListener; |
|---|
| 10 | +import javax.servlet.http.HttpServletResponse; |
|---|
| 11 | +import javax.servlet.http.HttpServletResponseWrapper; |
|---|
| 12 | + |
|---|
| 13 | +public class GZipServletResponseWrapper extends HttpServletResponseWrapper { |
|---|
| 14 | + |
|---|
| 15 | + private GZIPServletOutputStream gzipOutputStream = null; |
|---|
| 16 | + private PrintWriter printWriter = null; |
|---|
| 17 | + |
|---|
| 18 | + public GZipServletResponseWrapper(HttpServletResponse response) throws IOException { |
|---|
| 19 | + super(response); |
|---|
| 20 | + } |
|---|
| 21 | + |
|---|
| 22 | + public void close() throws IOException { |
|---|
| 23 | + |
|---|
| 24 | + //PrintWriter.close does not throw exceptions. |
|---|
| 25 | + //Hence no try-catch block. |
|---|
| 26 | + if (this.printWriter != null) { |
|---|
| 27 | + this.printWriter.close(); |
|---|
| 28 | + } |
|---|
| 29 | + |
|---|
| 30 | + if (this.gzipOutputStream != null) { |
|---|
| 31 | + this.gzipOutputStream.close(); |
|---|
| 32 | + } |
|---|
| 33 | + } |
|---|
| 34 | + |
|---|
| 35 | + /** |
|---|
| 36 | + * Flush OutputStream or PrintWriter |
|---|
| 37 | + * |
|---|
| 38 | + * @throws IOException |
|---|
| 39 | + */ |
|---|
| 40 | + |
|---|
| 41 | + @Override |
|---|
| 42 | + public void flushBuffer() throws IOException { |
|---|
| 43 | + |
|---|
| 44 | + //PrintWriter.flush() does not throw exception |
|---|
| 45 | + if (this.printWriter != null) { |
|---|
| 46 | + this.printWriter.flush(); |
|---|
| 47 | + } |
|---|
| 48 | + |
|---|
| 49 | + IOException exception1 = null; |
|---|
| 50 | + try { |
|---|
| 51 | + if (this.gzipOutputStream != null) { |
|---|
| 52 | + this.gzipOutputStream.flush(); |
|---|
| 53 | + } |
|---|
| 54 | + } catch (IOException e) { |
|---|
| 55 | + exception1 = e; |
|---|
| 56 | + } |
|---|
| 57 | + |
|---|
| 58 | + IOException exception2 = null; |
|---|
| 59 | + try { |
|---|
| 60 | + super.flushBuffer(); |
|---|
| 61 | + } catch (IOException e) { |
|---|
| 62 | + exception2 = e; |
|---|
| 63 | + } |
|---|
| 64 | + |
|---|
| 65 | + if (exception1 != null) |
|---|
| 66 | + throw exception1; |
|---|
| 67 | + if (exception2 != null) |
|---|
| 68 | + throw exception2; |
|---|
| 69 | + } |
|---|
| 70 | + |
|---|
| 71 | + @Override |
|---|
| 72 | + public ServletOutputStream getOutputStream() throws IOException { |
|---|
| 73 | + if (this.printWriter != null) { |
|---|
| 74 | + throw new IllegalStateException("PrintWriter obtained already - cannot get OutputStream"); |
|---|
| 75 | + } |
|---|
| 76 | + if (this.gzipOutputStream == null) { |
|---|
| 77 | + this.gzipOutputStream = new GZIPServletOutputStream(getResponse().getOutputStream()); |
|---|
| 78 | + } |
|---|
| 79 | + return this.gzipOutputStream; |
|---|
| 80 | + } |
|---|
| 81 | + |
|---|
| 82 | + @Override |
|---|
| 83 | + public PrintWriter getWriter() throws IOException { |
|---|
| 84 | + if (this.printWriter == null && this.gzipOutputStream != null) { |
|---|
| 85 | + throw new IllegalStateException("OutputStream obtained already - cannot get PrintWriter"); |
|---|
| 86 | + } |
|---|
| 87 | + if (this.printWriter == null) { |
|---|
| 88 | + this.gzipOutputStream = new GZIPServletOutputStream(getResponse().getOutputStream()); |
|---|
| 89 | + this.printWriter = new PrintWriter(new OutputStreamWriter(this.gzipOutputStream, getResponse().getCharacterEncoding())); |
|---|
| 90 | + } |
|---|
| 91 | + return this.printWriter; |
|---|
| 92 | + } |
|---|
| 93 | + |
|---|
| 94 | + @Override |
|---|
| 95 | + public void setContentLength(int len) { |
|---|
| 96 | + //ignore, since content length of zipped content |
|---|
| 97 | + //does not match content length of unzipped content. |
|---|
| 98 | + } |
|---|
| 99 | + |
|---|
| 100 | + private static class GZIPServletOutputStream extends ServletOutputStream { |
|---|
| 101 | + private final ServletOutputStream servletOutputStream; |
|---|
| 102 | + private final GZIPOutputStream gzipStream; |
|---|
| 103 | + |
|---|
| 104 | + public GZIPServletOutputStream(ServletOutputStream servletOutputStream) throws IOException { |
|---|
| 105 | + this.servletOutputStream = servletOutputStream; |
|---|
| 106 | + this.gzipStream = new GZIPOutputStream(servletOutputStream); |
|---|
| 107 | + } |
|---|
| 108 | + |
|---|
| 109 | + @Override |
|---|
| 110 | + public boolean isReady() { |
|---|
| 111 | + return this.servletOutputStream.isReady(); |
|---|
| 112 | + } |
|---|
| 113 | + |
|---|
| 114 | + @Override |
|---|
| 115 | + public void setWriteListener(WriteListener writeListener) { |
|---|
| 116 | + this.servletOutputStream.setWriteListener(writeListener); |
|---|
| 117 | + } |
|---|
| 118 | + |
|---|
| 119 | + @Override |
|---|
| 120 | + public void write(int b) throws IOException { |
|---|
| 121 | + this.gzipStream.write(b); |
|---|
| 122 | + } |
|---|
| 123 | + |
|---|
| 124 | + @Override |
|---|
| 125 | + public void close() throws IOException { |
|---|
| 126 | + this.gzipStream.close(); |
|---|
| 127 | + } |
|---|
| 128 | + |
|---|
| 129 | + @Override |
|---|
| 130 | + public void flush() throws IOException { |
|---|
| 131 | + this.gzipStream.flush(); |
|---|
| 132 | + } |
|---|
| 133 | + |
|---|
| 134 | + public void finish() throws IOException { |
|---|
| 135 | + this.gzipStream.finish(); |
|---|
| 136 | + } |
|---|
| 137 | + } |
|---|
| 138 | + |
|---|
| 139 | +} |
|---|