camel-http4帖子无法正常工作我没有回复

时间:2018-04-23 18:10:05

标签: https apache-camel camel-http

这是我的第一个camel HTTP4实现并遇到麻烦。我得不到响应,好像到URI的路由是错误的,消息去了???它不会出现HTTP4调用吗?

使用camel-version 2.17.0.redhat-630310,java 8

我的pom有依赖

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-http4</artifactId>
    </dependency>

我的路线

    <route 
        id="core.getToken.route"
        autoStartup="true" >
        <from id="getToken" ref="getToken" />
        <process ref="uAARequestTokenProcessor" />
        <log message="Message after uAARequestTokenProcessor:  ${body}" loggingLevel="INFO"/> 
         <setHeader headerName="CamelHttpMethod">
            <constant>POST</constant>
         </setHeader>
        <to uri="http4://dummyhost" />
        <log message="After HTTP4 POST: ${body}" loggingLevel="INFO"/> 
        <to uri="{{accessToken}}" />    
    </route>    

我调用uAARequestTokenProcessor来调节出站消息。

public class UAARequestTokenProcessor implements Processor {

    private static final Logger LOG = LoggerFactory.getLogger(UAARequestTokenProcessor.class);

    @Override
    public void process(Exchange exchange) throws Exception {

        LOG.info("Entering UAA Request Token Processor: start " );

            final String clientId = (String)exchange.getIn().getHeader("CLIENTID");
            final byte[] ClientId = clientId.getBytes("UTF-8");
            final String userName = (String)exchange.getIn().getHeader("USERNAME");
            final String password = (String)exchange.getIn().getHeader("PASSWORD");
            final String tokenUrl = (String)exchange.getIn().getHeader("TOKENURL");

            LOG.info("ClientId: " + new String(ClientId));
            LOG.info("userName: " + userName);
            LOG.info("password: " + password);
            LOG.info("tokenUrl: " + tokenUrl);
            LOG.info("Processing UAA token request for Client ID: " + clientId + " and User Name: " + userName);            

            LOG.info("Before Base64 encryption of Client ID: " + new String(ClientId));
            StringBuilder authHeader = new StringBuilder("Basic "); 
            authHeader.append(Base64.getEncoder().encodeToString(ClientId));

            LOG.info("after Base64 encryption of Client ID: " + authHeader.toString());

            String body = String.format("grant_type=password&username=%s&password=%s",
                      URLEncoder.encode(userName, "UTF-8"), //Translates a string into x-www-form-urlencoded format
                      URLEncoder.encode(password, "UTF-8"));
            LOG.info("HTTP BODY: " + body);
            exchange.getIn().setHeader(Exchange.HTTP_URI, tokenUrl);  
            exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "MediaType.APPLICATION_FORM_URLENCODE");
            exchange.getIn().setHeader("Authorization", authHeader.toString());
            exchange.getIn().setBody(body);

            LOG.info("Exchange.HTTP_URI: " + exchange.getIn().getHeader("Exchange.HTTP_URI"));  
            LOG.info("Exchange.CONTENT_TYPE: " + exchange.getIn().getHeader("Exchange.CONTENT_TYPE"));
            LOG.info("Authorization: " + exchange.getIn().getHeader("Authorization"));

            LOG.info("Exiting UAA Request Token Processor: Finish " );
        }

}

除非HTTP4的标头不同,否则我认为Exchange.HTTP_URI或HTTP标头没有设置?但我正在关注HTTP4文档,https://github.com/apache/camel/blob/master/components/camel-http4/src/main/docs/http4-component.adoc

在处理器内部发生的事情我尝试读取HTTP标头,并在设置后显示它们为空。

