差異處
這裏顯示兩個版本的差異處。
java:apache_camel:rest_with_netty_http_and_ssl [2017/11/22 20:58] tony |
java:apache_camel:rest_with_netty_http_and_ssl [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>camel}} | ||
- | ====== Apply the REST with Netty-http and SSL ====== | ||
- | 網路找到的大部分都是透過xml的設定方法,後來實驗出一個可行方法分享給大家。 | ||
- | ===== Java ===== | ||
- | <code 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(); | ||
- | } | ||
- | } | ||
- | } | ||
- | </code> | ||
- | ===== Ivy ===== | ||
- | <code xml> | ||
- | <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"/> | ||
- | </code> | ||
- | ===== Reference ===== | ||
- | * [[http://camel.apache.org/netty4-http.html|netty4-http]] | ||
- | * [[http://camel.apache.org/camel-configuration-utilities.html|camel-configuration-utilities]] | ||
- | |||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |