rsanchez
2017-06-08 d00705286218a5f501ab18bc8a50ec22f043b8b7
#0 enh - Gzipping main bundle on the fly
1 files added
3 files modified
changed files
securis/src/main/java/net/curisit/securis/GzipFilter.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/utils/GZipServletResponseWrapper.java patch | view | blame | history
securis/src/main/webapp/WEB-INF/web.xml patch | view | blame | history
securis/src/main/webapp/index.jsp patch | view | blame | history
securis/src/main/java/net/curisit/securis/GzipFilter.java
....@@ -10,13 +10,16 @@
1010 import javax.servlet.ServletRequest;
1111 import javax.servlet.ServletResponse;
1212 import javax.servlet.annotation.WebFilter;
13
+import javax.servlet.http.HttpServletRequest;
1314 import javax.servlet.http.HttpServletResponse;
1415
1516 import org.apache.logging.log4j.LogManager;
1617 import org.apache.logging.log4j.Logger;
1718
19
+import net.curisit.securis.utils.GZipServletResponseWrapper;
20
+
1821 @ApplicationScoped
19
-@WebFilter(urlPatterns = "*.gz")
22
+@WebFilter(urlPatterns = "*.js")
2023 public class GzipFilter implements Filter {
2124
2225 @SuppressWarnings("unused")
....@@ -27,12 +30,24 @@
2730 }
2831
2932 @Override
30
- public void doFilter(ServletRequest sreq, ServletResponse sres, FilterChain fc) throws IOException, ServletException {
31
- //HttpServletRequest req = (HttpServletRequest) sreq;
32
- HttpServletResponse res = (HttpServletResponse) sres;
33
- res.addHeader("Content-Encoding", "gzip");
34
- // LOG.info("Content served as gzip: {}", req.getRequestURI());
35
- fc.doFilter(sreq, sres);
33
+ public void doFilter(ServletRequest sreq, ServletResponse sres, FilterChain chain) throws IOException, ServletException {
34
+ HttpServletRequest httpRequest = (HttpServletRequest) sreq;
35
+ HttpServletResponse httpResponse = (HttpServletResponse) sres;
36
+
37
+ if (acceptsGZipEncoding(httpRequest)) {
38
+ httpResponse.addHeader("Content-Encoding", "gzip");
39
+ GZipServletResponseWrapper gzipResponse = new GZipServletResponseWrapper(httpResponse);
40
+ chain.doFilter(sreq, gzipResponse);
41
+ gzipResponse.close();
42
+ } else {
43
+ chain.doFilter(sreq, sres);
44
+ }
45
+ }
46
+
47
+ private boolean acceptsGZipEncoding(HttpServletRequest httpRequest) {
48
+ String acceptEncoding = httpRequest.getHeader("Accept-Encoding");
49
+
50
+ return acceptEncoding != null && acceptEncoding.indexOf("gzip") != -1;
3651 }
3752
3853 @Override
securis/src/main/java/net/curisit/securis/utils/GZipServletResponseWrapper.java
....@@ -0,0 +1,139 @@
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
+}
securis/src/main/webapp/WEB-INF/web.xml
....@@ -60,7 +60,7 @@
6060 </filter>
6161 <filter-mapping>
6262 <filter-name>GzipFilter</filter-name>
63
- <url-pattern>*.gz</url-pattern>
63
+ <url-pattern>/main-bundle.js</url-pattern>
6464 </filter-mapping>
6565
6666 <filter>
securis/src/main/webapp/index.jsp
....@@ -11,7 +11,7 @@
1111
1212 <script src="jspm.browser.js"></script>
1313 <script src="jspm.config.js"></script>
14
- <script src="main-bundle.js.gz"></script>
14
+ <script src="main-bundle.js"></script>
1515 <script>
1616 System.import('src/main.js').catch(function(err){ console.error(err); });
1717 </script>