Search in sources :

Example 1 with CacheControl

use of org.springframework.http.CacheControl in project dhis2-core by dhis2.

the class ContextUtils method configureResponse.

public void configureResponse(HttpServletResponse response, String contentType, CacheStrategy cacheStrategy, String filename, boolean attachment) {
    CacheControl cacheControl;
    if (contentType != null) {
        response.setContentType(contentType);
    }
    if (CacheStrategy.RESPECT_SYSTEM_SETTING.equals(cacheStrategy)) {
        String strategy = trimToNull((String) systemSettingManager.getSystemSetting(SettingKey.CACHE_STRATEGY));
        cacheStrategy = strategy != null ? CacheStrategy.valueOf(strategy) : CacheStrategy.NO_CACHE;
    }
    if (CacheStrategy.CACHE_15_MINUTES.equals(cacheStrategy)) {
        cacheControl = CacheControl.maxAge(15, TimeUnit.MINUTES);
    } else if (CacheStrategy.CACHE_30_MINUTES.equals(cacheStrategy)) {
        cacheControl = CacheControl.maxAge(30, TimeUnit.MINUTES);
    } else if (CacheStrategy.CACHE_1_HOUR.equals(cacheStrategy)) {
        cacheControl = CacheControl.maxAge(1, TimeUnit.HOURS);
    } else if (CacheStrategy.CACHE_6AM_TOMORROW.equals(cacheStrategy)) {
        cacheControl = CacheControl.maxAge(getSecondsUntilTomorrow(6), TimeUnit.SECONDS);
    } else if (CacheStrategy.CACHE_TWO_WEEKS.equals(cacheStrategy)) {
        cacheControl = CacheControl.maxAge(14, TimeUnit.DAYS);
    } else {
        cacheControl = CacheControl.noCache();
    }
    if (cacheStrategy != null && cacheStrategy != CacheStrategy.NO_CACHE) {
        Cacheability cacheability = (Cacheability) systemSettingManager.getSystemSetting(SettingKey.CACHEABILITY);
        if (cacheability.equals(Cacheability.PUBLIC)) {
            cacheControl.cachePublic();
        } else if (cacheability.equals(Cacheability.PRIVATE)) {
            cacheControl.cachePrivate();
        }
    }
    response.setHeader(HEADER_CACHE_CONTROL, cacheControl.getHeaderValue());
    if (filename != null) {
        String type = attachment ? "attachment" : "inline";
        response.setHeader(HEADER_CONTENT_DISPOSITION, type + "; filename=\"" + filename + "\"");
    }
}
Also used : Cacheability(org.hisp.dhis.common.cache.Cacheability) CacheControl(org.springframework.http.CacheControl)

Example 2 with CacheControl

use of org.springframework.http.CacheControl in project spring-framework by spring-projects.

the class WebContentInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
    checkRequest(request);
    String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
    if (logger.isDebugEnabled()) {
        logger.debug("Looking up cache seconds for [" + lookupPath + "]");
    }
    CacheControl cacheControl = lookupCacheControl(lookupPath);
    Integer cacheSeconds = lookupCacheSeconds(lookupPath);
    if (cacheControl != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Applying CacheControl to [" + lookupPath + "]");
        }
        applyCacheControl(response, cacheControl);
    } else if (cacheSeconds != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Applying CacheControl to [" + lookupPath + "]");
        }
        applyCacheSeconds(response, cacheSeconds);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Applying default cache seconds to [" + lookupPath + "]");
        }
        prepareResponse(response);
    }
    return true;
}
Also used : CacheControl(org.springframework.http.CacheControl)

Example 3 with CacheControl

use of org.springframework.http.CacheControl in project spring-framework by spring-projects.

the class ResourcesBeanDefinitionParser method registerResourceHandler.

