cancel
Showing results for 
Search instead for 
Did you mean: 

Cange Cache-Control for documents retrieved from Document Service

karstenvoigt
Participant
0 Kudos

Hello,

we currently use the document service to store common images that are delivered to the clients using a Proxy Bridge (extends AbstractCmisProxyServlet). How can we change the Cache-Control header for the content response? The documents should be cacheable by any proxy and at 24 hours valid. The default just returns:

Cache-Control →private, max-age=0

Any hint or documentation link is welcome!

Accepted Solutions (1)

Accepted Solutions (1)

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Karsten,

Is the cache control header being set when you load images via proxy bridge or is it only when your request returns a json response?

It is my understanding that documents and images should not have the cache control set as private. It makes sense to set the more strict cache control in the proxy bridge when your request is more dynamic (like a json response).

However, if you are getting this behavior even when when the result is a document or image, I would suggest implementing a Servlet Filter. I've found one that works for setting the Cache-Control and the Expires here.

The docs instruct you to use it via pom dependency. But I've used the CacheFilter class with a slight manual change in it. If you use as default, you would still have two headers being set with the same name for Cache Control. Thus, you would need to change some logic to avoid it the received header variable from being set. I did it like so:

@Override
public void addHeader(String name, String value) {
    if (!HTTPCacheHeader.PRAGMA.getName().equalsIgnoreCase(name)) {
        if (!name.equalsIgnoreCase(HTTPCacheHeader.CACHE_CONTROL.getName())) {
        	super.addHeader(name, value);	
        }
    }
}

And if you are interested, I've used the following annotations on my modified class:

@WebFilter(
	    urlPatterns={"/cmis/*"},
	    initParams = { 
	    		@WebInitParam(name = "expiration", value = "2592000"),
	    		@WebInitParam(name = "private", value = "false")
	    }
	)

NOTE: If you are not using maven to build your proxy bridge, you should download the library and include it in the WEB-INF/lib folder or you could simply import the whole source code into your project.

Hope this helps.

Best regards,

Ivan

karstenvoigt
Participant
0 Kudos

Hi Ivan,

thanks a lot! That solved the issue.

Regards Karsten

Answers (1)

Answers (1)

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Karsten,

AbstractCmisProxyServlet extends the default HTTPServlet class.

Have you tried already to set the response with the appropriate headers by overriding the method doGet(), like so:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

	LocalDateTime ldt = LocalDateTime.now();
	LocalDateTime in24H = ldt.plusHours(24);
	long timeInMilSec = in24H.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
	
	resp.setDateHeader("Expires", timeInMilSec);
	resp.setHeader("Cache-Control","public");

	super.doGet(req, resp);
}

Best regards,
Ivan

karstenvoigt
Participant
0 Kudos

Thanks, but it seems to be not that easy.

The AbstractCmisProxyServlet implements the service - method and If I override that method to set the Header field I get the Content-Cache Header twice in the response:

Cache-Control →public, max-age=86400
Cache-Control →private, max-age=0 

It seems that the Cache-Control: private - part is added after the service - method returns. (I justed printed all header fields after calling super.service and this only shows my public Cache-Control.)