尝试通过Tomcat内部的骆驼调用外部HTTP Post请求

时间:2018-12-28 18:54:09

标签: java http tomcat routes apache-camel

我对Apache Camel并不陌生,并且能够成功设置初始的hello servlet。我在Tomcat容器内使用Apache Camel。

下面是我的camel-config.xml

  <camelContext xmlns="http://camel.apache.org/schema/spring">

<route id="helloRoute">
  <!-- incoming requests from the servlet is routed -->
  <from uri="servlet:hello"/>
  <choice>
    <when>
      <!-- is there a header with the key name? -->
      <header>name</header>
      <!-- yes so return back a message to the user -->
      <transform>
        <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today?</simple>
      </transform>
    </when>
    <otherwise>
      <!-- if no name parameter then output a syntax to the user -->
      <transform>
        <constant>Add a name parameter to uri, eg ?name=foo</constant>
      </transform>
    </otherwise>
  </choice>
</route>
<route id="svRoute">
    <from uri="servlet:camel/sxx-search"/>
    <to uri="https4://sxx.abc.com/sxx/sxx.php"/>
</route>
</camelContext>

这是我的web.xml中的代码

<servlet>
        <servlet-name>CamelServlet</servlet-name>
        <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
      </servlet>

      <!-- Camel servlet mapping -->
      <servlet-mapping>
        <servlet-name>CamelServlet</servlet-name>
        <url-pattern>/camel/*</url-pattern>
</servlet-mapping>

当我打     http://localhost:port/camel/hello?name=Mr 我得到了想要的答复。

当前,在下面的代码中,我以XML作为发布请求命中了一个外部HTTP URL,并作为响应接收了XML(String)

String xml="<some input to the httppost>"
HttpPost httpPost = new HttpPost("camel/sxx-search"); //tryin to map this in route defined in config.xml above
 SSLContext sslctx= SSLContexts.createSystemDefault();

    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            sslctx,
            new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" },
            null,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());

    CloseableHttpClient httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .addInterceptorFirst(new RequestAcceptEncoding())   // adds gzip encoding header
            .build();

    CloseableHttpResponse response = null;
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("xml", xml)); // URLEncoding taken care of in the next line

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        response = httpclient.execute(httpPost);
        result = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch(Exception e) {...}

现在,我想通过骆驼路线执行此httpPost请求。谁能帮助我指导正确的方向?就像我如何更改上面的http代码以通过骆驼路线。

我尝试使用

camel/sxx-search
上面写过的HttpPost中的

。这样它就可以通过骆驼到达

"https4://sxx.abc.com/sxx/sxx.php" 

并点击

http://localhost:8080/camel/sxx-search

应用程序抛出404错误。

1 个答案:

答案 0 :(得分:0)

您是否尝试过将http方法设置为POST?

camel docs包含如何在消息头中进行设置的示例。例如

  <route>
    <from uri="direct:start"/>
    <setHeader headerName="CamelHttpMethod">
        <constant>POST</constant>
    </setHeader>
    <to uri="http4://www.google.com"/>
    <to uri="mock:results"/>
  </route>

我认为您还需要在消息正文中设置要发布的数据。