Well, as you say that you want to cache
the entire content of your underlying target resource, you have to cache its byte[]
from inputStream
.
Since <mvc:resources>
is backed by ResourceHttpRequestHandler
there is no stops to write your own sub-class and use it directly instead of that custom tag.
And implement your caching logic just within overrided writeContent
method:
public class CacheableResourceHttpRequestHandler extends ResourceHttpRequestHandler {
private Map<URL, byte[]> cache = new HashMap<URL, byte[]>();
@Override
protected void writeContent(HttpServletResponse response, Resource resource) throws IOException {
byte[] content = this.cache.get(resource.getURL());
if (content == null) {
content = StreamUtils.copyToByteArray(resource.getInputStream());
this.cache.put(resource.getURL(), content);
}
StreamUtils.copy(content, response.getOutputStream());
}
}
And use it from spring config as generic bean:
<bean id="staticResources" class="com.my.proj.web.CacheableResourceHttpRequestHandler">
<property name="locations" value="/public-resources/"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/resources/**=staticResources</value>
</property>
</bean>