尝试使用Camel编写一个简单的HTTPS服务器

时间:2017-08-09 19:54:35

标签: java apache-camel jetty

我试图建立在xverges对Apache Camel Http and SSL的回答之上。 Camel客户端针对正在运行的openssl s_server工作,但是当我尝试替换Camel服务器时,我收到HTTP错误500.这似乎与我作为请求发送的内容有关但错误被埋在骆驼深处,我不知道如何解决它。

我已经创建了私钥和自签名证书以及相应的密钥库(jks),这部分似乎没问题。

要运行openssl s_server,我会:

$ openssl s_server -key ~/camel/resources/dut_rsa2048_key.pem -cert ~/camel/resources/dut_rsa2048_cert.pem -accept 44330 -www

客户是:

package ca.gc.drdc_rddc.linux.utilinux.camel;

import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.http4.HttpComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.util.jsse.KeyManagersParameters;
import org.apache.camel.util.jsse.KeyStoreParameters;
import org.apache.camel.util.jsse.SSLContextParameters;
import org.apache.camel.util.jsse.TrustManagersParameters;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;

public class CamelSSLTLSJavaRouterClient
{
    private static Endpoint setupSSLContext(CamelContext camelContext) throws Exception {
        System.err.println("#### setupSSLContext() starts ####");
        KeyStoreParameters keyStoreParameters = new KeyStoreParameters();
        keyStoreParameters.setResource("~/camel/resources/keystore.jks");
        keyStoreParameters.setPassword("changeit");
        KeyManagersParameters keyManagersParameters = new KeyManagersParameters();
        keyManagersParameters.setKeyStore(keyStoreParameters);
        keyManagersParameters.setKeyPassword("changeit");
        TrustManagersParameters trustManagersParameters = new TrustManagersParameters();
        trustManagersParameters.setKeyStore(keyStoreParameters);
        SSLContextParameters sslContextParameters = new SSLContextParameters();
        sslContextParameters.setKeyManagers(keyManagersParameters);
        sslContextParameters.setTrustManagers(trustManagersParameters);
        HttpComponent httpComponent = camelContext.getComponent("https4", HttpComponent.class);
        httpComponent.setSslContextParameters(sslContextParameters);
        httpComponent.setX509HostnameVerifier(new AllowAllHostnameVerifier());
        return httpComponent.createEndpoint("https4://127.0.0.1:44330");
    }


    public static void main(String[] args)
        throws Exception
    {
        CamelContext context = new DefaultCamelContext();
        System.err.println("#### context created ####");
        ProducerTemplate producer = context.createProducerTemplate();
        System.err.println("#### producer created ####");
        Endpoint httpsEndpoint = setupSSLContext(context);
        System.err.println("#### httpsEndpoint created ####");
        context.addRoutes(new RouteBuilder() {
            public void configure() throws Exception {
                System.err.println("#### configure() starts ####");
                from("timer:demo?period=1s")
                    .log("#### timer-to "+ body().toString())
                    .to(httpsEndpoint)
                    .log("#### timer-to-choice")
                    .choice()
                    .when(simple("${headers.CamelHttpResponseCode} == 200"))
                    .log("#### timer Succès")
                    .otherwise()
                    .log("#### timer Échec")
                ;
                System.err.println("#### configure() ends ####");
            }
        });
        context.start();
        System.err.println("#### context started ####");
        producer.start();
        System.err.println("#### producer started ####");

        try {
            // If I send a non-null body, httpsEndpoint jams
            producer.sendBody("seda:queue", null);
        } catch (CamelExecutionException cee) {
            System.err.println("1) producer.sendBody failed because of " + cee.getCause().getMessage());
        }

        producer.stop();
        Thread.sleep(2000);
        context.stop();
    }
}

这个客户端只是在服务器上激活了几个连接,每个连接都接受它们。

现在我编写一个将运行而不是openssl s_server的Camel服务器:

package ca.gc.drdc_rddc.linux.utilinux.camel;

import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jetty.JettyHttpComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.util.jsse.*;

public class CamelSSLTLSJavaRouterServer
{
    private static void configureSslForJetty(CamelContext context)
    {
        KeyStoreParameters ksp = new KeyStoreParameters();
        ksp.setResource("~/camel/resources/keystore.jks");
        ksp.setPassword("changeit");
        KeyManagersParameters kmp = new KeyManagersParameters();
        kmp.setKeyStore(ksp);
        kmp.setKeyPassword("changeit");
        SSLContextParameters scp = new SSLContextParameters();
        scp.setKeyManagers(kmp);
        JettyHttpComponent jettyComponent = context.getComponent("jetty", JettyHttpComponent.class);
        jettyComponent.setSslContextParameters(scp);
    }


    public static void main(String[] args)
        throws Exception
    {
        CamelContext context = new DefaultCamelContext();

        System.err.println("%%%% Building routes %%%%");
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                configureSslForJetty(context);

                from("jetty:https4://127.0.0.1:44330")
                    .log("%%%% ACCEPT")
                    .process(new Processor() {
                        public void process(Exchange exchange) throws Exception {
                            System.err.println("%%%% jetty processor begins %%%%");
                            exchange.getOut().setBody(
                                "When you go home, tell them of us and say, for your tomorrow, we gave our today."
                            );
                        }
                    }
                    )
                ;
            }
        });

        System.err.println("%%%% Starting context %%%%");
        context.start();
        System.err.println("%%%% Waiting 30 s %%%%");
        Thread.sleep(30000);
        System.err.println("%%%% Stopping context %%%%");
        context.stop();
    }
}

输出显示服务器永远不会退出"来自"线。日志部分显示:

2017-08-09 15:12:08:705 -0400 [Camel (camel-1) thread #0 - timer://demo] INFO route1 - #### timer-to simple{}
2017-08-09 15:12:09:430 -0400 [Camel (camel-1) thread #0 - timer://demo] ERROR org.apache.camel.processor.DefaultErrorHandler - Failed delivery for (MessageId: ID-sds-dut-vb-33499-1502305925227-0-3 on ExchangeId: ID-sds-dut-vb-33499-1502305925227-0-4). Exhausted after delivery attempt: 1 caught: org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking https://127.0.0.1:44330 with statusCode: 500
...
2017-08-09 15:12:09:432 -0400 [Camel (camel-1) thread #0 - timer://demo] WARN org.apache.camel.component.timer.TimerConsumer - Error processing exchange. Exchange[ID-sds-dut-vb-33499-1502305925227-0-4]. Caused by: [org.apache.camel.http.common.HttpOperationFailedException - HTTP operation failed invoking https://127.0.0.1:44330 with statusCode: 500]
2017-08-09 15:12:09:766 -0400 [qtp331122245-17] WARN org.eclipse.jetty.server.HttpChannel - /
java.lang.IllegalStateException: !(Jetty || Servlet 3.0 || ContinuationFilter)
at org.eclipse.jetty.continuation.ContinuationSupport.getContinuation(ContinuationSupport.java:110)
at org.apache.camel.component.jetty.CamelContinuationServlet.doService(CamelContinuationServlet.java:142)

请帮帮忙?

0 个答案:

没有答案