How to access a remote web service by Camel CXF endpoint

傷痕小丑魚發表於2016-01-26
  • There was a litte infomation about camel proxy webservice with local
    wsdl or remot url in the side of GFW,even in the stackoverfllow.

  • So,i will answer the question of the title,also see the q in

http://stackoverflow.com/questions/29422493/how-to-access-a-remote-web-service-by-camel-cxf-endpoint

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cxf="http://camel.apache.org/schema/cxf"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://camel.apache.org/schema/spring 
        http://camel.apache.org/schema/spring/camel-spring.xsd
        http://camel.apache.org/schema/cxf
        http://camel.apache.org/schema/cxf/camel-cxf.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" />

    <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
    <bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    
    <!--<bean id="servlet" class="org.apache.camel.component.servlet.CamelHttpTransportServlet"/>-->

    <bean id="servlet" class="org.apache.camel.component.http.HttpComponent">
            <property name="camelContext" ref="testCamelContext" />
    </bean>
    
    <bean id="wss4jInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> 
         <property name="properties"> 
             <map> 
                 <entry key="action" value="UsernameToken Timestamp"/> 
                 <entry key="passwordType" value="PasswordDigest"/> 
                 <entry key="ws-security.is-bsp-compliant" value="false"/>
                 <entry key="passwordCallbackClass" value="camel.UTPasswordCallback"/>
             </map> 
         </property> 
     </bean> 
     
     <bean id="wss4jOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
         <constructor-arg>
             <map>
                 <entry key="action" value="UsernameToken Timestamp"/>
                 <entry key="passwordType" value="PasswordText"/>
                 <entry key="user" value="Alice"/>
                 <entry key="passwordCallbackClass" value="camel.UTPasswordCallback"/>
             </map>
         </constructor-arg>
        </bean>
        
    
    <!--釋出一個前置webservice
    <cxf:cxfEndpoint id="orderEndpoint"
        address="http://localhost:8999/CXF_HELLO_ObjectSpringService"
        serviceClass="com.lucky.IHello" />-->

    <!--利用wsdl生成服務端對映 all the param will find in wsdl-->   
    <!-- address is what this proxy service address -->   
    <!-- wsdlURL is the real webservice wsdl --> 
    <!-- xmlns:s is the targetNamespace of the header definitions-->           
    <cxf:cxfEndpoint id="orderEndpoint"
                   address="http://localhost:8989/CXF_HELLO_ObjectSpringService/IHello"
                   wsdlURL="/WEB-INF/wsdl/IHello.wsdl"
                   xmlns:s="http://impl.server.cxf.banana.com/">
        <cxf:inInterceptors>
            <ref bean="loggingInInterceptor"/>
            <ref bean="wss4jInInterceptor"/>
        </cxf:inInterceptors>
        <cxf:outInterceptors>
             <ref bean="loggingOutInterceptor"/>
        </cxf:outInterceptors>
    </cxf:cxfEndpoint>
    
    <!--<cxf:cxfEndpoint id="orderEndpoint2"
                   address="http://localhost:8081/wsproxy/sy/sysi2"
                   wsdlURL="/WEB-INF/wsdl/IHello.wsdl"
                   xmlns:s="http://impl.server.cxf.banana.com/">
        <cxf:inInterceptors>
            <ref bean="loggingInInterceptor"/>
            <ref bean="wss4jInInterceptor"/>
        </cxf:inInterceptors>
        <cxf:outInterceptors>
             <ref bean="loggingOutInterceptor"/>
        </cxf:outInterceptors>
    </cxf:cxfEndpoint>-->
      
                       
    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">  
        <package>com.lucky</package>  
    </camelContext>
</beans>

routConfig.properties

#server1
server1.address=http://localhost:9000/CXF_HELLO_ObjectSpringService/IHello
server1.wsdl=http://localhost:9000/CXF_HELLO_ObjectSpringService/IHello?wsdl
server1.endPointName=orderEndpoint

RouteLoad.java



public class RouteLoad extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        //Properties prop = new Properties();
        InputStream in = RouteLoad.class
                .getResourceAsStream("./../../routConfig.properties");
        PropertiesUtil prop = new PropertiesUtil(in);
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        String key = "";
        String old_key = "";
        String configName = "";
        RoutBean routBean = new RoutBean();
        List<Object> keyValue = prop.getKeyList();
        for (Iterator<?> it = keyValue.iterator(); it.hasNext();) {
            key = (String) it.next();
            if (!"".equals(old_key)
                    && !key.substring(0, 7).equals(old_key.substring(0, 7))) {
                createCxfEndpoint(routBean.getAddress(),routBean.getWsdl(),routBean.getEndPointName());
            } 

            configName = key.split("\.")[1];
            if ("address".equals(configName)) {
                routBean.setAddress((String) prop.get(key));
            } else if ("wsdl".equals(configName)) {
                routBean.setWsdl((String) prop.get(key));
            } else if ("endPointName".equals(configName)) {
                routBean.setEndPointName((String) prop.get(key));
            }

            if(!it.hasNext()){
                createCxfEndpoint(routBean.getAddress(),routBean.getWsdl(),routBean.getEndPointName());
            }
            old_key = key;
            
        }
    }

    public void createCxfEndpoint(String address,String wsdl,String endPointName) {
        Endpoint cxfEndpoint = endpoint("cxf:" + address // serviceAddress
                + "?" 
                +"wsdlURL="+ wsdl // wsdl
                + "&" + "dataFormat=MESSAGE" // dataformat type dataFormat=PAYLOAD
        ); 
        
        
        
            from("cxf:bean:" + endPointName + "?dataFormat=MESSAGE").to("log:loggingOutInterceptor")
            //.process(myProc)
            .to(cxfEndpoint).to("log:loggingOutInterceptor");
        
        //send multi service
        //from("activemq:queue:foo").multicast().to("seda:foo", "seda:bar");
        
//        from("cxf:bean:" + routBean.getEndPointName() + "?dataFormat=MESSAGE")
//        .to("log:input")
//        .process(new Processor(){
//            @Override
//            public void process(Exchange exchange) throws Exception {
//                addWSSESecurityHeader(exchange, "login","password");
//            }
//        })
//        .to(cxfEndpoint)
//        .to("log:output");
    }
}

that all ,thanks for your coming!

相關文章