Entering UAA Request Token Processor: start
ClientId: ingestor.57e72dd3-6f9e-4931-b4bc-cd04eaaff3e3.1f7dbe12-2372-439e-8104-06a5f4098ec9
userName: 5c0642fe-a495-44db-93f7-67056fa2c061_ingestor
password: 154f0252d166f27b5e21ef171b02a79f41a0daf3
tokenUrl: http4://d1e53858-2903-4c21-86c0-95edc7a5cef2.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token
Processing UAA token request for Client ID: ingestor.57e72dd3-6f9e-4931-b4bc-cd04eaaff3e3.1f7dbe12-2372-439e-8104-06a5f4098eff9 and User Name: 5c0232fe-a495-44db-94f7-67156fa2c061_ingestor
Before Base64 encryption of Client ID: ingestor.57e72dd3-6f9e-4931-b4bc-cd04eaaff3e3.1f7dbe12-2372-439e-8104-06a5f4098eff9
after Base64 encryption of Client ID: Basic aW5nZXN0b3IuNTdlNzJkZDMtNmY5ZS00OTMxLWI0YmMtY2QwNGVhYWZmM2UzLjFmN2RiZTEyLTIzNzItNDM5ZS04MTA0LTA2YTVmNDA5OGVjOQ==
HTTP BODY: grant_type=password&username=5c0232fe-a495-44db-94f7-67156fa2c061_ingestor&password=154f0252d166f27b5e21ef171b02beebop
Exchange.HTTP_URI: null
Exchange.CONTENT_TYPE: null
Authorization: Basic aW5nZXN0b3IuNTdlNzJkZDMtNmY5ZS00OTMxLWI0YmMtY2QwNGVhYWZmM2UzLjFmN2RiZTEyLTIzNzItNDM5ZS04MTA0LTA2YTVmNDA5OGVjOQ==
Exiting UAA Request Token Processor: Finish

堆栈跟踪显示标题但不显示为EXCHANGE.HTTP_URI

    2018-04-23 13:17:29,203 | INFO  | ile://ge-ip/core | Tracer                           | 231 - org.apache.camel.camel-core - 2.17.0.redhat-630310 | ID-alphprdfuse2i-44477-1524503724804-3-52 >>> 
(core.getToken.route) setHeader[CamelHttpMethod, POST] --> http4://predhost <<< Pattern:InOnly, 
Headers:{Authorization=Basic aW5nZXN0b3IuNTdlNzJkZDMtNmY5ZS00OTMxLWI0YmMtY2QwNGVhYWZmM2UzLjFmN2RiZTEyLTIzNzItNDM5ZS04MTA0LTA2YTVmNDA5OGVjOQ==, 
breadcrumbId=ID-alphprdfuse2i-44477-1524503724804-3-50, 
CamelFileAbsolute=false, 
CamelFileAbsolutePath=/app/iprctest/jboss-fuse-6.3.0.redhat-310/ge-ip/core/Noble/nst_dge-1_2018-04-10_19-55-10.CSV, CamelFileLastModified=1524503800000, 
CamelFileLength=3706, CamelFileName=Noble/nst_dge-1_2018-04-10_19-55-10.CSV, CamelFileNameConsumed=Noble/nst_dge-1_2018-04-10_19-55-10.CSV, CamelFileNameOnly=nst_dge-1_2018-04-10_19-55-10.CSV, 
CamelFileNameProduced=ge-ip/upload/Noble/nst_dge-1_2018-04-10_19-55-10.CSV, CamelFileParent=ge-ip/core/Noble, 
CamelFilePath=ge-ip/core/Noble/nst_dge-1_2018-04-10_19-55-10.CSV, CamelFileRelativePath=Noble/nst_dge-1_2018-04-10_19-55-10.CSV, CamelHttpMethod=POST, 
CamelHttpUri=http4://d1e53858-2903-4c21-86c0-95edc7a5cef2.pred-uaa.run.aws-usw02-pr.ice.pred.io/oauth/token, 
CLIENTID=ingestor.57e72dd3-6f9e-4931-b4bc-cd04eaaff3e3.1f7dbe12-2372-439e-8104-06a5f4098ec9, 
Content-Type=MediaType.APPLICATION_FORM_URLENCODE, 
CUSTKEY=Noble, PASSWORD=154f0252d166f27b5e21a79f41a0daf3, 
TENANTUUID=c0642fe-a495-44db-93f7-67056fa2c061, 
TOKENURL=http4://d1e53858-2903-4c21-86c0-95edc7f2.pred-uaa.run.aws-usw02-pr.ice.pred.io/oauth/token, 
UPLOADURL=http4://apm-times-query-svc-prod.app-api.aws-usw02-pr.pred.io/v2/time_series/upload, 
USERNAME=5c0642fe-a495-44db-93f7-67056fa2c061_ingestor}, 
BodyType:String, Body:grant_type=password&username=5c0642fe-a495-44db-93f7-2c061_ingestor&password=154f0252d166f2ef171b02a79f41a0daf3

http4:// predhost是spring XML中的虚拟URI,应该由exchange.getIn()。setHeader(Exchange.HTTP_URI,tokenUrl)覆盖;