private String registerResourceHandler(ParserContext parserContext, Element element, Object source) {
    String locationAttr = element.getAttribute("location");
    if (!StringUtils.hasText(locationAttr)) {
        parserContext.getReaderContext().error("The 'location' attribute is required.", parserContext.extractSource(element));
        return null;
    }
    ManagedList<String> locations = new ManagedList<>();
    locations.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(locationAttr)));
    RootBeanDefinition resourceHandlerDef = new RootBeanDefinition(ResourceHttpRequestHandler.class);
    resourceHandlerDef.setSource(source);
    resourceHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    MutablePropertyValues values = resourceHandlerDef.getPropertyValues();
    values.add("locations", locations);
    String cacheSeconds = element.getAttribute("cache-period");
    if (StringUtils.hasText(cacheSeconds)) {
        values.add("cacheSeconds", cacheSeconds);
    }
    Element cacheControlElement = DomUtils.getChildElementByTagName(element, "cache-control");
    if (cacheControlElement != null) {
        CacheControl cacheControl = parseCacheControl(cacheControlElement);
        values.add("cacheControl", cacheControl);
    }
    Element resourceChainElement = DomUtils.getChildElementByTagName(element, "resource-chain");
    if (resourceChainElement != null) {
        parseResourceChain(resourceHandlerDef, parserContext, resourceChainElement, source);
    }
    Object manager = MvcNamespaceUtils.getContentNegotiationManager(parserContext);
    if (manager != null) {
        values.add("contentNegotiationManager", manager);
    }
    String beanName = parserContext.getReaderContext().generateBeanName(resourceHandlerDef);
    parserContext.getRegistry().registerBeanDefinition(beanName, resourceHandlerDef);
    parserContext.registerComponent(new BeanComponentDefinition(resourceHandlerDef, beanName));
    return beanName;
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) CacheControl(org.springframework.http.CacheControl)

Example 4 with CacheControl

use of org.springframework.http.CacheControl in project spring-framework by spring-projects.

the class WebContentGenerator method applyCacheSeconds.

/**
	 * Apply the given cache seconds and generate corresponding HTTP headers,
	 * i.e. allow caching for the given number of seconds in case of a positive
	 * value, prevent caching if given a 0 value, do nothing else.
	 * Does not tell the browser to revalidate the resource.
	 * @param response current HTTP response
	 * @param cacheSeconds positive number of seconds into the future that the
	 * response should be cacheable for, 0 to prevent caching
	 */
@SuppressWarnings("deprecation")
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds) {
    if (this.useExpiresHeader || !this.useCacheControlHeader) {
        // Deprecated HTTP 1.0 cache behavior, as in previous Spring versions
        if (cacheSeconds > 0) {
            cacheForSeconds(response, cacheSeconds);
        } else if (cacheSeconds == 0) {
            preventCaching(response);
        }
    } else {
        CacheControl cControl;
        if (cacheSeconds > 0) {
            cControl = CacheControl.maxAge(cacheSeconds, TimeUnit.SECONDS);
            if (this.alwaysMustRevalidate) {
                cControl = cControl.mustRevalidate();
            }
        } else if (cacheSeconds == 0) {
            cControl = (this.useCacheControlNoStore ? CacheControl.noStore() : CacheControl.noCache());
        } else {
            cControl = CacheControl.empty();
        }
        applyCacheControl(response, cControl);
    }
}
Also used : CacheControl(org.springframework.http.CacheControl)

Example 5 with CacheControl

use of org.springframework.http.CacheControl in project spring-framework by spring-projects.

the class HeaderAssertionsTests method cacheControl.

@Test
public void cacheControl() throws Exception {
    CacheControl control = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform();
    HttpHeaders headers = new HttpHeaders();
    headers.setCacheControl(control.getHeaderValue());
    HeaderAssertions assertions = headerAssertions(headers);
    // Success
    assertions.cacheControl(control);
    try {
        assertions.cacheControl(CacheControl.noStore());
        fail("Wrong value expected");
    } catch (AssertionError error) {
    // Expected
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CacheControl(org.springframework.http.CacheControl) Test(org.junit.Test)

Aggregations

CacheControl (org.springframework.http.CacheControl)5 Cacheability (org.hisp.dhis.common.cache.Cacheability)1 Test (org.junit.Test)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)1 ManagedList (org.springframework.beans.factory.support.ManagedList)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 HttpHeaders (org.springframework.http.HttpHeaders)1 Element (org.w3c.dom.Element)1