Search code examples
htmlgrailsgroovyhttprequestgrails-2.0

How to set Response Headers with Grails CacheHeaders Plugin?


I am using Grails 2.4.4 with the Cache Headers Plugin.

Here is the tag I use:

  withCacheHeaders {
    etag {
      "${objectType}-${lastModifiedDateTime}-${width}-${objectId}".encodeAsSHA1()
    }
    delegate.lastModified {
      lastModifiedDate ?: 0
    }
    generate {

      response.setContentType(ImageService.getMimeTypeFromFileName(fullFileName))
      response.setHeader("Content-disposition", "filename=\"${fullFileName}\"")
      response.setContentLength(imageBytes?.size())

      response.outputStream << imageBytes
    }        
  } // end withCacheHeaders

t works fine but it turns out that the response headers I set are not part of the response. They were perfectly set when I do not use the plugin.

Here are the headers which are set:

enter image description here

The headers which are not set are the one declared with:

response.setContentType(ImageService.getMimeTypeFromFileName(fullFileName))
response.setHeader("Content-disposition", "filename=\"${fullFileName}\"")
response.setContentLength(imageBytes?.size())

I.e., mime type, file name, content length.

How to set Response Headers with Grails CacheHeaders Plugin?


Solution

  • The reason why these headers do not get set is by design. Not design of the plugin but HTTP. According to RFC2616 Content-Type and Content-Length headers should not be sent with HTTP response status of 304.