java - Mule ESB Riak 连接器 : How to configure HTTP over SSL endpoints

标签 java ssl mule riak

我对 Mule Riak 连接器及其配置可能性有疑问。 我想使用 HTTPS 端点配置连接器。现在连接器包含一个 org.mule.modules.riak.config.RiakHttpClientConfigurationAdapter 而不是 Mule HttpConnector。

为了使我的方法更加清晰,我尝试了以下配置:

<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>

当然失败了:

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

现在,您能想出一种方法来实际重用现有的 HttpConnector(具有所有 SSL 配置设置)吗?我实际上不想再次配置 HttpClient 并将其添加到 riak 客户端。

编辑 1:更新了 David 的方法 我直接添加了以下配置来完成反射部分:

<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>

遗憾的是,反射部分的调用已经失败。 我在 Spring 中所做的应该相当于这个 Java 部分:

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

在启动 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>

异常:

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'

编辑 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;
    }
}

为了使案例成功,HttpClient 库版本差异太大。 也需要以某种方式调整这些版本。

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

最佳答案

我能想到办法!

  1. 更改 doClientConnectMule HTTP connector 上的可见性使用 setAccessible
  2. 使用 Spring 通过此方法获取 HttpClient 的实例。
  3. 注入(inject) riakclient
  4. 利润!

关于java - Mule ESB Riak 连接器 : How to configure HTTP over SSL endpoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25491241/

相关文章:

java - 是否可以使用 java FileReader 或 java 的替代方案从 pdf 中读取文本?

java - apache Spark - 迭代地跳过并从 RDD 中获取

java - 使用 ORDER BY 和 UNION 的 ORACLE 查询

java - Spring Security 不允许登录页面

php - 无法使用 twilio 发送短信发生错误

java - java如何选择最强的密码在jsse中使用?

maven - tomcat上的mule maven应用程序

java - mule - 映射数组或对象 JSON 响应

php - Laravel 回显服务器在 web 上工作,但不使用 api 进行身份验证

java - 如何使用 Mule 读取邮件并插入数据库?