我从未到达URL端点,并且在进入HTTP4://

之后永远不会到达日志消息
        <to uri="http4://predixhost" />
        <log message="After HTTP4 POST: ${body}" loggingLevel="INFO"/> 

我已经尝试了以下但仍然没有得到回应,没有例外,没有。将标题CamelHttpUri设置为覆盖默认的虚拟uri,以及动态toD,???

    <route 
        id="core.getToken.route"
        autoStartup="true" >
        <from id="getToken" ref="getToken" />
        <process ref="uAARequestTokenProcessor" />
<!--        <log message="Message after uAARequestTokenProcessor:  ${body}" loggingLevel="INFO"/>   -->
         <setHeader headerName="CamelHttpMethod">
            <constant>POST</constant>
         </setHeader>
<!--         <setHeader headerName="CamelHttpUri">
            <simple>${header.TOKENURL}</simple>
         </setHeader>  -->
        <toD uri="${header.TOKENURL}" />
        <log message="After HTTP4 POST: ${body}" loggingLevel="INFO"/> 
        <to uri="{{accessToken}}" />    
    </route> 

我不知道有什么问题?我无法得到响应,例外,看起来我无法发送到HTTP4端点。我现在到达了,在环境中验证了我的代理设置,并且还尝试了驼峰环境,我不知道发生了什么或做了什么,我没有记录信息继续,...

<camelContext     
  id="com.passthru.coreCamelContext"
  trace="true"
  xmlns="http://camel.apache.org/schema/blueprint"
  allowUseOriginalMessage="false"
  streamCache="true"
  errorHandlerRef="deadLetterErrorHandler" >
 <properties>
       <property key="http.proxyHost" value="PITC-Zscaler-Americas-Cin.proxy.corporate.comp.com"/>
       <property key="http.proxyPort" value="80"/>
  </properties>

<streamCaching  id="CacheConfig" 
                spoolUsedHeapMemoryThreshold="70" 
                anySpoolRules="true"/>

我尝试过使用CURL,它可以在命令行中运行,我已经尝试过Postman并且它可以工作。谢谢你的帮助。

这是做什么的,只需将消息发布到UAA(https)服务,主体为:

grant_type=password&username=myUserName&password=myPassword

和标题:

Content-Type=application/x-www-form-urlencoded
Authorization=Basic aW5nZXN0b3IuNTdlNzJkZDMt==

,服务器将发出访问令牌。

像这个邮递员的例子一样:

OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "username=93f7c061_ingestor&password=15a79f41a0daf3&grant_type=password");
Request request = new Request.Builder()
  .url("https://d1e53858-2903-4c21-86c0-95edc7a5cef2.uaa.run.aws-usw02-pr.ice.io/oauth/token")
  .post(body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .addHeader("Authorization", "Basic aW5nZXN0b3IuNTdlNzJkZDMtNmY5ZS00OTMxLWI0YmMtY2QwNGVhYWZmM2UzLjFmN2RiZTEyLTIzNzItNDM5ZS04MTA0LTA2YTVmNDA5OGVjOTo=")
  .addHeader("Cache-Control", "no-cache")
  .addHeader("Postman-Token", "ad8ba9b0-6215-4c53-a6ef-b3a5bf7901f6")
  .build();

Response response = client.newCall(request).execute();

您好,我希望有人可以提供帮助,我被困住了。 我已经确认,我相信HTTP4端点不起作用。我有一个死信队列

<bean id="deadLetterErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">
    <property name="deadLetterUri" value="${deadLetterQueue}"/>
    <property name="redeliveryPolicy" ref="redeliveryPolicyConfig"/>
</bean>

<bean id="redeliveryPolicyConfig" class="org.apache.camel.processor.RedeliveryPolicy">
    <property name="maximumRedeliveries" value="3"/>
</bean>

发送到此端点的每个交换尝试3次,然后发送到deadletter队列。 deadletter队列中的消息内容应该发送到HTTP4端点,从而发送到UAA服务。

├── deadletterqueue
│   ├── ID-alphprdfuse2i-36326-1524606956414-3-65

grant_type=password&username=5c0642fe_ingestor&password=02a79f41a0daf3

导致此外观的邮件标题也很好。

core.getToken.route) log[HTTP4 POST body: ${body}] --> http4://d1e53858-2903-4c21-86c0-95edc7a5cef2.uaa.run.aws-usw02.io:443/oauth/token?throwExceptionOnFailure=false <<< Pattern:InOnly, Headers:{Authorization=Basic aW5nZXN0b3IuNTdlNzJkZDMtNmY5ZS00OTMxLWI0YmMtY2QwNGVhYWZmM2UzLjFmN2RiZTEyLTIzNzItNDM5ZS04MTA0LTA2YTVmNDA5OGVjOQ==, CamelHttpCharacterEncoding=UTF-8, CamelHttpMethod=POST, CamelHttpUri=http4://d1e53858-2903-4c21-86c0-95edc7a5cef2.uaa.run.aws.io:443/oauth/token, Content-Type=MediaType.APPLICATION_FORM_URLENCODE, TOKENURL=http4://d95edc7a5cef2.uaa.run.aws-usw02-pr.ice.io:443/oauth/token}, BodyType:String, Body:grant_type=password&username=5c06-a495-44db-93f7-67056fa2c061_ingestor&password=7b5e21ef171b02a79f41a0daf3

