Example usage for org.springframework.web.servlet.resource ResourceHttpRequestHandler setCacheSeconds

List of usage examples for org.springframework.web.servlet.resource ResourceHttpRequestHandler setCacheSeconds

Introduction

In this page you can find the example usage for org.springframework.web.servlet.resource ResourceHttpRequestHandler setCacheSeconds.

Prototype

public final void setCacheSeconds(int seconds) 

Source Link

Document

Cache content for the given number of seconds, by writing cache-related HTTP headers to the response: <ul> <li>seconds == -1 (default value): no generation cache-related headers</li> <li>seconds == 0: "Cache-Control: no-store" will prevent caching</li> <li>seconds > 0: "Cache-Control: max-age=seconds" will ask to cache content</li> </ul> <p>For more specific needs, a custom org.springframework.http.CacheControl should be used.

Usage

From source file:com.javaetmoi.core.spring.vfs.Vfs2ResourceHandlerRegistration.java

@Override
protected ResourceHttpRequestHandler getRequestHandler() {
    Field locationsField = ReflectionUtils.findField(ResourceHandlerRegistration.class, "locations");
    ReflectionUtils.makeAccessible(locationsField);
    @SuppressWarnings("unchecked")
    List<Resource> locations = (List<Resource>) ReflectionUtils.getField(locationsField, this);

    Field cachePeriodField = ReflectionUtils.findField(ResourceHandlerRegistration.class, "cachePeriod");
    ReflectionUtils.makeAccessible(cachePeriodField);
    Integer cachePeriod = (Integer) ReflectionUtils.getField(cachePeriodField, this);

    // Initial code is replace by a new Vfs2ResourceHttpRequestHandler()
    Assert.isTrue(!CollectionUtils.isEmpty(locations),
            "At least one location is required for resource handling.");
    ResourceHttpRequestHandler requestHandler = new Vfs2ResourceHttpRequestHandler();
    requestHandler.setLocations(locations);
    if (cachePeriod != null) {
        requestHandler.setCacheSeconds(cachePeriod);
    }//from  w  w  w  .j  av a 2s  .  c om
    return requestHandler;
}