简体   繁体   English

HTTPS归档在Apache Camel中

[英]HTTPS to file in Apache Camel

I need to access a HTTPS Resource which is using Basic Authentication and It is a GET Resource. 我需要访问使用基本身份验证的HTTPS资源,它是GET资源。 I have written a code in java using URIBuilder and adding the username, password etc as Headers to the URIBuilder and using Apache HTTPClient to access the resource and it is working well. 我已经使用URIBuilder在Java中编写了一个代码,并将用户名,密码等作为标头添加到URIBuilder并使用Apache HTTPClient访问该资源,并且运行良好。

Now my requirement is to implement the same functionality using Apache Camel , Well I tried using Camel HTTP component and by using Java DSL . 现在,我的要求是使用Apache Camel实现相同的功能。我尝试使用Camel HTTP组件和Java DSL

The problem is I am just able to provide the URI . 问题是我只能提供URI But how can I add various Headers to this URI ?. 但是如何向此URI添加各种标头?

I suggest you use the http4 component as a way to consume this secure resource. 我建议您使用http4组件作为使用此安全资源的方法。 From reading the docs of the component you can see it's possible to set the query parameters, path and even uri at runtime. 通过阅读组件文档,您可以看到可以在运行时设置查询参数,路径甚至uri。

In answer to your specific question, the headers on the exchange at the point it reaches the .to() will be sent as headers in the HTTP request so you may want to define a header filter strategy. 为了回答您的特定问题,交换器到达.to()时的标头将作为HTTP请求中的标头发送,因此您可能需要定义标头过滤器策略。 It has support for http basic auth and you can set your credentials via the authUsername and authPassword headers. 它支持HTTP基本身份验证,您可以通过authUsername和authPassword标头设置凭据。 You may need to provide a custom HttpContext because you're authenticating via https as it suggests at the bottom of the docs. 您可能需要提供自定义HttpContext,因为您正在通过https进行身份验证,如文档底部所示。 For example: 例如:

from("direct:in")
    .process(new Processor() {
        public void process(Exchange exchange) {
            //These headers you set here will get sent with the http request in the to() after this processor
            exchange.getIn().setHeader("authUsername", "username");
            exchange.getIn().setHeader("authPassword", "password");
        }
    })
    .to("https4://uri.com);

Use the simple language to add headers if you are using blueprint or the java dsl if its pure java. 如果使用的是蓝图,请使用简单的语言添加标头;如果使用的是纯Java,请使用java dsl。 Simple example: 简单的例子:

from("direct:start")
  .setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.POST))
  .to("http4://www.google.com")
  .to("mock:results");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM