WebContentInterceptor.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:6k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.web.servlet.mvc;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import java.util.Properties;
  21. import javax.servlet.ServletException;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import org.springframework.util.PathMatcher;
  25. import org.springframework.web.servlet.HandlerInterceptor;
  26. import org.springframework.web.servlet.ModelAndView;
  27. import org.springframework.web.servlet.support.WebContentGenerator;
  28. import org.springframework.web.util.UrlPathHelper;
  29. /**
  30.  * Interceptor that checks and prepares request and response. Checks for supported
  31.  * methods and a required session, and applies the specified number of cache seconds.
  32.  * See superclass bean properties for configuration options.
  33.  *
  34.  * <p>All the settings supported by this interceptor can also be set on AbstractController.
  35.  * This interceptor is mainly intended for applying checks and preparations to a set of
  36.  * controllers mapped by a HandlerMapping.
  37.  *
  38.  * @author Juergen Hoeller
  39.  * @since 27.11.2003
  40.  * @see AbstractController
  41.  */
  42. public class WebContentInterceptor extends WebContentGenerator implements HandlerInterceptor {
  43. private UrlPathHelper urlPathHelper = new UrlPathHelper();
  44. private Map cacheMappings = new HashMap();
  45. /**
  46.  * Set if URL lookup should always use full path within current servlet
  47.  * context. Else, the path within the current servlet mapping is used
  48.  * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
  49.  * Default is false.
  50.  * <p>Only relevant for the "cacheMappings" setting.
  51.  * @see #setCacheMappings
  52.  * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
  53.  */
  54. public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
  55. this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
  56. }
  57. /**
  58.  * Set if context path and request URI should be URL-decoded.
  59.  * Both are returned <i>undecoded</i> by the Servlet API,
  60.  * in contrast to the servlet path.
  61.  * <p>Uses either the request encoding or the default encoding according
  62.  * to the Servlet spec (ISO-8859-1).
  63.  * <p>Note: Setting this to true requires J2SE 1.4, as J2SE 1.3's
  64.  * URLDecoder class does not offer a way to specify the encoding.
  65.  * <p>Only relevant for the "cacheMappings" setting.
  66.  * @see #setCacheMappings
  67.  * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
  68.  */
  69. public void setUrlDecode(boolean urlDecode) {
  70. this.urlPathHelper.setUrlDecode(urlDecode);
  71. }
  72. /**
  73.  * Set the UrlPathHelper to use for resolution of lookup paths.
  74.  * <p>Use this to override the default UrlPathHelper with a custom subclass,
  75.  * or to share common UrlPathHelper settings across multiple HandlerMappings
  76.  * and MethodNameResolvers.
  77.  * <p>Only relevant for the "cacheMappings" setting.
  78.  * @see #setCacheMappings
  79.  * @see org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#setUrlPathHelper
  80.  * @see org.springframework.web.servlet.mvc.multiaction.AbstractUrlMethodNameResolver#setUrlPathHelper
  81.  */
  82. public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
  83. this.urlPathHelper = urlPathHelper;
  84. }
  85. /**
  86.  * Map specific URL paths to specific cache seconds.
  87.  * <p>Overrides the default cache seconds setting of this interceptor.
  88.  * Can specify "-1" to exclude an URL path from default caching.
  89.  * @param cacheMappings a mapping between URL paths (as keys) and
  90.  * cache seconds (as values, need to be integer-parsable)
  91.  * @see #setCacheSeconds
  92.  */
  93. public void setCacheMappings(Properties cacheMappings) {
  94. this.cacheMappings.clear();
  95. for (Iterator it = cacheMappings.keySet().iterator(); it.hasNext();) {
  96. String path = (String) it.next();
  97. this.cacheMappings.put(path, Integer.valueOf(cacheMappings.getProperty(path)));
  98. }
  99. }
  100. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
  101.     throws ServletException {
  102. String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
  103. if (logger.isDebugEnabled()) {
  104. logger.debug("Looking up cache seconds for [" + lookupPath + "]");
  105. }
  106. Integer cacheSeconds = lookupCacheSeconds(lookupPath);
  107. if (cacheSeconds != null) {
  108. if (logger.isDebugEnabled()) {
  109. logger.debug("Applying " + cacheSeconds + " cache seconds to [" + lookupPath + "]");
  110. }
  111. checkAndPrepare(request, response, cacheSeconds.intValue(), handler instanceof LastModified);
  112. }
  113. else {
  114. if (logger.isDebugEnabled()) {
  115. logger.debug("Applying default cache seconds to [" + lookupPath + "]");
  116. }
  117. checkAndPrepare(request, response, handler instanceof LastModified);
  118. }
  119. return true;
  120. }
  121. /**
  122.  * Look up a cache seconds value for the given URL path.
  123.  * <p>Supports direct matches, e.g. a registered "/test" matches "/test",
  124.  * and various Ant-style pattern matches, e.g. a registered "/t*" matches
  125.  * both "/test" and "/team". For details, see the PathMatcher class.
  126.  * @param urlPath URL the bean is mapped to
  127.  * @return the associated cache seconds, or null if not found
  128.  * @see org.springframework.util.PathMatcher
  129.  */
  130. protected Integer lookupCacheSeconds(String urlPath) {
  131. // direct match?
  132. Integer cacheSeconds = (Integer) this.cacheMappings.get(urlPath);
  133. if (cacheSeconds == null) {
  134. // pattern match?
  135. for (Iterator it = this.cacheMappings.keySet().iterator(); it.hasNext();) {
  136. String registeredPath = (String) it.next();
  137. if (PathMatcher.match(registeredPath, urlPath)) {
  138. cacheSeconds = (Integer) this.cacheMappings.get(registeredPath);
  139. }
  140. }
  141. }
  142. return cacheSeconds;
  143. }
  144. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
  145.     ModelAndView modelAndView) {
  146. }
  147. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
  148.     Exception ex) {
  149. }
  150. }