首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mule连接器:如何在SSL端点上配置HTTP

Mule连接器:如何在SSL端点上配置HTTP
EN

Stack Overflow用户
提问于 2014-08-25 17:29:08
回答 1查看 444关注 0票数 1

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

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

代码语言:javascript
运行
复制
<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>

当然,这是失败的:

代码语言:javascript
运行
复制
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的方法更新,我直接添加了以下配置,以完成反射部分:

代码语言:javascript
运行
复制
<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文章:

代码语言:javascript
运行
复制
    HttpsConnector https = new org.mule.transport.http.HttpsConnector(null);
    https.getClass().getMethod("doCientConnect", null).setAccessible(true);

在启动Mule时,我在这个bean的反射部分得到以下异常:

代码语言:javascript
运行
复制
    <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>

异常:

代码语言:javascript
运行
复制
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;

代码语言:javascript
运行
复制
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库版本相差太大,这样才能使情况成功。在某种程度上也需要修改这些版本。

代码语言:javascript
运行
复制
java.lang.ClassCastException: org.apache.commons.httpclient.HttpClient cannot be cast to org.apache.http.client.HttpClient
EN

回答 1

Stack Overflow用户

发布于 2014-08-25 22:04:04

我能想到办法!

  1. doClientConnect更改Mule HTTP connector上的可见性。
  2. 使用Spring通过此方法获取HttpClient的实例。
  3. 注射riakclient
  4. 利润!
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25491241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档