如何设置Apache的骆驼的Cache-Control头 [英] How to set Cache-Control headers in Apache Camel

查看:484
本文介绍了如何设置Apache的骆驼的Cache-Control头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Apache骆驼2.15.1版本。在此我使用的servlet组件休息DSL。我简单的路线看起来像下面

I am using Apache camel 2.15.1 version. In this I am using servlet component for rest dsl. My simple route looks like below

从(休息:得到:CustomerDetails.json)
。要(HTTP://本地主机:8080 /客户/ getCustomerDetails bridgeEndpoint =真正的);

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

I have a requirement to set Cache-Control and Pragma headers for response.

从(休息:得到:CustomerDetails.json)
。要(HTTP://本地主机:8080 /客户/ getCustomerDetails bridgeEndpoint =真的吗?)
.setHeader(缓存控制,常数(私人,最大年龄= 0,无店))​​;

但骆驼忽略这一点。我看了一些别人博客这表明使用自定义HeaderFilterStrategy。我这个尝试为好。它并没有帮助。

But camel ignores this. I read few others blog which suggests to use a custom HeaderFilterStrategy. I tried with this as well. It didn't help.

<一个href=\"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\" rel=\"nofollow\">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

任何有助于解决这个问题是高度AP preciated。

Any help to fix this issue is highly appreciated.

推荐答案

您可以得到它的自定义HeaderFilterStrategy工作。诀窍是配置在restConfiguration()endpointProperties(..)是这样的:

You can get it to work with custom HeaderFilterStrategy. The trick is to configure it in restConfiguration().endpointProperties(..) like this:

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 仅仅是虚拟实现这样的(你可以创建一个适合你的好需要的过滤器)

where #filter is just dummy implementation like this (you can create a filter that suits better your needs)

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,#过滤器)的行我得到这样的输出


Now if if run my routes without .endpointProperty("headerFilterStrategy","#filter") line I get output like this

$ 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.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

这篇关于如何设置Apache的骆驼的Cache-Control头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