2015-09-09 36 views
1

我使用的是Apache camel 2.15.1版本。在这我使用Servlet组件来休息dsl。我简单的路线看起来像下面如何在Apache Camel中设置Cache-Control标头

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

我有一个要求设置缓存控制和附注头的响应。

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

任何有助于解决这个问题是高度赞赏。

回答

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; 
    } 

} 

现在如果要是跑我的路线没有.endpointProperty( “headerFilterStrategy”, “#过滤器”)线我得到的输出力科本

$ 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 

.endpointProperty( “headerFilterStrategy”, “#过滤器”)线输出这样

$ 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.ne[email protected]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 
相关问题