Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Spring to avoid setting Pragma No-Cache

My system is Spring MVC based and I checked that Spring automatically sets PRAGMA: no-cache. The system is available to the users through SSL. When the users try to download something using the INTERNET EXPLORER 7 or 8 an error like "Internet Explorer cannot download file from server" appears (more details: http://support.microsoft.com/default.aspx?scid=KB;EN-US;q316431&).

I tried to configure the WebContentInterceptor like the code bellow but does not work:

<mvc:interceptors>
    <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <property name="cacheSeconds" value="2100" />
        <property name="useExpiresHeader" value="false" />
        <property name="useCacheControlHeader" value="false" />
        <property name="useCacheControlNoStore" value="false" />
    </bean>
</mvc:interceptors>

What can I do avoid Spring send the Pragma: no-cache and related to Cache Control?

Regards!

like image 385
Marcelo Juventino Avatar asked Jan 23 '12 18:01

Marcelo Juventino


3 Answers

You can write your own custom interceptor and set the header values to the response object. Interceptors are nothing but filters so override the filter and use the

prehandle and posthandle to set the request and response headers respectively.

Let me know if you want specific examples doing that.

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <beans:bean id="customInterceptor"
            class="org.example.interceptors.CustomInterceptor">
        </beans:bean>
    </mvc:interceptor>

</mvc:interceptors>




 public class CustomInterceptor implements HandlerInterceptor{

     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView modelAndView) throws Exception {
     response.setHeader(...);}
}
like image 124
Liam Avatar answered Oct 01 '22 21:10

Liam


The simplest approach is probably just to stop the header from being written with a servlet filter. This way no Spring configuration has to be modified and you pick up the correct cache functionality for free.

public class PragmaFilter implements Filter {

    private static String PRAGMA_HEADER = "Pragma";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException { }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request, new NoPragmaHttpServletResponseWrapper(response));
    }

    @Override
    public void destroy() { }

    private final class NoPragmaHttpServletResponseWrapper extends HttpServletResponseWrapper {

        private NoPragmaHttpServletResponseWrapper(ServletResponse response) {
            super((HttpServletResponse) response);
        }

        @Override
        public void addHeader(String name, String value) {
            if (PRAGMA_HEADER.equals(name)) {
                return;
            }
            super.addHeader(name, value);
        }

        @Override
        public void setHeader(String name, String value) {
            if (PRAGMA_HEADER.equals(name)) {
                return;
            }
            super.setHeader(name, value);
        }
    }

}
like image 26
Scott Avatar answered Oct 01 '22 20:10

Scott


Try to set cache seconds to an negative value.


If this does not help you will need to override:

   protected final void preventCaching(HttpServletResponse response)

or

  protected final void applyCacheSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)

Both methods are implements in WebContentGenerator

like image 34
Ralph Avatar answered Oct 01 '22 19:10

Ralph