Struts 2 discard cache header

Posted by Dewfy on Stack Overflow See other posts from Stack Overflow or by Dewfy
Published on 2010-05-15T22:05:50Z Indexed on 2010/05/15 22:10 UTC
Read the original article Hit count: 225

Filed under:
|

I have strange discarding behavior of struts2 while setting cache option for my image.

I'm trying to put image from db to be cached on client side To render image I use ( http://struts.apache.org/2.x/docs/how-can-we-display-dynamic-or-static-images-that-can-be-provided-as-an-array-of-bytes.html ) where special result type render as follow:

public void execute(ActionInvocation invocation) throws Exception {
     ...//some preparation
    HttpServletResponse response = ServletActionContext.getResponse();
    HttpServletRequest request = ServletActionContext.getRequest();
    ServletOutputStream os = response.getOutputStream();
    try
    {
        byte[] imageBytes = action.getImage();
        response.setContentType("image/gif");
        response.setContentLength(imageBytes.length);
        //I want cache up to 10 min
        Date future = new Date(((new Date()).getTime() + 1000 * 10*60l));
        ;
        response.addDateHeader("Expires", future.getTime());
      response.setHeader("Cache-Control", "max-age=" + 10*60 + "");
        response.addHeader("cache-Control", "public"); 
        response.setHeader("ETag", request.getRequestURI());
        os.write(imageBytes);
    }
    catch(Exception e)
    {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }

    os.flush();
    os.close();
}

But when image is embedded to page it is always reloaded (Firebug shows code 200), and neither Expires, nor max-age are presented in header

Host    localhost:9090
Accept  image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  300
Connection  keep-alive
Referer http://localhost:9090/web/result?matchId=1
Cookie  JSESSIONID=4156BEED69CAB0B84D950932AB9EA1AC; 
If-None-Match   /web/_srv/teamcolor
Cache-Control   max-age=0 

I have no idea why it is dissapered, may be problem in url? It is forms with parameter:

 http://localhost:9090/web/_srv/teamcolor?loginId=3

© Stack Overflow or respective owner

Related posts about struts2

Related posts about caching