|
@@ -1,170 +1,170 @@ |
1 |
|
- |
// This Source Code is Copyright & Licenced as indicated below |
2 |
|
- |
package org.litesoft.GWT.server; |
3 |
|
- |
/* |
4 |
|
- |
* Copyright 2006 Mat Gessel <mat.gessel@gmail.com> |
5 |
|
- |
* |
6 |
|
- |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7 |
|
- |
* use this file except in compliance with the License. You may obtain a copy of |
8 |
|
- |
* the License at |
9 |
|
- |
* |
10 |
|
- |
* http://www.apache.org/licenses/LICENSE-2.0 |
11 |
|
- |
* |
12 |
|
- |
* Unless required by applicable law or agreed to in writing, software |
13 |
|
- |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14 |
|
- |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15 |
|
- |
* License for the specific language governing permissions and limitations under |
16 |
|
- |
* the License. |
17 |
|
- |
*/ |
18 |
|
- |
|
19 |
|
- |
import javax.servlet.*; |
20 |
|
- |
import javax.servlet.http.*; |
21 |
|
- |
import java.io.*; |
22 |
|
- |
import java.util.regex.*; |
23 |
|
- |
|
24 |
|
- |
/** |
25 |
|
- |
* This is a filter which enforces proper caching of the generated GWT script |
26 |
|
- |
* files. It requires that you serve your GWT application via a Java servlet |
27 |
|
- |
* container. |
28 |
|
- |
* <p/> |
29 |
|
- |
* To use, add the jar to <code>WEB-INF/lib</code> and add the |
30 |
|
- |
* following to your deployment descriptor (web.xml): |
31 |
|
- |
* <p/> |
32 |
|
- |
* <pre> |
33 |
|
- |
* <filter> |
34 |
|
- |
* <filter-name>GWTCacheFilter</filter-name> |
35 |
|
- |
* <filter-class>asquare.gwt.tk.server.GWTCacheFilter</filter-class> |
36 |
|
- |
* <description>Enforces proper caching of GWT files</description> |
37 |
|
- |
* </filter> |
38 |
|
- |
* |
39 |
|
- |
* <filter-mapping> |
40 |
|
- |
* <filter-name>GWTCacheFilter</filter-name> |
41 |
|
- |
* <url-pattern>/*</url-pattern> |
42 |
|
- |
* </filter-mapping></pre> |
43 |
|
- |
* |
44 |
|
- |
* By default, files ending in <code>.cache.*</code> are cached and files |
45 |
|
- |
* ending in <code>.nocache.*</code> are not cached. You can override the |
46 |
|
- |
* defaults by specifying file name patterns in filter init-params. The pattern |
47 |
|
- |
* is parsed as a JDK regular expression. The defaults are below: |
48 |
|
- |
* |
49 |
|
- |
* <pre> |
50 |
|
- |
* <init-param> |
51 |
|
- |
* <param-name>forceDontCache</param-name> |
52 |
|
- |
* <param-value>.+\.nocache\..+</param-value> |
53 |
|
- |
* </init-param> |
54 |
|
- |
* <init-param> |
55 |
|
- |
* <param-name>forceCache</param-name> |
56 |
|
- |
* <param-value>.+\.cache\..+</param-value> |
57 |
|
- |
* </init-param></pre> |
58 |
|
- |
* |
59 |
|
- |
* |
60 |
|
- |
* Usage notes |
61 |
|
- |
* <ul> |
62 |
|
- |
* <li>You can verify that the filter is being applied with Firefox's Web |
63 |
|
- |
* Developer Extension. Click Tools > Web Developer > Information > View |
64 |
|
- |
* Response Headers. |
65 |
|
- |
* <li>If you are running an Apache httpd/Jk/Tomcat server configuration you |
66 |
|
- |
* need to ensure that Tomcat is serving HTML files, otherwise the filter will |
67 |
|
- |
* not be applied. |
68 |
|
- |
* <li>One reason that this filter exists is that you cannot use <code>*.nocache.html</code> or |
69 |
|
- |
* <code>*.cache.html</code> for url patterns. According to the 2.3 servlet |
70 |
|
- |
* spec, an extension is defined as the characters after the <strong>last</strong> |
71 |
|
- |
* period. |
72 |
|
- |
* <li>The header is modified <em>before</em> passing control down the filter chain. |
73 |
|
- |
* </ul> |
74 |
|
- |
* |
75 |
|
- |
* @see <a |
76 |
|
- |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9">Cache-control |
77 |
|
- |
* directive</a> |
78 |
|
- |
* @see <a |
79 |
|
- |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21">Expires |
80 |
|
- |
* directive</a> |
81 |
|
- |
* @see <a |
82 |
|
- |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.32">Pragma |
83 |
|
- |
* directive</a> |
84 |
|
- |
* @see Pattern |
85 |
|
- |
*/ |
86 |
|
- |
public class GWTCacheFilter implements Filter { |
87 |
|
- |
/** |
88 |
|
- |
* The name of the filter init-param which specifies files not to cache. |
89 |
|
- |
* The name is <code>{@value}</code>. |
90 |
|
- |
*/ |
91 |
|
- |
public static final String INITPARAM_FORCEDONTCACHE = "forceDontCache"; |
92 |
|
- |
|
93 |
|
- |
/** |
94 |
|
- |
* The name of the filter init-param which specifies files to cache. |
95 |
|
- |
* The name is <code>{@value}</code>. |
96 |
|
- |
*/ |
97 |
|
- |
public static final String INITPARAM_FORCECACHE = "forceCache"; |
98 |
|
- |
|
99 |
|
- |
/** |
100 |
|
- |
* The default value of the <code>forceCache</code> init-param. |
101 |
|
- |
* The value is <code>{@value}</code>. |
102 |
|
- |
*/ |
103 |
|
- |
public static final String DEFAULT_FORCEDONTCACHE = ".+\\.nocache\\..+"; |
104 |
|
- |
|
105 |
|
- |
/** |
106 |
|
- |
* The default value of the <code>forceDontCache</code> init-param. |
107 |
|
- |
* The value is <code>{@value}</code>. |
108 |
|
- |
*/ |
109 |
|
- |
public static final String DEFAULT_FORCECACHE = ".+\\.cache\\..+"; |
110 |
|
- |
|
111 |
|
- |
private Pattern forceDontCachePattern; |
112 |
|
- |
private Pattern forceCachePattern; |
113 |
|
- |
|
114 |
|
- |
/* |
115 |
|
- |
* (non-Javadoc) |
116 |
|
- |
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig) |
117 |
|
- |
*/ |
118 |
|
- |
@Override |
119 |
|
- |
public void init( FilterConfig filterConfig ) |
120 |
|
- |
throws ServletException { |
121 |
|
- |
String forceDontCachePatternString = filterConfig.getInitParameter( INITPARAM_FORCEDONTCACHE ); |
122 |
|
- |
String forceCachePatternString = filterConfig.getInitParameter( INITPARAM_FORCECACHE ); |
123 |
|
- |
if ( forceDontCachePatternString == null ) { |
124 |
|
- |
forceDontCachePatternString = DEFAULT_FORCEDONTCACHE; |
125 |
|
- |
} |
126 |
|
- |
if ( forceCachePatternString == null ) { |
127 |
|
- |
forceCachePatternString = DEFAULT_FORCECACHE; |
128 |
|
- |
} |
129 |
|
- |
forceDontCachePattern = Pattern.compile( forceDontCachePatternString ); |
130 |
|
- |
forceCachePattern = Pattern.compile( forceCachePatternString ); |
131 |
|
- |
} |
132 |
|
- |
|
133 |
|
- |
/* |
134 |
|
- |
* (non-Javadoc) |
135 |
|
- |
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) |
136 |
|
- |
*/ |
137 |
|
- |
@Override |
138 |
|
- |
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) |
139 |
|
- |
throws IOException, ServletException { |
140 |
|
- |
if ( request instanceof HttpServletRequest ) { |
141 |
|
- |
HttpServletRequest hRequest = (HttpServletRequest) request; |
142 |
|
- |
if ( forceDontCachePattern.matcher( hRequest.getRequestURL() ).matches() ) { |
143 |
|
- |
HttpServletResponse hResponse = (HttpServletResponse) response; |
144 |
|
- |
hResponse.setHeader( "Cache-Control", "no-cache no-store must-revalidate" ); |
145 |
|
- |
hResponse.setHeader( "Pragma", "no-cache" ); // HTTP/1.0 |
146 |
|
- |
hResponse.setDateHeader( "Expires", 86400000 ); // January 2, 1970 |
147 |
|
- |
} else if ( forceCachePattern.matcher( hRequest.getRequestURL() ).matches() ) { |
148 |
|
- |
HttpServletResponse hresponse = (HttpServletResponse) response; |
149 |
|
- |
|
150 |
|
- |
// the w3c spec requires a maximum age of 1 year |
151 |
|
- |
// Firefox 3+ needs 'public' to cache this resource when received via SSL |
152 |
|
- |
hresponse.setHeader( "Cache-Control", "public max-age=31536000" ); |
153 |
|
- |
|
154 |
|
- |
// necessary to overwrite "Pragma: no-cache" header |
155 |
|
- |
hresponse.setHeader( "Pragma", "temp" ); |
156 |
|
- |
hresponse.setHeader( "Pragma", "" ); |
157 |
|
- |
hresponse.setDateHeader( "Expires", System.currentTimeMillis() + 31536000000l ); |
158 |
|
- |
} |
159 |
|
- |
} |
160 |
|
- |
chain.doFilter( request, response ); |
161 |
|
- |
} |
162 |
|
- |
|
163 |
|
- |
/* |
164 |
|
- |
* (non-Javadoc) |
165 |
|
- |
* @see javax.servlet.Filter#destroy() |
166 |
|
- |
*/ |
167 |
|
- |
@Override |
168 |
|
- |
public void destroy() { |
169 |
|
- |
} |
170 |
|
- |
} |
|
1 |
+ |
// This Source Code is Copyright & Licenced as indicated below |
|
2 |
+ |
package org.litesoft.GWT.server; |
|
3 |
+ |
/* |
|
4 |
+ |
* Copyright 2006 Mat Gessel <mat.gessel@gmail.com> |
|
5 |
+ |
* |
|
6 |
+ |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7 |
+ |
* use this file except in compliance with the License. You may obtain a copy of |
|
8 |
+ |
* the License at |
|
9 |
+ |
* |
|
10 |
+ |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11 |
+ |
* |
|
12 |
+ |
* Unless required by applicable law or agreed to in writing, software |
|
13 |
+ |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14 |
+ |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15 |
+ |
* License for the specific language governing permissions and limitations under |
|
16 |
+ |
* the License. |
|
17 |
+ |
*/ |
|
18 |
+ |
|
|
19 |
+ |
import javax.servlet.*; |
|
20 |
+ |
import javax.servlet.http.*; |
|
21 |
+ |
import java.io.*; |
|
22 |
+ |
import java.util.regex.*; |
|
23 |
+ |
|
|
24 |
+ |
/** |
|
25 |
+ |
* This is a filter which enforces proper caching of the generated GWT script |
|
26 |
+ |
* files. It requires that you serve your GWT application via a Java servlet |
|
27 |
+ |
* container. |
|
28 |
+ |
* <p/> |
|
29 |
+ |
* To use, add the jar to <code>WEB-INF/lib</code> and add the |
|
30 |
+ |
* following to your deployment descriptor (web.xml): |
|
31 |
+ |
* <p/> |
|
32 |
+ |
* <pre> |
|
33 |
+ |
* <filter> |
|
34 |
+ |
* <filter-name>GWTCacheFilter</filter-name> |
|
35 |
+ |
* <filter-class>asquare.gwt.tk.server.GWTCacheFilter</filter-class> |
|
36 |
+ |
* <description>Enforces proper caching of GWT files</description> |
|
37 |
+ |
* </filter> |
|
38 |
+ |
* |
|
39 |
+ |
* <filter-mapping> |
|
40 |
+ |
* <filter-name>GWTCacheFilter</filter-name> |
|
41 |
+ |
* <url-pattern>/*</url-pattern> |
|
42 |
+ |
* </filter-mapping></pre> |
|
43 |
+ |
* |
|
44 |
+ |
* By default, files ending in <code>.cache.*</code> are cached and files |
|
45 |
+ |
* ending in <code>.nocache.*</code> are not cached. You can override the |
|
46 |
+ |
* defaults by specifying file name patterns in filter init-params. The pattern |
|
47 |
+ |
* is parsed as a JDK regular expression. The defaults are below: |
|
48 |
+ |
* |
|
49 |
+ |
* <pre> |
|
50 |
+ |
* <init-param> |
|
51 |
+ |
* <param-name>forceDontCache</param-name> |
|
52 |
+ |
* <param-value>.+\.nocache\..+</param-value> |
|
53 |
+ |
* </init-param> |
|
54 |
+ |
* <init-param> |
|
55 |
+ |
* <param-name>forceCache</param-name> |
|
56 |
+ |
* <param-value>.+\.cache\..+</param-value> |
|
57 |
+ |
* </init-param></pre> |
|
58 |
+ |
* |
|
59 |
+ |
* |
|
60 |
+ |
* Usage notes |
|
61 |
+ |
* <ul> |
|
62 |
+ |
* <li>You can verify that the filter is being applied with Firefox's Web |
|
63 |
+ |
* Developer Extension. Click Tools > Web Developer > Information > View |
|
64 |
+ |
* Response Headers. |
|
65 |
+ |
* <li>If you are running an Apache httpd/Jk/Tomcat server configuration you |
|
66 |
+ |
* need to ensure that Tomcat is serving HTML files, otherwise the filter will |
|
67 |
+ |
* not be applied. |
|
68 |
+ |
* <li>One reason that this filter exists is that you cannot use <code>*.nocache.html</code> or |
|
69 |
+ |
* <code>*.cache.html</code> for url patterns. According to the 2.3 servlet |
|
70 |
+ |
* spec, an extension is defined as the characters after the <strong>last</strong> |
|
71 |
+ |
* period. |
|
72 |
+ |
* <li>The header is modified <em>before</em> passing control down the filter chain. |
|
73 |
+ |
* </ul> |
|
74 |
+ |
* |
|
75 |
+ |
* @see <a |
|
76 |
+ |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9">Cache-control |
|
77 |
+ |
* directive</a> |
|
78 |
+ |
* @see <a |
|
79 |
+ |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21">Expires |
|
80 |
+ |
* directive</a> |
|
81 |
+ |
* @see <a |
|
82 |
+ |
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.32">Pragma |
|
83 |
+ |
* directive</a> |
|
84 |
+ |
* @see Pattern |
|
85 |
+ |
*/ |
|
86 |
+ |
public class GWTCacheFilter implements Filter { |
|
87 |
+ |
/** |
|
88 |
+ |
* The name of the filter init-param which specifies files not to cache. |
|
89 |
+ |
* The name is <code>{@value}</code>. |
|
90 |
+ |
*/ |
|
91 |
+ |
public static final String INITPARAM_FORCEDONTCACHE = "forceDontCache"; |
|
92 |
+ |
|
|
93 |
+ |
/** |
|
94 |
+ |
* The name of the filter init-param which specifies files to cache. |
|
95 |
+ |
* The name is <code>{@value}</code>. |
|
96 |
+ |
*/ |
|
97 |
+ |
public static final String INITPARAM_FORCECACHE = "forceCache"; |
|
98 |
+ |
|
|
99 |
+ |
/** |
|
100 |
+ |
* The default value of the <code>forceCache</code> init-param. |
|
101 |
+ |
* The value is <code>{@value}</code>. |
|
102 |
+ |
*/ |
|
103 |
+ |
public static final String DEFAULT_FORCEDONTCACHE = ".+\\.nocache\\..+"; |
|
104 |
+ |
|
|
105 |
+ |
/** |
|
106 |
+ |
* The default value of the <code>forceDontCache</code> init-param. |
|
107 |
+ |
* The value is <code>{@value}</code>. |
|
108 |
+ |
*/ |
|
109 |
+ |
public static final String DEFAULT_FORCECACHE = ".+\\.cache\\..+"; |
|
110 |
+ |
|
|
111 |
+ |
private Pattern forceDontCachePattern; |
|
112 |
+ |
private Pattern forceCachePattern; |
|
113 |
+ |
|
|
114 |
+ |
/* |
|
115 |
+ |
* (non-Javadoc) |
|
116 |
+ |
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig) |
|
117 |
+ |
*/ |
|
118 |
+ |
@Override |
|
119 |
+ |
public void init( FilterConfig filterConfig ) |
|
120 |
+ |
throws ServletException { |
|
121 |
+ |
String forceDontCachePatternString = filterConfig.getInitParameter( INITPARAM_FORCEDONTCACHE ); |
|
122 |
+ |
String forceCachePatternString = filterConfig.getInitParameter( INITPARAM_FORCECACHE ); |
|
123 |
+ |
if ( forceDontCachePatternString == null ) { |
|
124 |
+ |
forceDontCachePatternString = DEFAULT_FORCEDONTCACHE; |
|
125 |
+ |
} |
|
126 |
+ |
if ( forceCachePatternString == null ) { |
|
127 |
+ |
forceCachePatternString = DEFAULT_FORCECACHE; |
|
128 |
+ |
} |
|
129 |
+ |
forceDontCachePattern = Pattern.compile( forceDontCachePatternString ); |
|
130 |
+ |
forceCachePattern = Pattern.compile( forceCachePatternString ); |
|
131 |
+ |
} |
|
132 |
+ |
|
|
133 |
+ |
/* |
|
134 |
+ |
* (non-Javadoc) |
|
135 |
+ |
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) |
|
136 |
+ |
*/ |
|
137 |
+ |
@Override |
|
138 |
+ |
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) |
|
139 |
+ |
throws IOException, ServletException { |
|
140 |
+ |
if ( request instanceof HttpServletRequest ) { |
|
141 |
+ |
HttpServletRequest hRequest = (HttpServletRequest) request; |
|
142 |
+ |
if ( forceDontCachePattern.matcher( hRequest.getRequestURL() ).matches() ) { |
|
143 |
+ |
HttpServletResponse hResponse = (HttpServletResponse) response; |
|
144 |
+ |
hResponse.setHeader( "Cache-Control", "no-cache no-store must-revalidate" ); |
|
145 |
+ |
hResponse.setHeader( "Pragma", "no-cache" ); // HTTP/1.0 |
|
146 |
+ |
hResponse.setDateHeader( "Expires", 86400000 ); // January 2, 1970 |
|
147 |
+ |
} else if ( forceCachePattern.matcher( hRequest.getRequestURL() ).matches() ) { |
|
148 |
+ |
HttpServletResponse hresponse = (HttpServletResponse) response; |
|
149 |
+ |
|
|
150 |
+ |
// the w3c spec requires a maximum age of 1 year |
|
151 |
+ |
// Firefox 3+ needs 'public' to cache this resource when received via SSL |
|
152 |
+ |
hresponse.setHeader( "Cache-Control", "public max-age=31536000" ); |
|
153 |
+ |
|
|
154 |
+ |
// necessary to overwrite "Pragma: no-cache" header |
|
155 |
+ |
hresponse.setHeader( "Pragma", "temp" ); |
|
156 |
+ |
hresponse.setHeader( "Pragma", "" ); |
|
157 |
+ |
hresponse.setDateHeader( "Expires", System.currentTimeMillis() + 31536000000l ); |
|
158 |
+ |
} |
|
159 |
+ |
} |
|
160 |
+ |
chain.doFilter( request, response ); |
|
161 |
+ |
} |
|
162 |
+ |
|
|
163 |
+ |
/* |
|
164 |
+ |
* (non-Javadoc) |
|
165 |
+ |
* @see javax.servlet.Filter#destroy() |
|
166 |
+ |
*/ |
|
167 |
+ |
@Override |
|
168 |
+ |
public void destroy() { |
|
169 |
+ |
} |
|
170 |
+ |
} |