KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > WebContentInterceptor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.mvc;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.springframework.util.AntPathMatcher;
29 import org.springframework.util.Assert;
30 import org.springframework.util.PathMatcher;
31 import org.springframework.web.servlet.HandlerInterceptor;
32 import org.springframework.web.servlet.ModelAndView;
33 import org.springframework.web.servlet.support.WebContentGenerator;
34 import org.springframework.web.util.UrlPathHelper;
35
36 /**
37  * Interceptor that checks and prepares request and response. Checks for supported
38  * methods and a required session, and applies the specified number of cache seconds.
39  * See superclass bean properties for configuration options.
40  *
41  * <p>All the settings supported by this interceptor can also be set on AbstractController.
42  * This interceptor is mainly intended for applying checks and preparations to a set of
43  * controllers mapped by a HandlerMapping.
44  *
45  * @author Juergen Hoeller
46  * @since 27.11.2003
47  * @see AbstractController
48  */

49 public class WebContentInterceptor extends WebContentGenerator implements HandlerInterceptor {
50
51     private UrlPathHelper urlPathHelper = new UrlPathHelper();
52
53     private Map JavaDoc cacheMappings = new HashMap JavaDoc();
54
55     private PathMatcher pathMatcher = new AntPathMatcher();
56
57
58     /**
59      * Set if URL lookup should always use full path within current servlet
60      * context. Else, the path within the current servlet mapping is used
61      * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
62      * Default is "false".
63      * <p>Only relevant for the "cacheMappings" setting.
64      * @see #setCacheMappings
65      * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
66      */

67     public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
68         this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
69     }
70
71     /**
72      * Set if context path and request URI should be URL-decoded.
73      * Both are returned <i>undecoded</i> by the Servlet API,
74      * in contrast to the servlet path.
75      * <p>Uses either the request encoding or the default encoding according
76      * to the Servlet spec (ISO-8859-1).
77      * <p>Note: Setting this to "true" requires JDK 1.4 if the encoding differs
78      * from the VM's platform default encoding, as JDK 1.3's URLDecoder class
79      * does not offer a way to specify the encoding.
80      * <p>Only relevant for the "cacheMappings" setting.
81      * @see #setCacheMappings
82      * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
83      */

84     public void setUrlDecode(boolean urlDecode) {
85         this.urlPathHelper.setUrlDecode(urlDecode);
86     }
87
88     /**
89      * Set the UrlPathHelper to use for resolution of lookup paths.
90      * <p>Use this to override the default UrlPathHelper with a custom subclass,
91      * or to share common UrlPathHelper settings across multiple HandlerMappings
92      * and MethodNameResolvers.
93      * <p>Only relevant for the "cacheMappings" setting.
94      * @see #setCacheMappings
95      * @see org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#setUrlPathHelper
96      * @see org.springframework.web.servlet.mvc.multiaction.AbstractUrlMethodNameResolver#setUrlPathHelper
97      */

98     public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
99         this.urlPathHelper = urlPathHelper;
100     }
101
102     /**
103      * Map specific URL paths to specific cache seconds.
104      * <p>Overrides the default cache seconds setting of this interceptor.
105      * Can specify "-1" to exclude a URL path from default caching.
106      * <p>Supports direct matches, e.g. a registered "/test" matches "/test",
107      * and a various Ant-style pattern matches, e.g. a registered "/t*" matches
108      * both "/test" and "/team". For details, see the AntPathMatcher javadoc.
109      * @param cacheMappings a mapping between URL paths (as keys) and
110      * cache seconds (as values, need to be integer-parsable)
111      * @see #setCacheSeconds
112      * @see org.springframework.util.AntPathMatcher
113      */

114     public void setCacheMappings(Properties JavaDoc cacheMappings) {
115         this.cacheMappings.clear();
116         for (Iterator JavaDoc it = cacheMappings.keySet().iterator(); it.hasNext();) {
117             String JavaDoc path = (String JavaDoc) it.next();
118             this.cacheMappings.put(path, Integer.valueOf(cacheMappings.getProperty(path)));
119         }
120     }
121
122     /**
123      * Set the PathMatcher implementation to use for matching URL paths
124      * against registered URL patterns, for determining cache mappings.
125      * Default is AntPathMatcher.
126      * @see #setCacheMappings
127      * @see org.springframework.util.AntPathMatcher
128      */

129     public void setPathMatcher(PathMatcher pathMatcher) {
130         Assert.notNull(pathMatcher, "PathMatcher must not be null");
131         this.pathMatcher = pathMatcher;
132     }
133
134
135     public boolean preHandle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc handler)
136         throws ServletException JavaDoc {
137
138         String JavaDoc lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
139         if (logger.isDebugEnabled()) {
140             logger.debug("Looking up cache seconds for [" + lookupPath + "]");
141         }
142
143         Integer JavaDoc cacheSeconds = lookupCacheSeconds(lookupPath);
144         if (cacheSeconds != null) {
145             if (logger.isDebugEnabled()) {
146                 logger.debug("Applying " + cacheSeconds + " cache seconds to [" + lookupPath + "]");
147             }
148             checkAndPrepare(request, response, cacheSeconds.intValue(), handler instanceof LastModified);
149         }
150         else {
151             if (logger.isDebugEnabled()) {
152                 logger.debug("Applying default cache seconds to [" + lookupPath + "]");
153             }
154             checkAndPrepare(request, response, handler instanceof LastModified);
155         }
156
157         return true;
158     }
159
160     /**
161      * Look up a cache seconds value for the given URL path.
162      * <p>Supports direct matches, e.g. a registered "/test" matches "/test",
163      * and various Ant-style pattern matches, e.g. a registered "/t*" matches
164      * both "/test" and "/team". For details, see the AntPathMatcher class.
165      * @param urlPath URL the bean is mapped to
166      * @return the associated cache seconds, or <code>null</code> if not found
167      * @see org.springframework.util.AntPathMatcher
168      */

169     protected Integer JavaDoc lookupCacheSeconds(String JavaDoc urlPath) {
170         // direct match?
171
Integer JavaDoc cacheSeconds = (Integer JavaDoc) this.cacheMappings.get(urlPath);
172         if (cacheSeconds == null) {
173             // pattern match?
174
for (Iterator JavaDoc it = this.cacheMappings.keySet().iterator(); it.hasNext();) {
175                 String JavaDoc registeredPath = (String JavaDoc) it.next();
176                 if (this.pathMatcher.match(registeredPath, urlPath)) {
177                     cacheSeconds = (Integer JavaDoc) this.cacheMappings.get(registeredPath);
178                 }
179             }
180         }
181         return cacheSeconds;
182     }
183
184
185     /**
186      * This implementation is empty.
187      */

188     public void postHandle(
189             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc handler, ModelAndView modelAndView)
190             throws Exception JavaDoc {
191     }
192
193     /**
194      * This implementation is empty.
195      */

196     public void afterCompletion(
197             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc handler, Exception JavaDoc ex)
198             throws Exception JavaDoc {
199     }
200
201 }
202
Popular Tags