View Javadoc
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   * CSS 文件过滤器,解决某些主题下有部分 CSS 文件不存在的问题。<br>
22   * 在 web.xml 中添加如下配置启用该过滤器:
23   * <pre><code>
24   * &lt;!-- css过滤器 --&gt;
25   * &lt;filter&gt;
26   *     &lt;filter-name&gt;gboat2CssFilter&lt;/filter-name&gt;
27   *     &lt;filter-class&gt;gboat2.base.plugin.servlet.filter.GboatCSSFilter&lt;/filter-class&gt;
28   * &lt;/filter&gt;
29   * &lt;filter-mapping&gt;
30   *     &lt;filter-name&gt;gboat2CssFilter&lt;/filter-name&gt;
31   *     &lt;url-pattern&gt;*.css&lt;/url-pattern&gt;
32   * &lt;/filter-mapping&gt;
33   * </code></pre>
34   * @date 2012-4-17
35   * @author zhangxj-a
36   * @author <a href="mailto:[email protected]">何明旺</a>
37   * @since 1.0
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  			//替换uri中的'$'
62  			requestURL = requestURL.replace("$", "");
63  			// http://192.168.69.68:8080/G2/static/gboat2.base.logginges/gray/css/logging_style.css
64  			
65  			URLConnection urlCon = null;
66  			
67  			try {
68  				urlCon = new URL(requestURL).openConnection();
69  				urlCon.setConnectTimeout(0);
70  				urlCon.setReadTimeout(0);
71  				 // 文件存在返回‘HTTP/1.1 200 OK’;文件不存在返回 ‘HTTP/1.1 404 Not Found’
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  }