1 package gboat2.base.plugin.servlet.filter;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.net.URLConnection;
6
7 import javax.servlet.Filter;
8 import javax.servlet.FilterChain;
9 import javax.servlet.FilterConfig;
10 import javax.servlet.ServletException;
11 import javax.servlet.ServletRequest;
12 import javax.servlet.ServletResponse;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class GboatCSSFilter implements Filter {
40
41 private static Logger logger = LoggerFactory.getLogger(GboatCSSFilter.class);
42
43 @Override
44 public void init(FilterConfig arg0) throws ServletException {
45
46 }
47
48 @Override
49 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
50 HttpServletRequest request = (HttpServletRequest) req;
51 HttpServletResponse response = (HttpServletResponse) res;
52
53 String requestURL = request.getRequestURL().toString();
54 String queryString = request.getQueryString();
55 if (null != queryString) {
56 requestURL += ("?" + queryString);
57 }
58
59 if (requestURL.contains("$")) {
60 logger.info("deal css file : " + requestURL);
61
62 requestURL = requestURL.replace("$", "");
63
64
65 URLConnection urlCon = null;
66
67 try {
68 urlCon = new URL(requestURL).openConnection();
69 urlCon.setConnectTimeout(0);
70 urlCon.setReadTimeout(0);
71
72 String message = urlCon.getHeaderField(0);
73
74 if (message.equals("HTTP/1.1 200 OK")) {
75 response.sendRedirect(requestURL);
76 } else {
77 String newURL = requestURL.replace(requestURL.substring(requestURL.indexOf("/themes/") + 8, requestURL.indexOf("/css/")), "default");
78 response.sendRedirect(newURL);
79 }
80 } catch (Exception e) {
81 logger.error("CSS 过滤器发生错误。", e);
82 }
83 } else {
84 chain.doFilter(request, response);
85 }
86 }
87
88 @Override
89 public void destroy() {
90 }
91
92 }