1
22
23 package com.liferay.filters.compression;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.servlet.BaseFilter;
28 import com.liferay.portal.kernel.servlet.BrowserSniffer;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.JavaConstants;
31 import com.liferay.portal.kernel.util.ParamUtil;
32 import com.liferay.portal.kernel.util.ServerDetector;
33 import com.liferay.util.Http;
34 import com.liferay.util.SystemProperties;
35
36 import java.io.IOException;
37
38 import javax.servlet.FilterChain;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletRequest;
41 import javax.servlet.ServletResponse;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45
52 public class CompressionFilter extends BaseFilter {
53
54 static boolean useFilter = GetterUtil.getBoolean(
55 SystemProperties.get(CompressionFilter.class.getName()), true);
56
57 public static final String ENCODING = GetterUtil.getString(
58 SystemProperties.get("file.encoding"), "UTF-8");
59
60 static {
61
62
65 if (useFilter) {
66 if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
67 ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
68 ServerDetector.isOrion() || ServerDetector.isTomcat()) {
69
70 useFilter = true;
71 }
72 else {
73 useFilter = false;
74 }
75 }
76 }
77
78 public void doFilter(
79 ServletRequest req, ServletResponse res, FilterChain chain)
80 throws IOException, ServletException {
81
82 if (_log.isDebugEnabled()) {
83 if (useFilter) {
84 _log.debug("Compression is enabled");
85 }
86 else {
87 _log.debug("Compression is disabled");
88 }
89 }
90
91 HttpServletRequest httpReq = (HttpServletRequest)req;
92 HttpServletResponse httpRes = (HttpServletResponse)res;
93
94 String completeURL = Http.getCompleteURL(httpReq);
95
96 if (useFilter && isCompress(httpReq) && !isInclude(httpReq) &&
97 BrowserSniffer.acceptsGzip(httpReq) &&
98 !isAlreadyFiltered(httpReq)) {
99
100 if (_log.isDebugEnabled()) {
101 _log.debug("Compressing " + completeURL);
102 }
103
104 httpReq.setAttribute(_ALREADY_FILTERED, Boolean.TRUE);
105
106 CompressionResponse compressionResponse =
107 new CompressionResponse(httpRes);
108
109 doFilter(CompressionFilter.class, req, compressionResponse, chain);
110
111 compressionResponse.finishResponse();
112 }
113 else {
114 if (_log.isDebugEnabled()) {
115 _log.debug("Not compressing " + completeURL);
116 }
117
118 doFilter(CompressionFilter.class, req, res, chain);
119 }
120 }
121
122 protected boolean isAlreadyFiltered(HttpServletRequest req) {
123 if (req.getAttribute(_ALREADY_FILTERED) != null) {
124 return true;
125 }
126 else {
127 return false;
128 }
129 }
130
131 protected boolean isCompress(HttpServletRequest req) {
132 if (!ParamUtil.get(req, _COMPRESS, true)) {
133 return false;
134 }
135 else {
136
137
142 String windowState = ParamUtil.getString(req, "p_p_state");
143
144 if (windowState.equals("exclusive")) {
145 return false;
146 }
147 else {
148 return true;
149 }
150 }
151 }
152
153 protected boolean isInclude(HttpServletRequest req) {
154 String uri = (String)req.getAttribute(
155 JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
156
157 if (uri == null) {
158 return false;
159 }
160 else {
161 return true;
162 }
163 }
164
165 private static final String _ALREADY_FILTERED =
166 CompressionFilter.class.getName() + "_ALREADY_FILTERED";
167
168 private static final String _COMPRESS = "compress";
169
170 private static Log _log = LogFactoryUtil.getLog(CompressionFilter.class);
171
172 }