網路找到的大都是透過xml設定SSL到RestConfiguration中,後來實驗出透過coding的方法。
Java
import java.io.File; import java.util.Scanner; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.netty4.http.NettyHttpComponent; import org.apache.camel.component.netty4.http.NettyHttpMessage; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.model.rest.RestBindingMode; import org.apache.camel.util.jsse.KeyManagersParameters; import org.apache.camel.util.jsse.KeyStoreParameters; import org.apache.camel.util.jsse.SSLContextParameters; import org.junit.Test; public class TestCamel2 { private EventHandler eventHandler = new EventHandler(); public static class EventHandler { public void receiveServiceEvent(String id, Exchange exchange){ NettyHttpMessage message = exchange.getIn().getBody(NettyHttpMessage.class); System.out.println("body: " + message.getBody()); System.out.println("id: " + id); exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200); } } private SSLContextParameters createSSLContextParameters(){ String keystore = new File("./certificate/.keystore").getAbsolutePath(); String passwd = "changeit"; KeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource(keystore); ksp.setPassword(passwd); KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); kmp.setKeyPassword(passwd); SSLContextParameters scp = new SSLContextParameters(); scp.setKeyManagers(kmp); return scp; } private RouteBuilder createRouteBuilder(CamelContext context){ return new RouteBuilder() { @Override public void configure() throws Exception { NettyHttpComponent nettyComponent = context.getComponent("netty4-http", NettyHttpComponent.class); nettyComponent.getConfiguration().setSslContextParameters(createSSLContextParameters()); restConfiguration().component("netty4-http").port(8444).bindingMode(RestBindingMode.auto).endpointProperty("ssl", "true"); rest("/services/{id}").get().route().bean(eventHandler, "receiveServiceEvent(header.id)"); } }; } @Test public void test() throws Exception { CamelContext context = new DefaultCamelContext(); try { context.addRoutes(createRouteBuilder(context)); context.start(); System.out.println("Enter"); Scanner s = new Scanner(System.in); s.next(); s.close(); } finally { context.stop(); } } }
Ivy
<dependency org="org.apache.camel" name="camel-core" rev="2.20.1"/> <dependency org="org.apache.camel" name="camel-netty4-http" rev="2.20.1"/> <dependency org="org.apache.camel" name="camel-http-common" rev="2.20.1"/> <dependency org="io.netty" name="netty-all" rev="4.1.17.Final"/> <dependency org="org.apache.camel" name="camel-netty4" rev="2.18.3"/>
留言
張貼留言