简体   繁体   English

Mule ESB Riak连接器:如何通过SSL端点配置HTTP

[英]Mule ESB Riak connector: How to configure HTTP over SSL endpoints

I have a question regarding the Mule Riak connector and its configuration possibilities. 我对Mule Riak连接器及其配置可能性有疑问。 I would like to configure the connector using an HTTPS endpoint. 我想使用HTTPS端点配置连接器。 Now the connector contains a org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter and not the Mule HttpConnector. 现在,连接器包含org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter而不是Mule HttpConnector。

To make my approach more clear, I tried the following configuration: 为了使方法更清晰,我尝试了以下配置:

<http:connector name="HTTP_HTTPS" cookieSpec="netscape"
    validateConnections="true" sendBufferSize="0" receiveBufferSize="0"
    receiveBacklog="0" clientSoTimeout="10000" serverSoTimeout="10000"
    socketSoLinger="0" doc:name="HTTP-HTTPS" />

<spring:beans>
    <spring:bean id="riakclient"
        class="org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter">
        <spring:property name="httpClient" ref="HTTP_HTTPS"></spring:property>
    </spring:bean>
</spring:beans>

Of course that fails: 当然失败了:

Cannot convert value of type [org.mule.transport.http.HttpConnector] to required type
[org.apache.http.client.HttpClient] for property 'httpClient': no matching editors or conversion
strategy found

Now, can you think of a way to actually reuse an existing HttpConnector (with all that SSL configuration setup)? 现在,您能想到一种实际上重用现有HttpConnector(具有所有SSL配置设置)的方法吗? I actually don't want to configure a HttpClient again and add this to the riak client. 我实际上不想再次配置HttpClient并将其添加到riak客户端。

EDIT 1: Updated with David's approach I added the following configuration straightforward to get the reflection part done: 编辑1:用大卫的方法更新了,我添加了以下简单的配置来完成反射部分:

<spring:beans>
    <!-- get class instance of HttpsConnector for reflection -->
    <spring:bean id="httpsConnectorClass"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <spring:property name="targetObject" ref="HTTP_HTTPS">
        </spring:property>
        <spring:property name="targetMethod">
            <spring:value>getClass</spring:value>
        </spring:property>
    </spring:bean>

    <!-- get method via reflection of HttpsConnector for reflection -->
    <spring:bean id="httpsConnectorMethod"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <spring:property name="targetObject" ref="httpsConnectorClass">
        </spring:property>
        <spring:property name="targetMethod">
            <spring:value>getMethod</spring:value>
        </spring:property>
        <spring:property name="arguments">
            <spring:list>
                <spring:value>doClientConnect</spring:value>
                <spring:value type="java.lang.Class"></spring:value>
            </spring:list>
        </spring:property>
    </spring:bean>

    <!-- set method accessible -->
    <spring:bean id="httpsConnectorMethodAccessible"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <spring:property name="targetObject" ref="httpsConnectorMethod">
        </spring:property>
        <spring:property name="targetMethod">
            <spring:value>setAccessible</spring:value>
        </spring:property>
        <spring:property name="arguments">
            <spring:list>
                <spring:value>true</spring:value>
            </spring:list>
        </spring:property>
    </spring:bean>

    <!-- call accessible doClientConnect() to retrieve HttpClient -->
    <spring:bean id="httpClientFromConnector"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <spring:property name="targetObject" ref="HTTP_HTTPS">
        </spring:property>
        <spring:property name="targetMethod">
            <spring:value>doClientConnect</spring:value>
        </spring:property>
    </spring:bean>

    <spring:bean id="riakclient"
        class="org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter">
        <spring:property name="httpClient" ref="httpClientFromConnector"></spring:property>
    </spring:bean>
</spring:beans>

Sadly, the invocation already fails on the reflection part. 可悲的是,调用在反射部分已经失败了。 What I do in Spring should be equivalent to this Java piece: 我在Spring中所做的工作应等同于这段Java代码:

    HttpsConnector https = new org.mule.transport.http.HttpsConnector(null);
    https.getClass().getMethod("doCientConnect", null).setAccessible(true);

On starting Mule I get the following exception on the reflection part of this bean: 在启动Mule时,我在此bean的反射部分得到以下异常:

    <spring:bean id="httpsConnectorMethod"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <spring:property name="targetObject" ref="httpsConnectorClass">
        </spring:property>
        <spring:property name="targetMethod">
            <spring:value>getMethod</spring:value>
        </spring:property>
        <spring:property name="arguments">
            <spring:list>
                <spring:value>doClientConnect</spring:value>
                <spring:value type="java.lang.Class"></spring:value>
            </spring:list>
        </spring:property>
    </spring:bean>

Exception: 例外:

ERROR 2014-08-26 11:33:22,021 [main] org.mule.module.launcher.application.DefaultMuleApplication: null
java.lang.NoSuchMethodException: org.mule.transport.http.HttpConnector.doClientConnect()
at java.lang.Class.getMethod(Class.java:1665)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'httpsConnectorMethod'

EDIT 2: Helper Class package com.mule.httpclient; 编辑2:助手类包com.mule.httpclient;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.http.client.HttpClient;
import org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter;
import org.mule.transport.http.HttpsConnector;

public class HttpClientAdapter {

    private HttpsConnector httpsConnector;
    private org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter riakClient;

    public HttpClientAdapter() {
    }

    public void setHttpsConnector(HttpsConnector httpsConnector) {
        this.httpsConnector = httpsConnector;
        this.riakClient = new org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter();


        try {
            try {
              Class httpsClass = httpsConnector.getClass();
              Method method = httpsClass.getSuperclass().getDeclaredMethod("doClientConnect", null); 
              method.setAccessible(true);
              // Cast not working due to too different HttpClient versions (3.5 vs. 4.2)
              this.riakClient.setHttpClient((HttpClient) method.invoke(httpsConnector, null));
            } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (NoSuchMethodException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public RiakHttpClientConfigurationAdapter getRiakClient() {
        return riakClient;
    }
}

The HttpClient library versions differ too much in order make the case successful. HttpClient库版本之间的差异太大,以使案例成功。 Will need to somehow adapt those versions too. 也将需要以某种方式改编那些版本。

java.lang.ClassCastException: org.apache.commons.httpclient.HttpClient cannot be cast to org.apache.http.client.HttpClient

I can think of a way! 我可以想办法!

  1. Change the visibility of doClientConnect on the Mule HTTP connector with setAccessible . 使用setAccessible更改Mule HTTP连接器doClientConnect的可见性。
  2. Use Spring to fetch an instance of HttpClient via this method. 使用Spring通过此方法获取HttpClient的实例。
  3. Inject it riakclient 注入riakclient
  4. Profit! 利润!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM