-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PrecompressedArtifactFilter.java
212 lines (181 loc) · 7.3 KB
/
PrecompressedArtifactFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* #%L
* BroadleafCommerce Common Libraries
* %%
* Copyright (C) 2009 - 2016 Broadleaf Commerce
* %%
* Licensed under the Broadleaf Fair Use License Agreement, Version 1.0
* (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt)
* unless the restrictions on use therein are violated and require payment to Broadleaf in which case
* the Broadleaf End User License Agreement (EULA), Version 1.1
* (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt)
* shall apply.
*
* Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License")
* between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license.
* #L%
*/
package org.broadleafcommerce.common.web.util;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.env.Environment;
import org.springframework.web.filter.GenericFilterBean;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Jeff Fischer
*/
public class PrecompressedArtifactFilter extends GenericFilterBean {
private boolean useWhileInDefaultEnvironment = true;
@Autowired
@Qualifier("blPrecompressedArtifactFileExtensionWhitelist")
List<String> fileExtensionWhitelist;
@Resource
Environment environment;
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
checkOutput: {
if (ArrayUtils.isNotEmpty(environment.getActiveProfiles()) || useWhileInDefaultEnvironment) {
String path = getResourcePath(request);
String gzipPath = path + ".gz";
if (useGzipCompression(request, response, path, gzipPath)) {
File output = new File(getServletContext().getRealPath(gzipPath));
if (output.exists()) {
response.addHeader("Content-Encoding", "gzip");
ServletOutputStream sos = servletResponse.getOutputStream();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(output));
boolean eof = false;
while (!eof) {
int temp = bis.read();
if (temp < 0) {
eof = true;
} else {
sos.write(temp);
}
}
} finally {
sos.flush();
try {
if (bis != null) {
bis.close();
}
} catch (Exception e) {
//do nothing
}
}
break checkOutput;
}
}
}
chain.doFilter(request, response);
}
}
/**
* Copied from Tomcat
*
* Return the page resource path from the request. For example:
* <pre class="codeHtml">
* <span class="blue">http://www.mycorp.com/banking/secure/login.htm</span> -> <span class="red">/secure/login.htm</span> </pre>
*
* @param request the page servlet request
* @return the page resource path from the request
*/
public String getResourcePath(HttpServletRequest request) {
// Adapted from VelocityViewServlet.handleRequest() method:
// If we get here from RequestDispatcher.include(), getServletPath()
// will return the original (wrong) URI requested. The following
// special attribute holds the correct path. See section 8.3 of the
// Servlet 2.3 specification.
String path = (String)
request.getAttribute("javax.servlet.include.servlet_path");
// Also take into account the PathInfo stated on
// SRV.4.4 Request Path Elements.
String info = (String)
request.getAttribute("javax.servlet.include.path_info");
if (path == null) {
path = request.getServletPath();
info = request.getPathInfo();
}
if (info != null) {
path += info;
}
return path;
}
/**
* Copied from Tomcat
*
* Return true if the response should be GZIP compressed.
*
* @param request the request to test
* @param response the response to test
* @param path the request path to test
* @return true if the response should be GZIP compressed
*/
protected boolean useGzipCompression(HttpServletRequest request, HttpServletResponse response, String path, String gzipPath) throws MalformedURLException {
if (gzipPath == null) {
return false;
}
String temp = path.toLowerCase();
if (!fileExtensionInWhitelist(temp)) {
return false;
}
if (getServletContext().getResource(gzipPath) == null) {
return false;
}
// If Content-Encoding header is already set on response, skip compression
if (response.containsHeader("Content-Encoding")) {
return false;
}
// Are we allowed to compress ?
String s = request.getParameter("gzip");
if ("false".equals(s)) {
return false;
}
Enumeration<?> e = request.getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
if (name.contains("gzip")) {
return true;
}
}
return true;
}
protected boolean fileExtensionInWhitelist(String path) {
if (path == null || !path.contains(".")) {
return false;
}
path = path.toLowerCase();
boolean inWhitelist = false;
for (String validFileExtension : fileExtensionWhitelist) {
if (path.endsWith(validFileExtension)) {
inWhitelist = true;
break;
}
}
return inWhitelist;
}
public boolean isUseWhileInDefaultEnvironment() {
return useWhileInDefaultEnvironment;
}
public void setUseWhileInDefaultEnvironment(boolean useWhileInDefaultEnvironment) {
this.useWhileInDefaultEnvironment = useWhileInDefaultEnvironment;
}
}