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