Search code examples
httpsapache-camelcamel-http

camel-http4 post not working I get no response


this is my first camel HTTP4 implementation and having trouble. I get no response as if the route to URI is wrong and message goes ??? it doesn't make the HTTP4 call it appears?

using camel-version 2.17.0.redhat-630310, java 8

my pom has dependency

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

my route

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

I call the uAARequestTokenProcessor to condition the outbound message.

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

}

it looks like to me that the Exchange.HTTP_URI or HTTP headers are not getting set unless the headers for HTTP4 are different? but I'm following the HTTP4 doc, https://github.com/apache/camel/blob/master/components/camel-http4/src/main/docs/http4-component.adoc

what happens is inside the processor I try to read the HTTP headers and it shows they are null, after being set.

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

The stack trace shows headers but not as 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

THE http4://predhost is a dummy URI in the spring XML and should be over ridden by the exchange.getIn().setHeader(Exchange.HTTP_URI, tokenUrl);

I never reach the URL endpoint and never reach the log message after making the to HTTP4://

        <to uri="http4://predixhost" />
        <log message="After HTTP4 POST: ${body}" loggingLevel="INFO"/> 

I have tried the following but still get no response, no exceptions, nothing. setting the header CamelHttpUri to overirde the default dummy uri, and a dynamic 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> 

I don't know what is wrong? I can't get a response, an exception, it looks like I cannot send to the HTTP4 endpoint. I am reaching now, verified my proxy settings in the environment, and also tried in the camel context, I don't know what is going on or what to do, I get no logging info to go on, ...

<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"/>

I've tried with CURL and it works from command line, I've tried with Postman and it works. thanks for your help.

What this is to do, simply POST a message to an UAA(https) service with a body of:

grant_type=password&username=myUserName&password=myPassword

and headers:

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

and the server will issue an access token.

like this postman example:

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();

Hello I hope there is someone out there that can help, I'm stuck. I have confirmed, I believe that the HTTP4 endpoint is not working. I have a deadletter queue

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

and each exchange sent to this endpoint is tried 3x and then sent to the deadletter queue. the content of the message in the deadletter queue is what should be sent to the HTTP4 endpoint and thus the UAA service.

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

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

the message headers leading in to this look good too.

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

I'm convinced it must be something wrong with the way I'm implementing HTTP4. I do not need to expose a web service I only need to push to it. I can't get this to work. please help.


Solution

  • ok, this was a multi faceted problem.

    1st my proxy setting was incorrect. I found it has to be set with either the IP or the name, cononical name without the protocol http://.

    I had it like:

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

    but as mentioned this apparently is incorrect and do not use the protocol in the def. like:

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

    or

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

    this gave me a response, not the one I was hoping for but one that gave me hope. this gave me a 504 gateway timeout, wow, i was getting something back. :)

    2nd, The endpoint over ride wasn't working...

    m.setHeader(Exchange.HTTP_URI, tokenUrl);
    

    using

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

    so I just tried a simple to in the route

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

    with the over ride

    m.setHeader(Exchange.HTTP_URI, tokenUrl);
    

    where the tokenUrl was defined as

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

    this gives me a 504, I was hoping this was something I could take care of on my side, usually isn't but sometimes... so I kept trying things... until... I used the actual address in the HTTP_URI like...

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

    and now I am getting a CamelHttpResponseCode=401, CamelHttpResponseText=Unauthorized

    I'm one happy camper, this is something I can work with... I then tested the same substitution setting the URI in the route in xml. and this worked also.

     <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" />
    

    I am still trying to get the dynamic route working, but I'm satisfied with what I have and will focus more on that later.

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

    thank you everyone, I hope my struggles as a newbie help others, I can't be the only one with these silly happenings. anyway, cheers all.