142.17. 高度な使用方法
HTTP プロデューサーをより詳細に制御する必要がある場合は、さまざまなクラスを設定してカスタム動作を提供する HttpComponent
を使用する必要があります。
142.17.1. HTTP クライアントの SSL 設定
JSSE 設定ユーティリティーの使用
Camel 2.8 より、HTTP4 コンポーネントは Camel JSSE 設定ユーティリティーを介して SSL/TLS 設定をサポートします。 このユーティリティーは、エンドポイントおよびコンポーネントレベルで記述し、設定する必要のあるコンポーネント固有のコードの量を大幅に削減します。 以下の例は、HTTP4 コンポーネントでユーティリティーを使用する方法を示しています。
コンポーネントのプログラムによる設定
KeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource("/users/home/server/keystore.jks"); ksp.setPassword("keystorePassword"); KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); kmp.setKeyPassword("keyPassword"); SSLContextParameters scp = new SSLContextParameters(); scp.setKeyManagers(kmp); HttpComponent httpComponent = getContext().getComponent("https4", HttpComponent.class); httpComponent.setSslContextParameters(scp);
エンドポイントの Spring DSL ベースの設定
... <camel:sslContextParameters id="sslContextParameters"> <camel:keyManagers keyPassword="keyPassword"> <camel:keyStore resource="/users/home/server/keystore.jks" password="keystorePassword"/> </camel:keyManagers> </camel:sslContextParameters>... ... <to uri="https4://127.0.0.1/mail/?sslContextParameters=#sslContextParameters"/>...
Apache HTTP クライアントを直接設定
基本的には、camel-http4 コンポーネントは Apache HttpClient の上部に構築されます。詳細は、SSL/TLS のカスタマイズ を参照するか、org.apache.camel.component.http4.HttpsServerTestSupport
ユニットテストベースクラスを調べます。
また、カスタム org.apache.camel.component.http4.HttpClientConfigurer
を実装して、完全な制御が必要な場合に http クライアントで設定を行うことができます。
ただし、キーストアとトラストストア のみ を指定する場合は、Apache HTTP HttpClientConfigurer
でこれを行うことができます。以下に例を示します。
KeyStore keystore = ...; KeyStore truststore = ...; SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", 443, new SSLSocketFactory(keystore, "mypassword", truststore)));
次に、HttpClientConfigurer
を実装するクラスを作成し、上記の例ごとにキーストアまたはトラストストアを提供する https プロトコルを登録する必要があります。その後、Camel ルートビルダークラスから、以下のようにフックすることができます。
HttpComponent httpComponent = getContext().getComponent("http4", HttpComponent.class); httpComponent.setHttpClientConfigurer(new MyHttpClientConfigurer());
Spring DSL を使用してこれを行う場合は、URI を使用して HttpClientConfigurer
を指定できます。以下に例を示します。
<bean id="myHttpClientConfigurer" class="my.https.HttpClientConfigurer"> </bean> <to uri="https4://myhostname.com:443/myURL?httpClientConfigurer=myHttpClientConfigurer"/>
HttpClientConfigurer を実装し、上記のようにキーストアとトラストストアを設定する限り、問題なく動作します。
HTTPS を使用した gotchas の認証
エンドユーザーは、HTTPS での認証に問題があると報告されました。この問題は最終的に、カスタムの設定された org.apache.http.protocol.HttpContext
を指定して解決されました。
- 1.HttpContexts に(Spring)ファクトリーを作成します。
public class HttpContextFactory { private String httpHost = "localhost"; private String httpPort = 9001; private BasicHttpContext httpContext = new BasicHttpContext(); private BasicAuthCache authCache = new BasicAuthCache(); private BasicScheme basicAuth = new BasicScheme(); public HttpContext getObject() { authCache.put(new HttpHost(httpHost, httpPort), basicAuth); httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache); return httpContext; } // getter and setter }
- 2.Spring アプリケーションコンテキストファイルで HttpContext を宣言します。
<bean id="myHttpContext" factory-bean="httpContextFactory" factory-method="getObject"/>
- 3.http4 URL のコンテキストを参照します。
<to uri="https4://myhostname.com:443/myURL?httpContext=myHttpContext"/>
異なる SSLContextParameters の使用
HTTP4 コンポーネントは、コンポーネントごとに org.apache.camel.util.jsse.SSLContextParameters
の 1 つのインスタンスのみをサポートします。2 つ以上の異なるインスタンスを使用する必要がある場合は、以下のように複数の HTTP4 コンポーネントを設定する必要があります。2 つのコンポーネントがあり、それぞれ sslContextParameters
プロパティーの独自のインスタンスを使用します。
<bean id="http4-foo" class="org.apache.camel.component.http4.HttpComponent"> <property name="sslContextParameters" ref="sslContextParams1"/> <property name="x509HostnameVerifier" ref="hostnameVerifier"/> </bean> <bean id="http4-bar" class="org.apache.camel.component.http4.HttpComponent"> <property name="sslContextParameters" ref="sslContextParams2"/> <property name="x509HostnameVerifier" ref="hostnameVerifier"/> </bean>