|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package com.opensymphony.oscache.web.filter; |
|
6 |
| |
|
7 |
| import com.opensymphony.oscache.base.Cache; |
|
8 |
| import com.opensymphony.oscache.base.NeedsRefreshException; |
|
9 |
| import com.opensymphony.oscache.web.ServletCacheAdministrator; |
|
10 |
| |
|
11 |
| import org.apache.commons.logging.Log; |
|
12 |
| import org.apache.commons.logging.LogFactory; |
|
13 |
| |
|
14 |
| import java.io.IOException; |
|
15 |
| |
|
16 |
| import javax.servlet.*; |
|
17 |
| import javax.servlet.http.HttpServletRequest; |
|
18 |
| import javax.servlet.http.HttpServletResponse; |
|
19 |
| import javax.servlet.jsp.PageContext; |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| public class CacheFilter implements Filter, ICacheKeyProvider, ICacheGroupsProvider { |
|
32 |
| |
|
33 |
| public static final String HEADER_LAST_MODIFIED = "Last-Modified"; |
|
34 |
| public static final String HEADER_CONTENT_TYPE = "Content-Type"; |
|
35 |
| public static final String HEADER_CONTENT_ENCODING = "Content-Encoding"; |
|
36 |
| public static final String HEADER_EXPIRES = "Expires"; |
|
37 |
| public static final String HEADER_IF_MODIFIED_SINCE = "If-Modified-Since"; |
|
38 |
| public static final String HEADER_CACHE_CONTROL = "Cache-control"; |
|
39 |
| public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding"; |
|
40 |
| |
|
41 |
| |
|
42 |
| public static final int FRAGMENT_AUTODETECT = -1; |
|
43 |
| public static final int FRAGMENT_NO = 0; |
|
44 |
| public static final int FRAGMENT_YES = 1; |
|
45 |
| |
|
46 |
| |
|
47 |
| public static final int NOCACHE_OFF = 0; |
|
48 |
| public static final int NOCACHE_SESSION_ID_IN_URL = 1; |
|
49 |
| |
|
50 |
| |
|
51 |
| public static final long LAST_MODIFIED_OFF = 0; |
|
52 |
| public static final long LAST_MODIFIED_ON = 1; |
|
53 |
| public static final long LAST_MODIFIED_INITIAL = -1; |
|
54 |
| |
|
55 |
| |
|
56 |
| public static final long EXPIRES_OFF = 0; |
|
57 |
| public static final long EXPIRES_ON = 1; |
|
58 |
| public static final long EXPIRES_TIME = -1; |
|
59 |
| |
|
60 |
| |
|
61 |
| private final static String REQUEST_FILTERED = "__oscache_filtered"; |
|
62 |
| |
|
63 |
| |
|
64 |
| private ExpiresRefreshPolicy expiresRefreshPolicy; |
|
65 |
| |
|
66 |
| |
|
67 |
| private final Log log = LogFactory.getLog(this.getClass()); |
|
68 |
| |
|
69 |
| |
|
70 |
| private FilterConfig config; |
|
71 |
| private ServletCacheAdministrator admin = null; |
|
72 |
| private int cacheScope = PageContext.APPLICATION_SCOPE; |
|
73 |
| private int fragment = FRAGMENT_AUTODETECT; |
|
74 |
| private int time = 60 * 60; |
|
75 |
| private int nocache = NOCACHE_OFF; |
|
76 |
| private long lastModified = LAST_MODIFIED_INITIAL; |
|
77 |
| private long expires = EXPIRES_ON; |
|
78 |
| private ICacheKeyProvider cacheKeyProvider = this; |
|
79 |
| private ICacheGroupsProvider cacheGroupsProvider = this; |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
0
| public void destroy() {
|
|
85 |
| |
|
86 |
| } |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
0
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
|
|
101 |
0
| if (log.isInfoEnabled()) {
|
|
102 |
0
| log.info("<cache>: filter in scope " + cacheScope);
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
0
| if (isFilteredBefore(request) || !isCacheable(request)) {
|
|
107 |
0
| chain.doFilter(request, response);
|
|
108 |
0
| return;
|
|
109 |
| } |
|
110 |
0
| request.setAttribute(REQUEST_FILTERED, Boolean.TRUE);
|
|
111 |
| |
|
112 |
0
| HttpServletRequest httpRequest = (HttpServletRequest) request;
|
|
113 |
| |
|
114 |
| |
|
115 |
0
| boolean fragmentRequest = isFragment(httpRequest);
|
|
116 |
| |
|
117 |
| |
|
118 |
0
| Cache cache;
|
|
119 |
0
| if (cacheScope == PageContext.SESSION_SCOPE) {
|
|
120 |
0
| cache = admin.getSessionScopeCache(httpRequest.getSession(true));
|
|
121 |
| } else { |
|
122 |
0
| cache = admin.getAppScopeCache(config.getServletContext());
|
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
0
| String key = cacheKeyProvider.createCacheKey(httpRequest, admin, cache);
|
|
127 |
| |
|
128 |
0
| try {
|
|
129 |
0
| ResponseContent respContent = (ResponseContent) cache.getFromCache(key, time);
|
|
130 |
| |
|
131 |
0
| if (log.isInfoEnabled()) {
|
|
132 |
0
| log.info("<cache>: Using cached entry for " + key);
|
|
133 |
| } |
|
134 |
| |
|
135 |
0
| boolean acceptsGZip = false;
|
|
136 |
0
| if ((!fragmentRequest) && (lastModified != LAST_MODIFIED_OFF)) {
|
|
137 |
0
| long clientLastModified = httpRequest.getDateHeader(HEADER_IF_MODIFIED_SINCE);
|
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
0
| if ((clientLastModified != -1) && (clientLastModified >= respContent.getLastModified())) {
|
|
142 |
0
| ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
|
143 |
0
| return;
|
|
144 |
| } |
|
145 |
| |
|
146 |
0
| acceptsGZip = respContent.isContentGZiped() && acceptsGZipEncoding(httpRequest);
|
|
147 |
| } |
|
148 |
| |
|
149 |
0
| respContent.writeTo(response, fragmentRequest, acceptsGZip);
|
|
150 |
| |
|
151 |
| |
|
152 |
| } catch (NeedsRefreshException nre) { |
|
153 |
0
| boolean updateSucceeded = false;
|
|
154 |
| |
|
155 |
0
| try {
|
|
156 |
0
| if (log.isInfoEnabled()) {
|
|
157 |
0
| log.info("<cache>: New cache entry, cache stale or cache scope flushed for " + key);
|
|
158 |
| } |
|
159 |
| |
|
160 |
0
| CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse) response, fragmentRequest, time * 1000L, lastModified, expires);
|
|
161 |
0
| chain.doFilter(request, cacheResponse);
|
|
162 |
0
| cacheResponse.flushBuffer();
|
|
163 |
| |
|
164 |
| |
|
165 |
0
| if (isCacheable(cacheResponse)) {
|
|
166 |
| |
|
167 |
0
| String[] groups = cacheGroupsProvider.createCacheGroups(httpRequest, admin, cache);
|
|
168 |
| |
|
169 |
0
| cache.putInCache(key, cacheResponse.getContent(), groups, expiresRefreshPolicy, null);
|
|
170 |
0
| updateSucceeded = true;
|
|
171 |
| } |
|
172 |
| } finally { |
|
173 |
0
| if (!updateSucceeded) {
|
|
174 |
0
| cache.cancelUpdate(key);
|
|
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 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
0
| public void init(FilterConfig filterConfig) {
|
|
221 |
| |
|
222 |
0
| config = filterConfig;
|
|
223 |
0
| admin = ServletCacheAdministrator.getInstance(config.getServletContext());
|
|
224 |
| |
|
225 |
| |
|
226 |
0
| try {
|
|
227 |
0
| time = Integer.parseInt(config.getInitParameter("time"));
|
|
228 |
| } catch (Exception e) { |
|
229 |
0
| log.info("Could not get init parameter 'time', defaulting to one hour.");
|
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
0
| expiresRefreshPolicy = new ExpiresRefreshPolicy(time);
|
|
234 |
| |
|
235 |
| |
|
236 |
0
| try {
|
|
237 |
0
| String scopeString = config.getInitParameter("scope");
|
|
238 |
| |
|
239 |
0
| if (scopeString.equals("session")) {
|
|
240 |
0
| cacheScope = PageContext.SESSION_SCOPE;
|
|
241 |
0
| } else if (scopeString.equals("application")) {
|
|
242 |
0
| cacheScope = PageContext.APPLICATION_SCOPE;
|
|
243 |
0
| } else if (scopeString.equals("request")) {
|
|
244 |
0
| cacheScope = PageContext.REQUEST_SCOPE;
|
|
245 |
0
| } else if (scopeString.equals("page")) {
|
|
246 |
0
| cacheScope = PageContext.PAGE_SCOPE;
|
|
247 |
| } |
|
248 |
| } catch (Exception e) { |
|
249 |
0
| log.info("Could not get init parameter 'scope', defaulting to 'application'.");
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
0
| try {
|
|
254 |
0
| String fragmentString = config.getInitParameter("fragment");
|
|
255 |
| |
|
256 |
0
| if (fragmentString.equals("no")) {
|
|
257 |
0
| fragment = FRAGMENT_NO;
|
|
258 |
0
| } else if (fragmentString.equals("yes")) {
|
|
259 |
0
| fragment = FRAGMENT_YES;
|
|
260 |
0
| } else if (fragmentString.equalsIgnoreCase("auto")) {
|
|
261 |
0
| fragment = FRAGMENT_AUTODETECT;
|
|
262 |
| } |
|
263 |
| } catch (Exception e) { |
|
264 |
0
| log.info("Could not get init parameter 'fragment', defaulting to 'auto detect'.");
|
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
0
| try {
|
|
269 |
0
| String nocacheString = config.getInitParameter("nocache");
|
|
270 |
| |
|
271 |
0
| if (nocacheString.equals("off")) {
|
|
272 |
0
| nocache = NOCACHE_OFF;
|
|
273 |
0
| } else if (nocacheString.equalsIgnoreCase("sessionIdInURL")) {
|
|
274 |
0
| nocache = NOCACHE_SESSION_ID_IN_URL;
|
|
275 |
| } |
|
276 |
| } catch (Exception e) { |
|
277 |
0
| log.info("Could not get init parameter 'nocache', defaulting to 'off'.");
|
|
278 |
| } |
|
279 |
| |
|
280 |
| |
|
281 |
0
| try {
|
|
282 |
0
| String lastModifiedString = config.getInitParameter("lastModified");
|
|
283 |
| |
|
284 |
0
| if (lastModifiedString.equals("off")) {
|
|
285 |
0
| lastModified = LAST_MODIFIED_OFF;
|
|
286 |
0
| } else if (lastModifiedString.equals("on")) {
|
|
287 |
0
| lastModified = LAST_MODIFIED_ON;
|
|
288 |
0
| } else if (lastModifiedString.equalsIgnoreCase("initial")) {
|
|
289 |
0
| lastModified = LAST_MODIFIED_INITIAL;
|
|
290 |
| } |
|
291 |
| } catch (Exception e) { |
|
292 |
0
| log.info("Could not get init parameter 'lastModified', defaulting to 'initial'.");
|
|
293 |
| } |
|
294 |
| |
|
295 |
| |
|
296 |
0
| try {
|
|
297 |
0
| String expiresString = config.getInitParameter("expires");
|
|
298 |
| |
|
299 |
0
| if (expiresString.equals("off")) {
|
|
300 |
0
| expires = EXPIRES_OFF;
|
|
301 |
0
| } else if (expiresString.equals("on")) {
|
|
302 |
0
| expires = EXPIRES_ON;
|
|
303 |
0
| } else if (expiresString.equalsIgnoreCase("time")) {
|
|
304 |
0
| expires = EXPIRES_TIME;
|
|
305 |
| } |
|
306 |
| } catch (Exception e) { |
|
307 |
0
| log.info("Could not get init parameter 'expires', defaulting to 'on'.");
|
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
0
| try {
|
|
312 |
0
| String className = config.getInitParameter("ICacheKeyProvider");
|
|
313 |
| |
|
314 |
0
| try {
|
|
315 |
0
| Class clazz = Class.forName(className);
|
|
316 |
| |
|
317 |
0
| if (!ICacheKeyProvider.class.isAssignableFrom(clazz)) {
|
|
318 |
0
| log.error("Specified class '" + className + "' does not implement ICacheKeyProvider. Ignoring this provider.");
|
|
319 |
| } else { |
|
320 |
0
| cacheKeyProvider = (ICacheKeyProvider) clazz.newInstance();
|
|
321 |
| } |
|
322 |
| } catch (ClassNotFoundException e) { |
|
323 |
0
| log.error("Class '" + className + "' not found. Ignoring this cache key provider.", e);
|
|
324 |
| } catch (InstantiationException e) { |
|
325 |
0
| log.error("Class '" + className + "' could not be instantiated because it is not a concrete class. Ignoring this cache key provider.", e);
|
|
326 |
| } catch (IllegalAccessException e) { |
|
327 |
0
| log.error("Class '" + className + "' could not be instantiated because it is not public. Ignoring this cache key provider.", e);
|
|
328 |
| } |
|
329 |
| } catch (Exception e) { |
|
330 |
0
| log.info("Could not get init parameter 'ICacheKeyProvider', defaulting to " + this.getClass().getName() + ".");
|
|
331 |
| } |
|
332 |
| |
|
333 |
| |
|
334 |
0
| try {
|
|
335 |
0
| String className = config.getInitParameter("ICacheGroupsProvider");
|
|
336 |
| |
|
337 |
0
| try {
|
|
338 |
0
| Class clazz = Class.forName(className);
|
|
339 |
| |
|
340 |
0
| if (!ICacheGroupsProvider.class.isAssignableFrom(clazz)) {
|
|
341 |
0
| log.error("Specified class '" + className + "' does not implement ICacheGroupsProvider. Ignoring this provider.");
|
|
342 |
| } else { |
|
343 |
0
| cacheGroupsProvider = (ICacheGroupsProvider) clazz.newInstance();
|
|
344 |
| } |
|
345 |
| } catch (ClassNotFoundException e) { |
|
346 |
0
| log.error("Class '" + className + "' not found. Ignoring this cache key provider.", e);
|
|
347 |
| } catch (InstantiationException e) { |
|
348 |
0
| log.error("Class '" + className + "' could not be instantiated because it is not a concrete class. Ignoring this cache groups provider.", e);
|
|
349 |
| } catch (IllegalAccessException e) { |
|
350 |
0
| log.error("Class '" + className + "' could not be instantiated because it is not public. Ignoring this cache groups provider.", e);
|
|
351 |
| } |
|
352 |
| } catch (Exception e) { |
|
353 |
0
| log.info("Could not get init parameter 'ICacheGroupsProvider', defaulting to " + this.getClass().getName() + ".");
|
|
354 |
| } |
|
355 |
| } |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
0
| public String createCacheKey(HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache) {
|
|
361 |
0
| return scAdmin.generateEntryKey(null, httpRequest, cacheScope);
|
|
362 |
| } |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
0
| public String[] createCacheGroups(HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache) {
|
|
368 |
0
| return null;
|
|
369 |
| } |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
0
| protected boolean isFragment(HttpServletRequest request) {
|
|
384 |
0
| if (fragment == FRAGMENT_AUTODETECT) {
|
|
385 |
0
| return request.getAttribute("javax.servlet.include.request_uri") != null;
|
|
386 |
| } else { |
|
387 |
0
| return (fragment == FRAGMENT_NO) ? false : true;
|
|
388 |
| } |
|
389 |
| } |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
0
| protected boolean isFilteredBefore(ServletRequest request) {
|
|
401 |
0
| return request.getAttribute(REQUEST_FILTERED) != null;
|
|
402 |
| } |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
0
| protected boolean isCacheable(ServletRequest request) {
|
|
412 |
| |
|
413 |
0
| boolean cachable = request instanceof HttpServletRequest;
|
|
414 |
| |
|
415 |
0
| if (cachable) {
|
|
416 |
0
| HttpServletRequest requestHttp = (HttpServletRequest) request;
|
|
417 |
0
| if (nocache == NOCACHE_SESSION_ID_IN_URL) {
|
|
418 |
0
| cachable = !requestHttp.isRequestedSessionIdFromURL();
|
|
419 |
| } |
|
420 |
| } |
|
421 |
| |
|
422 |
0
| if (log.isDebugEnabled()) {
|
|
423 |
0
| log.debug("<cache>: the request " + ((cachable) ? "is" : "is not") + " cachable.");
|
|
424 |
| } |
|
425 |
| |
|
426 |
0
| return cachable;
|
|
427 |
| } |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
0
| protected boolean isCacheable(CacheHttpServletResponseWrapper cacheResponse) {
|
|
437 |
| |
|
438 |
| |
|
439 |
0
| boolean cachable = cacheResponse.getStatus() == HttpServletResponse.SC_OK;
|
|
440 |
| |
|
441 |
0
| if (log.isDebugEnabled()) {
|
|
442 |
0
| log.debug("<cache>: the response " + ((cachable) ? "is" : "is not") + " cachable.");
|
|
443 |
| } |
|
444 |
| |
|
445 |
0
| return cachable;
|
|
446 |
| } |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
0
| protected boolean acceptsGZipEncoding(HttpServletRequest request) {
|
|
455 |
0
| String acceptEncoding = request.getHeader(HEADER_ACCEPT_ENCODING);
|
|
456 |
0
| return (acceptEncoding != null) && (acceptEncoding.indexOf("gzip") != -1);
|
|
457 |
| } |
|
458 |
| |
|
459 |
| } |