RESTEasy - Response with duplicated Cache-Control - Wildfly10

夙愿已清 提交于 2019-12-08 02:58:25

问题


I have a GET response with an image:

@GET
@Path("{id}/thumbnail")
public Response readThumbnailById(@PathParam("id") String id, @QueryParam("serviceContext") EIServiceContext serviceContext) throws BadRequestRestException {
    try {
        serviceContextServices.setFullContext(serviceContext);
        byte[] thumbnail = documentClientWebServices.readThumbnailById(id);
        CacheControl cc = new CacheControl();
        cc.setMaxAge(2592000);
        cc.setPrivate(true);

        ResponseBuilder builder = Response.ok(thumbnail,"image/png");
        builder.cacheControl(cc);
        return builder.build();
    } catch (XECMException ex) {
        throw new BadRequestRestException(ex);
    } catch (ComponentException ex) {
        XECMRestException e = new XECMRestException(ex);
        return Response.noContent().entity(e.getEiResultStatus()).build();
    }
}

And I want the client browser caching my image automatically,

But the image was not cached,

The Response Header of Chrome show me:

HTTP/1.1 200 OK
Content-Encoding: gzip
Expires: 0
Cache-Control: no-cache, no-store, must-revalidate
Cache-Control: no-transform, max-age=2592000, private
X-Powered-By: Undertow/1
Server: WildFly/10
Pragma: no-cache
Date: Fri, 01 Jul 2016 05:57:21 GMT
Connection: keep-alive
Content-Type: image/png
Content-Length: 8727

It is because of the duplicated Cache-Control

How can I remove the first Cache-Control entry?

Thanks


回答1:


After google a lot, I found out, a global solution :

<subsystem xmlns="urn:jboss:domain:undertow:3.0">
        <buffer-cache name="default"/>
        <server name="default-server">
            <http-listener name="default" socket-binding="http" redirect-socket="https"/>
            <https-listener name="https" security-realm="UndertowRealm" socket-binding="https"/>
            <host name="default-host" alias="localhost">
                <location name="/" handler="welcome-content"/>
                <filter-ref name="server-header"/>
                <filter-ref name="x-powered-by-header"/>
                <filter-ref name="gzipFilter" predicate="exists['%{o,Content-Type}'] or regex[pattern='(?:application/javascript|text/css|text/html|text/xml|application/json)(;.*)?', value=%{o,Content-Type}, full-match=true]"/>
                <filter-ref name="custom-max-age" predicate="exists['%{o,Content-Type}'] or regex[pattern='(?:image/png)(;.*)?', value=%{o,Content-Type}, full-match=true] or path-suffix['.js'] or path-suffix ['.json'] or path-suffix ['.html'] or path-suffix ['.css'] or path-suffix ['.jpg'] or path-suffix ['.jpeg'] or path-suffix ['.png'] or path-suffix ['.gif']"/>
            </host>
        </server>
        <servlet-container name="default" disable-caching-for-secured-pages="false">
            <jsp-config/>
            <websockets/>
        </servlet-container>
        <handlers>
            <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
        </handlers>
        <filters>
            <response-header name="server-header" header-name="Server" header-value="WildFly/10"/>
            <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
            <response-header name="custom-max-age" header-name="Cache-Control" header-value="max-age=86400, public"/>
            <gzip name="gzipFilter"/>
        </filters>
    </subsystem>

The key is :

<servlet-container name="default" disable-caching-for-secured-pages="false">

After disable-caching-for-secured-pages, There is no first entry Cache-Control: no-cache, no-store, must-revalidate



来源:https://stackoverflow.com/questions/38138503/resteasy-response-with-duplicated-cache-control-wildfly10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!