1 16 17 package org.springframework.web.servlet.mvc; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 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 49 public class WebContentInterceptor extends WebContentGenerator implements HandlerInterceptor { 50 51 private UrlPathHelper urlPathHelper = new UrlPathHelper(); 52 53 private Map cacheMappings = new HashMap (); 54 55 private PathMatcher pathMatcher = new AntPathMatcher(); 56 57 58 67 public void setAlwaysUseFullPath(boolean alwaysUseFullPath) { 68 this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath); 69 } 70 71 84 public void setUrlDecode(boolean urlDecode) { 85 this.urlPathHelper.setUrlDecode(urlDecode); 86 } 87 88 98 public void setUrlPathHelper(UrlPathHelper urlPathHelper) { 99 this.urlPathHelper = urlPathHelper; 100 } 101 102 114 public void setCacheMappings(Properties cacheMappings) { 115 this.cacheMappings.clear(); 116 for (Iterator it = cacheMappings.keySet().iterator(); it.hasNext();) { 117 String path = (String ) it.next(); 118 this.cacheMappings.put(path, Integer.valueOf(cacheMappings.getProperty(path))); 119 } 120 } 121 122 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 request, HttpServletResponse response, Object handler) 136 throws ServletException { 137 138 String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); 139 if (logger.isDebugEnabled()) { 140 logger.debug("Looking up cache seconds for [" + lookupPath + "]"); 141 } 142 143 Integer 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 169 protected Integer lookupCacheSeconds(String urlPath) { 170 Integer cacheSeconds = (Integer ) this.cacheMappings.get(urlPath); 172 if (cacheSeconds == null) { 173 for (Iterator it = this.cacheMappings.keySet().iterator(); it.hasNext();) { 175 String registeredPath = (String ) it.next(); 176 if (this.pathMatcher.match(registeredPath, urlPath)) { 177 cacheSeconds = (Integer ) this.cacheMappings.get(registeredPath); 178 } 179 } 180 } 181 return cacheSeconds; 182 } 183 184 185 188 public void postHandle( 189 HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) 190 throws Exception { 191 } 192 193 196 public void afterCompletion( 197 HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 198 throws Exception { 199 } 200 201 } 202 | Popular Tags |