Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// This Source Code is Copyright & Licenced as indicated below
package org.litesoft.GWT.server;
/*
 * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.regex.*;

/**
 * This is a filter which enforces proper caching of the generated GWT script
 * files. It requires that you serve your GWT application via a Java servlet
 * container.
 * <p/>
 * To use, add the jar to <code>WEB-INF/lib</code> and add the
 * following to your deployment descriptor (web.xml):
 * <p/>
 * <pre>
 * &lt;filter&gt;
 *   &lt;filter-name&gt;GWTCacheFilter&lt;/filter-name&gt;
 *   &lt;filter-class&gt;asquare.gwt.tk.server.GWTCacheFilter&lt;/filter-class&gt;
 *   &lt;description&gt;Enforces proper caching of GWT files&lt;/description&gt;
 * &lt;/filter&gt;
 *
 * &lt;filter-mapping&gt;
 *   &lt;filter-name&gt;GWTCacheFilter&lt;/filter-name&gt;
 *   &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
 * &lt;/filter-mapping&gt;</pre>
 *
 * By default, files ending in <code>.cache.*</code> are cached and files
 * ending in <code>.nocache.*</code> are not cached. You can override the
 * defaults by specifying file name patterns in filter init-params. The pattern
 * is parsed as a JDK regular expression. The defaults are below:
 *
 * <pre>
 * &lt;init-param&gt;
 *   &lt;param-name&gt;forceDontCache&lt;/param-name&gt;
 *   &lt;param-value&gt;.+\.nocache\..+&lt;/param-value&gt;
 * &lt;/init-param&gt;
 * &lt;init-param&gt;
 *   &lt;param-name&gt;forceCache&lt;/param-name&gt;
 *   &lt;param-value&gt;.+\.cache\..+&lt;/param-value&gt;
 * &lt;/init-param&gt;</pre>
 *
 *
 * Usage notes
 * <ul>
 * <li>You can verify that the filter is being applied with Firefox's Web
 * Developer Extension. Click Tools > Web Developer > Information > View
 * Response Headers.
 * <li>If you are running an Apache httpd/Jk/Tomcat server configuration you
 * need to ensure that Tomcat is serving HTML files, otherwise the filter will
 * not be applied.
 * <li>One reason that this filter exists is that you cannot use <code>*.nocache.html</code> or
 * <code>*.cache.html</code> for url patterns. According to the 2.3 servlet
 * spec, an extension is defined as the characters after the <strong>last</strong>
 * period.
 * <li>The header is modified <em>before</em> passing control down the filter chain.
 * </ul>
 *
 * @see <a
 * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9">Cache-control
 * directive</a>
 * @see <a
 * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21">Expires
 * directive</a>
 * @see <a
 * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.32">Pragma
 * directive</a>
 * @see Pattern
 */
public class GWTCacheFilter implements Filter {
    /**
     * The name of the filter init-param which specifies files not to cache.
     * The name is <code>{@value}</code>.
     */
    public static final String INITPARAM_FORCEDONTCACHE = "forceDontCache";

    /**
     * The name of the filter init-param which specifies files to cache.
     * The name is <code>{@value}</code>.
     */
    public static final String INITPARAM_FORCECACHE = "forceCache";

    /**
     * The default value of the <code>forceCache</code> init-param.
     * The value is <code>{@value}</code>.
     */
    public static final String DEFAULT_FORCEDONTCACHE = ".+\\.nocache\\..+";

    /**
     * The default value of the <code>forceDontCache</code> init-param.
     * The value is <code>{@value}</code>.
     */
    public static final String DEFAULT_FORCECACHE = ".+\\.cache\\..+";

    private Pattern forceDontCachePattern;
    private Pattern forceCachePattern;

    /*
     * (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    @Override
    public void init( FilterConfig filterConfig )
            throws ServletException {
        String forceDontCachePatternString = filterConfig.getInitParameter( INITPARAM_FORCEDONTCACHE );
        String forceCachePatternString = filterConfig.getInitParameter( INITPARAM_FORCECACHE );
        if ( forceDontCachePatternString == null ) {
            forceDontCachePatternString = DEFAULT_FORCEDONTCACHE;
        }
        if ( forceCachePatternString == null ) {
            forceCachePatternString = DEFAULT_FORCECACHE;
        }
        forceDontCachePattern = Pattern.compile( forceDontCachePatternString );
        forceCachePattern = Pattern.compile( forceCachePatternString );
    }

    /*
     * (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    @Override
    public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
            throws IOException, ServletException {
        if ( request instanceof HttpServletRequest ) {
            HttpServletRequest hRequest = (HttpServletRequest) request;
            if ( forceDontCachePattern.matcher( hRequest.getRequestURL() ).matches() ) {
                HttpServletResponse hResponse = (HttpServletResponse) response;
                hResponse.setHeader( "Cache-Control", "no-cache no-store must-revalidate" );
                hResponse.setHeader( "Pragma", "no-cache" ); // HTTP/1.0
                hResponse.setDateHeader( "Expires", 86400000 ); // January 2, 1970
            } else if ( forceCachePattern.matcher( hRequest.getRequestURL() ).matches() ) {
                HttpServletResponse hresponse = (HttpServletResponse) response;

                // the w3c spec requires a maximum age of 1 year
                // Firefox 3+ needs 'public' to cache this resource when received via SSL
                hresponse.setHeader( "Cache-Control", "public max-age=31536000" );

                // necessary to overwrite "Pragma: no-cache" header
                hresponse.setHeader( "Pragma", "temp" );
                hresponse.setHeader( "Pragma", "" );
                hresponse.setDateHeader( "Expires", System.currentTimeMillis() + 31536000000l );
            }
        }
        chain.doFilter( request, response );
    }

    /*
     * (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {
    }
}

Commits for litesoft/trunk/Java/GWT/Server/src/org/litesoft/GWT/server/GWTCacheFilter.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

25 Diff Diff GeorgeS picture GeorgeS Mon 01 Mar, 2010 14:09:51 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000