/** * Check and prepare the given request and response according to the settings * of this generator. * @see #checkRequest(HttpServletRequest) * @see #prepareResponse(HttpServletResponse) * @deprecated as of 4.2, since the {@code lastModified} flag is effectively ignored, * with a must-revalidate header only generated if explicitly configured */ @Deprecated protected final void checkAndPrepare( HttpServletRequest request, HttpServletResponse response, boolean lastModified) throws ServletException { checkRequest(request); prepareResponse(response); }
/** * Create a new WebContentGenerator. * @param supportedMethods the supported HTTP methods for this content generator */ public WebContentGenerator(String... supportedMethods) { setSupportedMethods(supportedMethods); }
/** * Prepare the given response according to the settings of this generator. * Applies the number of cache seconds specified for this generator. * @param response current HTTP response * @since 4.2 */ protected final void prepareResponse(HttpServletResponse response) { if (this.cacheControl != null) { applyCacheControl(response, this.cacheControl); } else { applyCacheSeconds(response, this.cacheSeconds); } if (this.varyByRequestHeaders != null) { for (String value : getVaryRequestHeadersToAdd(response, this.varyByRequestHeaders)) { response.addHeader("Vary", value); } } }
/** * Check and prepare the given request and response according to the settings * of this generator. * @see #checkRequest(HttpServletRequest) * @see #applyCacheSeconds(HttpServletResponse, int) * @deprecated as of 4.2, since the {@code lastModified} flag is effectively ignored, * with a must-revalidate header only generated if explicitly configured */ @Deprecated protected final void checkAndPrepare( HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException { checkRequest(request); applyCacheSeconds(response, cacheSeconds); }
cacheForSeconds(response, cacheSeconds); preventCaching(response); cControl = CacheControl.empty(); applyCacheControl(response, cControl);
/** * Apply the given cache seconds and generate respective HTTP headers. * <p>That is, allow caching for the given number of seconds in the * case of a positive value, prevent caching if given a 0 value, else * do nothing (i.e. leave caching to the client). * @param response the current HTTP response * @param cacheSeconds the (positive) number of seconds into the future * that the response should be cacheable for; 0 to prevent caching; and * a negative value to leave caching to the client. * @param mustRevalidate whether the client should revalidate the resource * (typically only necessary for controllers with last-modified support) * @deprecated as of 4.2, in favor of {@link #applyCacheControl} */ @Deprecated protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate) { if (cacheSeconds > 0) { cacheForSeconds(response, cacheSeconds, mustRevalidate); } else if (cacheSeconds == 0) { preventCaching(response); } }
/** * @see #checkRequest(HttpServletRequest) * @see #applyCacheSeconds(HttpServletResponse, int) * @deprecated as of 4.2, since the {@code lastModified} flag is effectively ignored, * with a must-revalidate header only generated if explicitly configured */ @Deprecated protected final void checkAndPrepare( HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException { checkRequest(request); applyCacheSeconds(response, cacheSeconds); }
/** * Set the HTTP methods that this content generator should support. * <p>Default is GET, HEAD and POST for simple form controller types; * unrestricted for general controllers and interceptors. */ public final void setSupportedMethods(@Nullable String... methods) { if (!ObjectUtils.isEmpty(methods)) { this.supportedMethods = new LinkedHashSet<>(Arrays.asList(methods)); } else { this.supportedMethods = null; } initAllowHeader(); }
@Test public void getAllowHeaderWithSupportedMethodsSetter() throws Exception { WebContentGenerator generator = new TestWebContentGenerator(); generator.setSupportedMethods("POST"); assertEquals("POST,OPTIONS", generator.getAllowHeader()); }
private void testVaryHeader(String[] configuredValues, String[] responseValues, String[] expected) { WebContentGenerator generator = new TestWebContentGenerator(); generator.setVaryByRequestHeaders(configuredValues); MockHttpServletResponse response = new MockHttpServletResponse(); for (String value : responseValues) { response.addHeader("Vary", value); } generator.prepareResponse(response); assertEquals(Arrays.asList(expected), response.getHeaderValues("Vary")); }
/** * Set HTTP headers to allow caching for the given number of seconds. * Does not tell the browser to revalidate the resource. * @param response current HTTP response * @param seconds number of seconds into the future that the response * should be cacheable for * @deprecated as of 4.2, in favor of {@link #applyCacheControl} */ @Deprecated protected final void cacheForSeconds(HttpServletResponse response, int seconds) { cacheForSeconds(response, seconds, false); }
@Test public void varyHeaderNone() throws Exception { WebContentGenerator generator = new TestWebContentGenerator(); MockHttpServletResponse response = new MockHttpServletResponse(); generator.prepareResponse(response); assertNull(response.getHeader("Vary")); }
cacheForSeconds(response, cacheSeconds); preventCaching(response); cControl = CacheControl.empty(); applyCacheControl(response, cControl);
/** * Apply the given cache seconds and generate respective HTTP headers. * <p>That is, allow caching for the given number of seconds in the * case of a positive value, prevent caching if given a 0 value, else * do nothing (i.e. leave caching to the client). * @param response the current HTTP response * @param cacheSeconds the (positive) number of seconds into the future * that the response should be cacheable for; 0 to prevent caching; and * a negative value to leave caching to the client. * @param mustRevalidate whether the client should revalidate the resource * (typically only necessary for controllers with last-modified support) * @deprecated as of 4.2, in favor of {@link #applyCacheControl} */ @Deprecated protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate) { if (cacheSeconds > 0) { cacheForSeconds(response, cacheSeconds, mustRevalidate); } else if (cacheSeconds == 0) { preventCaching(response); } }
/** * Check and prepare the given request and response according to the settings * of this generator. * @see #checkRequest(HttpServletRequest) * @see #applyCacheSeconds(HttpServletResponse, int) * @deprecated as of 4.2, since the {@code lastModified} flag is effectively ignored, * with a must-revalidate header only generated if explicitly configured */ @Deprecated protected final void checkAndPrepare( HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException { checkRequest(request); applyCacheSeconds(response, cacheSeconds); }
/** * Create a new WebContentGenerator. * @param restrictDefaultSupportedMethods {@code true} if this * generator should support HTTP methods GET, HEAD and POST by default, * or {@code false} if it should be unrestricted */ public WebContentGenerator(boolean restrictDefaultSupportedMethods) { if (restrictDefaultSupportedMethods) { this.supportedMethods = new LinkedHashSet<>(4); this.supportedMethods.add(METHOD_GET); this.supportedMethods.add(METHOD_HEAD); this.supportedMethods.add(METHOD_POST); } initAllowHeader(); }
@Test public void getAllowHeaderWithSupportedMethodsSetterEmpty() throws Exception { WebContentGenerator generator = new TestWebContentGenerator(); generator.setSupportedMethods(); assertEquals("Effectively \"no restriction\" on supported methods", "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS", generator.getAllowHeader()); }
/** * Set HTTP headers to allow caching for the given number of seconds. * Does not tell the browser to revalidate the resource. * @param response current HTTP response * @param seconds number of seconds into the future that the response * should be cacheable for * @deprecated as of 4.2, in favor of {@link #applyCacheControl} */ @Deprecated protected final void cacheForSeconds(HttpServletResponse response, int seconds) { cacheForSeconds(response, seconds, false); }
/** * Prepare the given response according to the settings of this generator. * Applies the number of cache seconds specified for this generator. * @param response current HTTP response * @since 4.2 */ protected final void prepareResponse(HttpServletResponse response) { if (this.cacheControl != null) { applyCacheControl(response, this.cacheControl); } else { applyCacheSeconds(response, this.cacheSeconds); } if (this.varyByRequestHeaders != null) { for (String value : getVaryRequestHeadersToAdd(response, this.varyByRequestHeaders)) { response.addHeader("Vary", value); } } }
cacheForSeconds(response, cacheSeconds); preventCaching(response); cControl = CacheControl.empty(); applyCacheControl(response, cControl);