View Javadoc
1   /*
2    * Copyright (c) 2014, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, 
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
6    */
7   package nl.mineleni.cbsviewer.servlet;
8   
9   import java.io.IOException;
10  import java.io.OutputStreamWriter;
11  import java.io.PrintWriter;
12  
13  import javax.servlet.ServletOutputStream;
14  import javax.servlet.http.HttpServletResponse;
15  import javax.servlet.http.HttpServletResponseWrapper;
16  
17  import org.apache.http.HttpHeaders;
18  
19  /**
20   * The Class GZIPHttpServletResponseWrapper.
21   */
22  public class GZIPHttpServletResponseWrapper extends HttpServletResponseWrapper {
23  	/** gecomprimeerde output. */
24  	private ServletResponseGZIPOutputStream gzipStream;
25  	/** te comprimeren output. */
26  	private ServletOutputStream outputStream;
27  	/** output. */
28  	private PrintWriter printWriter;
29  
30  	/**
31  	 * Instantiates a new GZIP http servlet response wrapper.
32  	 *
33  	 * @param response
34  	 *            the response
35  	 * @throws IOException
36  	 *             Signals that an I/O exception has occurred.
37  	 */
38  	public GZIPHttpServletResponseWrapper(HttpServletResponse response)
39  			throws IOException {
40  		super(response);
41  		response.addHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
42  	}
43  
44  	/**
45  	 * Finish.
46  	 *
47  	 * @throws IOException
48  	 *             Signals that an I/O exception has occurred.
49  	 */
50  	public void finish() throws IOException {
51  		if (printWriter != null) {
52  			printWriter.close();
53  		}
54  		if (outputStream != null) {
55  			outputStream.close();
56  		}
57  		if (gzipStream != null) {
58  			gzipStream.close();
59  		}
60  	}
61  
62  	/*
63  	 * (non-Javadoc)
64  	 * 
65  	 * @see javax.servlet.ServletResponseWrapper#flushBuffer()
66  	 */
67  	@Override
68  	public void flushBuffer() throws IOException {
69  		if (printWriter != null) {
70  			printWriter.flush();
71  		}
72  		if (outputStream != null) {
73  			outputStream.flush();
74  		}
75  		super.flushBuffer();
76  	}
77  
78  	/*
79  	 * (non-Javadoc)
80  	 * 
81  	 * @see javax.servlet.ServletResponseWrapper#getOutputStream()
82  	 */
83  	@Override
84  	public ServletOutputStream getOutputStream() throws IOException {
85  		if (printWriter != null) {
86  			throw new IllegalStateException("printWriter already defined");
87  		}
88  		if (outputStream == null) {
89  			initGzip();
90  			outputStream = gzipStream;
91  		}
92  		return outputStream;
93  	}
94  
95  	/*
96  	 * (non-Javadoc)
97  	 * 
98  	 * @see javax.servlet.ServletResponseWrapper#getWriter()
99  	 */
100 	@Override
101 	public PrintWriter getWriter() throws IOException {
102 		if (outputStream != null) {
103 			throw new IllegalStateException("printWriter already defined");
104 		}
105 		if (printWriter == null) {
106 			initGzip();
107 			printWriter = new PrintWriter(new OutputStreamWriter(gzipStream,
108 					getResponse().getCharacterEncoding()));
109 		}
110 		return printWriter;
111 	}
112 
113 	/*
114 	 * (non-Javadoc)
115 	 * 
116 	 * @see javax.servlet.ServletResponseWrapper#setContentLength(int)
117 	 */
118 	@Override
119 	public void setContentLength(int len) {
120 	}
121 
122 	/**
123 	 * Inits the gzip.
124 	 *
125 	 * @throws IOException
126 	 *             Signals that an I/O exception has occurred.
127 	 */
128 	private void initGzip() throws IOException {
129 		gzipStream = new ServletResponseGZIPOutputStream(getResponse()
130 				.getOutputStream());
131 	}
132 
133 }