1
2
3
4
5 package gboat2.base.plugin.servlet.filter;
6
7 import gboat2.base.bridge.GboatAppConstants;
8 import gboat2.base.bridge.debug.AbstractDebugHook;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.net.URLDecoder;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16 import javax.servlet.Filter;
17 import javax.servlet.FilterChain;
18 import javax.servlet.FilterConfig;
19 import javax.servlet.ServletContext;
20 import javax.servlet.ServletException;
21 import javax.servlet.ServletRequest;
22 import javax.servlet.ServletResponse;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
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 public class StaticFileDebugFilter extends AbstractDebugHook implements Filter {
62
63 private Logger logger = LoggerFactory.getLogger(StaticFileDebugFilter.class);
64
65
66
67
68
69
70
71
72 private ServletContext servletContext;
73
74 @Override
75 public void init(FilterConfig filterConfig) throws ServletException {
76 servletContext = filterConfig.getServletContext();
77 initConfig(filterConfig.getServletContext().getResourceAsStream(CONFIG_FILE));
78
79
80
81
82
83
84 }
85
86 @Override
87 public void destroy() {
88 }
89
90
91
92
93
94
95
96
97 @Override
98 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
99 throws IOException, ServletException {
100 HttpServletRequest req = (HttpServletRequest) request;
101 HttpServletResponse res = (HttpServletResponse) response;
102
103 if (devMode) {
104 String reqURL = StringUtils.removeStart(req.getRequestURI(), req.getContextPath());
105 Matcher matcher = Pattern.compile("^/static/(.+?)/.*$").matcher(reqURL);
106 if (matcher.matches()) {
107 String bundle = matcher.group(1);
108 if (isBundleDebugEnabled(bundle)) {
109 hook(req, res, bundle, reqURL);
110 return;
111 }
112 }
113 }
114
115 chain.doFilter(request, response);
116 }
117
118
119 private void hook(HttpServletRequest request, HttpServletResponse response, String bundle, String reqURL)
120 throws IOException {
121
122
123
124 reqURL = URLDecoder.decode(reqURL, GboatAppConstants.ENCODING_UTF8);
125 String pathname = getResourcesAbsolutPath(bundle) + reqURL;
126 logger.debug("use debug file : {}", pathname);
127 File file = new File(pathname);
128 if (file.exists()) {
129 String contenType = servletContext.getMimeType(pathname);
130 if (null != contenType) {
131 response.setContentType(contenType);
132 }
133 response.setContentLength((int) file.length());
134 response.setHeader("Cache-Control", "no-cache");
135 response.setHeader("Pragma", "no-cache");
136 response.setHeader("Expires", "-1");
137 FileUtils.copyFile(file, response.getOutputStream());
138 } else {
139 response.sendError(HttpServletResponse.SC_NOT_FOUND, pathname);
140 }
141
142 }
143 }