~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to net/sf/ehcache/constructs/web/filter/CachingFilter.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-07-14 02:08:53 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080714020853-37uz6uzh6mc5mcgx
Tags: 1.5.0-1
* New upstream release
* Add libjgroups-java to Build-Depends-Indep
* Bump Standards-Version to 3.8.0
* debian/copyright: remove the full text of Apache 2.0 license, as now
  is included in common licenses

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
import org.apache.commons.logging.LogFactory;
34
34
 
35
35
import javax.servlet.FilterChain;
 
36
import javax.servlet.FilterConfig;
36
37
import javax.servlet.http.Cookie;
37
38
import javax.servlet.http.HttpServletRequest;
38
39
import javax.servlet.http.HttpServletResponse;
 
40
import java.io.BufferedOutputStream;
39
41
import java.io.ByteArrayOutputStream;
40
42
import java.io.IOException;
 
43
import java.io.OutputStream;
41
44
import java.util.Collection;
42
45
import java.util.Iterator;
43
46
import java.util.zip.DataFormatException;
55
58
 * The {@link CachingFilter} uses the {@link net.sf.ehcache.constructs.blocking.BlockingCache}. It blocks until the thread which
56
59
 * did a get which results in a null does a put. If reentry happens a second get happens before the first put. The second
57
60
 * get could wait indefinitely. This situation is monitored and if it happens, an IllegalStateException will be thrown.
 
61
 *
58
62
 * @author @author Greg Luck
59
 
 * @version $Id: CachingFilter.java 519 2007-07-27 07:11:45Z gregluck $
 
63
 * @version $Id: CachingFilter.java 606 2008-05-02 23:22:40Z gregluck $
60
64
 */
61
65
public abstract class CachingFilter extends Filter {
 
66
 
62
67
    private static final Log LOG = LogFactory.getLog(CachingFilter.class.getName());
63
68
 
64
 
 
65
69
    /**
66
70
     * The cache holding the web pages. Ensure that all threads for a given cache name are using the same instance of this.
67
71
     */
75
79
     *
76
80
     * @throws CacheException The most likely cause is that a cache has not been
77
81
     *                        configured in ehcache's configuration file ehcache.xml for the filter name
 
82
     * @param filterConfig
78
83
     */
79
 
    public void doInit() throws CacheException {
 
84
    public void doInit(FilterConfig filterConfig) throws CacheException {
80
85
        synchronized (this.getClass()) {
81
86
            if (blockingCache == null) {
82
87
                final String cacheName = getCacheName();
100
105
    }
101
106
 
102
107
    /**
103
 
     * Performs the filtering for a request.
 
108
     * Performs the filtering for a request. This method caches
 
109
     * based responses keyed by {@link #calculateKey(javax.servlet.http.HttpServletRequest)}
 
110
     * <p/>
 
111
     * By default this method will queue requests requesting the page response for a given key
 
112
     * until the first thread in the queue has completed. The request which occurs when the page
 
113
     * expires incurs the cost of waiting for the downstream processing to return the respone.
 
114
     * <p/>
 
115
     * The maximum time to wait can be configured by setting <code>setTimeoutMillis</code> on the
 
116
     * underlying <code>BlockingCache</code>.
104
117
     *
105
118
     * @param request
106
119
     * @param response
208
221
        chain.doFilter(request, wrapper);
209
222
        wrapper.flush();
210
223
 
211
 
 
212
224
        // Return the page info
213
225
        return new PageInfo(wrapper.getStatus(), wrapper.getContentType(), wrapper.getHeaders(), wrapper.getCookies(),
214
226
                outstr.toByteArray(), true);
289
301
 
290
302
        for (Iterator iterator = headers.iterator(); iterator.hasNext();) {
291
303
            final String[] headerPair = (String[]) iterator.next();
292
 
            response.addHeader(headerPair[header], headerPair[value]);
 
304
            response.setHeader(headerPair[header], headerPair[value]);
293
305
        }
294
306
    }
295
307
 
307
319
     * <p/>
308
320
     * This method was introduced in ehcache 1.2.1. Older versions used a singleton CacheManager instance created with
309
321
     * the default factory method.
 
322
     *
310
323
     * @return the CacheManager to be used
311
324
     * @since 1.2.1
312
325
     */
324
337
     * <li>Additional parameters can be added to any page. The page will still work but will miss the
325
338
     * cache. Consider coding defensively around this issue.
326
339
     * </ul>
 
340
     * <p/>
 
341
     * Implementers should differentiate between GET and HEAD requests otherwise blank pages
 
342
     * can result. See SimplePageCachingFilter for an example implementation.
327
343
     *
328
344
     * @param httpRequest
329
345
     * @return the key, generally the URL plus request parameters
362
378
        }
363
379
 
364
380
        response.setContentLength(body.length);
365
 
        response.getOutputStream().write(body);
 
381
        OutputStream out = new BufferedOutputStream(response.getOutputStream());
 
382
        out.write(body);
 
383
        out.flush();
366
384
    }
367
385
 
368
386
    /**