我确信我实施HTTP4的方式一定有问题。我不需要公开我只需要推送它的Web服务。我不能让这个工作。请帮忙。

1 个答案:

答案 0 :(得分:0)

好吧,这是一个多方面的问题。

第一次我的代理设置不正确。我发现它必须设置IP或名称,没有协议http://的cononical名称。

我喜欢它:

<camelContext     
  id="com.ge.digital.passthru.coreCamelContext"
  trace="true"
  xmlns="http://camel.apache.org/schema/blueprint"
  allowUseOriginalMessage="false"
  streamCache="true"
  errorHandlerRef="deadLetterErrorHandler" >
  <properties>
       <property key="http.proxyHost" value="http://PITC-Zscaler-Americas.proxy.corporate.com"/>
       <property key="http.proxyPort" value="80"/>
  </properties>

但如上所述,这显然是不正确的,并且不使用def中的协议。像:

  <properties>
       <property key="http.proxyHost" value="PITC-Zscaler-Americas.proxy.corporate.com"/>
       <property key="http.proxyPort" value="80"/>
  </properties>

  <properties>
       <property key="http.proxyHost" value="123.123.123.123"/>
       <property key="http.proxyPort" value="80"/>
  </properties>

这给了我一个回应,不是我希望的那个,而是一个给了我希望的回答。 这给了我一个504网关超时,哇,我收到了回报。 :)

第二,终点骑行没有工作......

m.setHeader(Exchange.HTTP_URI, tokenUrl);
使用

<toD uri="${header.TOKENURL}?throwExceptionOnFailure=false" />
<toD uri="${header.TOKENURL}" />

所以我只是尝试了一个简单的路线

<to uri="http4://d1e53858-uaa.run.aws-usw02-pr.ice.io:443/oauth/token?throwExceptionOnFailure=false" />

过度骑行

m.setHeader(Exchange.HTTP_URI, tokenUrl);

其中tokenUrl被定义为

http4://d1e53858-uaa.run.aws-usw02-pr.ice.io:443/oauth/token
这给了我一个504,我希望这是我可以照顾的东西,通常不是,但有时......所以我一直在尝试......直到......我用了实际的地址在HTTP_URI中... ...

https://d1e53858-uaa.run.aws-usw02-pr.ice.io/oauth/token

现在我得到一个CamelHttpResponseCode = 401,CamelHttpResponseText =未经授权

我是一个快乐的露营者,这是我可以使用的东西......然后我测试了相同的替换设置xml中路由中的URI。这也有效。

 <setHeader headerName="CamelHttpMethod">
    <constant>POST</constant>
 </setHeader>
<log message="HTTP4 POST headers: ${headers}" loggingLevel="DEBUG"/>
<setHeader headerName="CamelHttpUri">
    <simple>${header.TOKENURL}?throwExceptionOnFailure=false</simple>
</setHeader>
<to uri="http4://d1e53858-uaa.run.aws-usw02-pr.ice.io/oauth/token?throwExceptionOnFailure=false" />

我仍然试图让动态路线发挥作用,但我对自己拥有的东西感到满意,并会在以后更加关注它。

<toD uri="${header.TOKENURL}?throwExceptionOnFailure=false" />
<toD uri="${header.TOKENURL}" /> -->
谢谢大家,我希望我作为新手帮助别人的挣扎,我不能成为唯一一个有这些愚蠢事件的人。无论如何,欢呼所有人。