【问题标题】:How to set Cache-Control headers in Apache Camel如何在 Apache Camel 中设置 Cache-Control 标头
【发布时间】:2015-09-09 15:05:35
【问题描述】:

我使用的是 Apache camel 2.15.1 版本。在这我使用 servlet 组件来休息 dsl。我的简单路线如下所示

from(rest:get:CustomerDetails.json) .to("http://localhost:8080/customer/getCustomerDetails?bridgeEndpoint=true");

我需要为响应设置 Cache-Control 和 Pragma 标头。

from(rest:get:CustomerDetails.json) .to("http://localhost:8080/customer/getCustomerDetails?bridgeEndpoint=true") .setHeader("Cache-Control",constant("private, max-age=0,no-store"));

但是骆驼忽略了这一点。我读了一些其他建议使用自定义 HeaderFilterStrategy 的博客。我也试过这个。它没有帮助。

https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Web_Services_and_Routing_with_Camel_CXF/files/Proxying-Headers.html

https://issues.apache.org/jira/browse/CAMEL-6085

非常感谢任何解决此问题的帮助。

【问题讨论】:

    标签: apache-camel cache-control


    【解决方案1】:

    您可以让它与自定义 HeaderFilterStrategy 一起使用。诀窍是在 restConfiguration().endpointProperties(..) 中配置它,如下所示:

    public void configure() {
        JndiRegistry registry = getContext().getRegistry(JndiRegistry.class);
        registry.bind("filter", new HeaderFilter());
    
        restConfiguration()
        .host("localhost")
        .endpointProperty("headerFilterStrategy","#filter")
        .setPort("10000");
    
        from("rest:get:hello")
        .to("http://localhost:20000?bridgeEndpoint=true")
        .setHeader("Cache-Control",constant("private, max-age=0,no-store"));
    
        from("netty-http:http://localhost:20000")
        .setBody(constant("ok"));
    }
    

    #filter 只是像这样的虚拟实现(您可以创建一个更适合您需求的过滤器)

    public class HeaderFilter implements HeaderFilterStrategy {
    
        @Override
        public boolean applyFilterToCamelHeaders(String arg0, Object arg1, Exchange arg2) {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public boolean applyFilterToExternalHeaders(String arg0, Object arg1, Exchange arg2) {
            // TODO Auto-generated method stub
            return false;
        }
    
    }
    

    现在如果运行我的路线 without .endpointProperty("headerFilterStrategy","#filter") 行我会得到这样的输出

    $ curl -D - http://localhost:10000/hello
    HTTP/1.1 200 OK
    Accept: */*
    breadcrumbId: ID-myhost-40508-1441899753215-0-1
    User-Agent: curl/7.35.0
    Content-Length: 2
    Connection: keep-alive
    
    ok
    

    with .endpointProperty("headerFilterStrategy","#filter") 这样的行输出

    $ curl -D - http://localhost:10000/hello
    HTTP/1.1 200 OK
    Accept: */*
    breadcrumbId: ID-myhost-56308-1441899833287-0-1
    Cache-Control: private, max-age=0,no-store
    CamelHttpMethod: GET
    CamelHttpResponseCode: 200
    CamelHttpUri: /hello
    CamelHttpUrl: http://localhost:10000/hello
    CamelNettyChannelHandlerContext: org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext@1fac34b
    CamelNettyLocalAddress: /127.0.0.1:10000
    CamelNettyMessageEvent: [id: 0x93dfe147, /127.0.0.1:35302 => /127.0.0.1:10000] RECEIVED: DefaultHttpRequest(chunked: false) GET /hello HTTP/1.1 User-Agent: curl/7.35.0 Host: localhost:10000 Accept: */*
    CamelNettyRemoteAddress: /127.0.0.1:35302
    User-Agent: curl/7.35.0
    Content-Length: 2
    Connection: keep-alive
    
    ok
    

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 2023-04-01
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多