1 16 17 package org.springframework.web.servlet.support; 18 19 import java.util.HashSet ; 20 import java.util.Set ; 21 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.springframework.util.StringUtils; 27 import org.springframework.web.HttpRequestMethodNotSupportedException; 28 import org.springframework.web.HttpSessionRequiredException; 29 import org.springframework.web.context.support.WebApplicationObjectSupport; 30 31 48 public abstract class WebContentGenerator extends WebApplicationObjectSupport { 49 50 51 public static final String METHOD_HEAD = "HEAD"; 52 53 54 public static final String METHOD_GET = "GET"; 55 56 57 public static final String METHOD_POST = "POST"; 58 59 60 private static final String HEADER_PRAGMA = "Pragma"; 61 62 private static final String HEADER_EXPIRES = "Expires"; 63 64 private static final String HEADER_CACHE_CONTROL = "Cache-Control"; 65 66 67 68 private Set supportedMethods = new HashSet (); 69 70 private boolean requireSession = false; 71 72 73 private boolean useExpiresHeader = true; 74 75 76 private boolean useCacheControlHeader = true; 77 78 private int cacheSeconds = -1; 79 80 81 public WebContentGenerator() { 82 this.supportedMethods.add(METHOD_HEAD); 83 this.supportedMethods.add(METHOD_GET); 84 this.supportedMethods.add(METHOD_POST); 85 } 86 87 91 public final void setSupportedMethods(String [] methods) { 92 if (methods == null || methods.length == 0) { 93 throw new IllegalArgumentException ("supportedMethods must not be empty"); 94 } 95 this.supportedMethods.clear(); 96 for (int i = 0; i < methods.length; i++) { 97 this.supportedMethods.add(methods[i]); 98 } 99 } 100 101 104 public final String [] getSupportedMethods() { 105 return StringUtils.toStringArray(this.supportedMethods); 106 } 107 108 111 public final void setRequireSession(boolean requireSession) { 112 this.requireSession = requireSession; 113 } 114 115 118 public final boolean isRequireSession() { 119 return requireSession; 120 } 121 122 127 public final void setUseExpiresHeader(boolean useExpiresHeader) { 128 this.useExpiresHeader = useExpiresHeader; 129 } 130 131 134 public final boolean isUseExpiresHeader() { 135 return useExpiresHeader; 136 } 137 138 143 public final void setUseCacheControlHeader(boolean useCacheControlHeader) { 144 this.useCacheControlHeader = useCacheControlHeader; 145 } 146 147 150 public final boolean isUseCacheControlHeader() { 151 return useCacheControlHeader; 152 } 153 154 161 public final void setCacheSeconds(int seconds) { 162 this.cacheSeconds = seconds; 163 } 164 165 168 public final int getCacheSeconds() { 169 return cacheSeconds; 170 } 171 172 173 182 protected final void checkAndPrepare( 183 HttpServletRequest request, HttpServletResponse response, boolean lastModified) 184 throws ServletException { 185 186 checkAndPrepare(request, response, this.cacheSeconds, lastModified); 187 } 188 189 200 protected final void checkAndPrepare( 201 HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) 202 throws ServletException { 203 204 String method = request.getMethod(); 206 if (!this.supportedMethods.contains(method)) { 207 throw new HttpRequestMethodNotSupportedException(method); 208 } 209 210 if (this.requireSession) { 212 if (request.getSession(false) == null) { 213 throw new HttpSessionRequiredException("Pre-existing session required but none found"); 214 } 215 } 216 217 applyCacheSeconds(response, cacheSeconds, lastModified); 220 } 221 222 226 protected final void preventCaching(HttpServletResponse response) { 227 response.setHeader(HEADER_PRAGMA, "No-cache"); 228 if (this.useExpiresHeader) { 229 response.setDateHeader(HEADER_EXPIRES, 1L); 231 } 232 if (this.useCacheControlHeader) { 233 response.setHeader(HEADER_CACHE_CONTROL, "no-cache"); 236 response.addHeader(HEADER_CACHE_CONTROL, "no-store"); 237 } 238 } 239 240 248 protected final void cacheForSeconds(HttpServletResponse response, int seconds) { 249 cacheForSeconds(response, seconds, false); 250 } 251 252 262 protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) { 263 if (this.useExpiresHeader) { 264 response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis() + seconds * 1000L); 266 } 267 if (this.useCacheControlHeader) { 268 String headerValue = "max-age=" + seconds; 270 if (mustRevalidate) { 271 headerValue += ", must-revalidate"; 272 } 273 response.setHeader(HEADER_CACHE_CONTROL, headerValue); 274 } 275 } 276 277 287 protected final void applyCacheSeconds(HttpServletResponse response, int seconds) { 288 applyCacheSeconds(response, seconds, false); 289 } 290 291 303 protected final void applyCacheSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) { 304 if (seconds > 0) { 305 cacheForSeconds(response, seconds, mustRevalidate); 306 } 307 else if (seconds == 0) { 308 preventCaching(response); 309 } 310 } 312 313 } 314 | Popular Tags |