huangzhiyong 8 years ago
parent
commit
10c5dd6c21

+ 0 - 6
hos-camel2/pom.xml

@ -85,12 +85,6 @@
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-spring-ws-starter -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-ws-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-cxf</artifactId>

+ 1 - 1
hos-camel2/src/main/java/camel/api7/processor/TestClient.java

@ -11,7 +11,7 @@ public class TestClient {
    public static void main(String args[]) throws Exception{
                 JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
                 org.apache.cxf.endpoint.Client client =dcf.createClient("http://127.0.0.1:8001/esb/ws?wsdl");
                 org.apache.cxf.endpoint.Client client =dcf.createClient("http://192.168.131.101:3333/soap/ws?wsdl");
                 //getUser 为接口中定义的方法名称  张三为传递的参数   返回一个Object数组
                 Object[] objects=client.invoke("test");
                 //输出调用结果

+ 2 - 1
hos-camel2/src/main/java/camel/api7/processor/TestWService.java

@ -1,6 +1,7 @@
package camel.api7.processor;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
@ -14,7 +15,7 @@ public interface TestWService {
    @WebMethod
    public Object test();
    @WebMethod
    public int jian(int a, int b);
    public int plus(@WebParam(name = "a") int a, @WebParam(name = "b") int b);
}

+ 2 - 6
hos-camel2/src/main/java/camel/api7/processor/TestWServiceImpl.java

@ -1,8 +1,5 @@
package camel.api7.processor;
import org.apache.camel.component.cxf.CxfEndpoint;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
@ -11,15 +8,14 @@ import javax.jws.WebService;
 * Created at 2017/4/20.
 */
@WebService(serviceName = "esbWService",targetNamespace="http://processor.api7.camel",endpointInterface = "camel.api7.processor.TestWService")
@Component("testWService")
public class TestWServiceImpl extends CxfEndpoint implements TestWService {
public class TestWServiceImpl implements TestWService {
    @Override
    public Object test() {
        return "ESB 中的webservice。。。。。";
    }
    @Override
    public int jian(int a, int b) {
    public int plus(int a, int b) {
        return a+b;
    }
}

+ 2 - 1
hos-camel2/src/main/java/camel/api7/processor/TestWsConfig.java

@ -6,6 +6,7 @@ import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@ -14,7 +15,7 @@ import javax.xml.ws.Endpoint;
 * @vsrsion 1.0
 * Created at 2017/4/20.
 */
//@Configuration
@Configuration
public class TestWsConfig {
    @Bean
    public ServletRegistrationBean dispatcherServlet() {

+ 98 - 23
hos-camel2/src/main/java/camel/api7/processor/ToWsProcessor.java

@ -1,43 +1,118 @@
package camel.api7.processor;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;
import javax.xml.soap.SOAPMessage;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import java.util.List;
@Component("toWsProcessor")
public class ToWsProcessor {
    public SOAPMessage processSOAP(Exchange exchange) {
        // Since the Camel-CXF endpoint uses a list to store the parameters
        // and bean component uses the bodyAs expression to get the value
        // we'll need to deal with the parameters ourself
        List list = exchange.getIn().getBody(List.class);
        if (list == null) {
            System.out.println("Incoming null message detected...");
            return createDefaultSoapMessage("null");
        }
        SOAPMessage soapMessage = null;
        try {
            if (list.isEmpty()){
                soapMessage = (SOAPMessage) list.get(0);
            }
            SOAPPart sp = soapMessage.getSOAPPart();
            SOAPEnvelope se = sp.getEnvelope();
            SOAPBody sb = se.getBody();
            String requestText = sb.getFirstChild().getNextSibling().getTextContent();
            System.out.println(requestText);
            return createDefaultSoapMessage(requestText);
        } catch (Exception e) {
            e.printStackTrace();
            return createDefaultSoapMessage(e.getClass().getName());
        }
    }
@Component
public class ToWsProcessor implements Processor {
    public void process(final Exchange exchange) {
        SOAPMessage soapMessage = (SOAPMessage)exchange.getIn().getBody();
    public SOAPMessage test(Exchange exchange) {
//        String wsdl = "http://127.0.0.1:8001/esb/ws?wsdl";
//        List soapMessage =  exchange.getIn().getBody(List.class);
//        if (soapMessage == null ) {
//            System.out.println("Incoming null message detected...");
//            return createDefaultSoapMessage("null");
//        }
        try {
//            String requestText = requestWs(exchange, wsdl, "test");
            return createDefaultSoapMessage(exchange.getIn().getBody().toString());
        } catch (Exception e) {
            e.printStackTrace();
            return createDefaultSoapMessage(e.getClass().getName());
        }
    }
    public SOAPMessage plus(Exchange exchange) {
        String wsdl = "http://127.0.0.1:8001/esb/ws?wsdl";
        SOAPMessage soapMessage = (SOAPMessage) exchange.getIn().getBody(List.class).get(0);
        if (soapMessage == null) {
            System.out.println("Incoming null message detected...");
            return createDefaultSoapMessage("null");
        }
        try {
            String requestText = requestWs(exchange, wsdl, "plus");
            return createDefaultSoapMessage(requestText);
        } catch (Exception e) {
            e.printStackTrace();
            return createDefaultSoapMessage(e.getClass().getName());
        }
    }
    public void requestWs(Exchange exchange) throws Exception {
        JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client =dcf.createClient("http://127.0.0.1:2222/soap/ws?wsdl");
    /**
     * 请求ws
     *
     * @param exchange
     * @param method
     * @return
     * @throws Exception
     */
    public String requestWs(Exchange exchange, String wsdl, String method) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = dcf.createClient(wsdl);
        //getUser 为接口中定义的方法名称  张三为传递的参数   返回一个Object数组
        Object[] objects=client.invoke("test");
        Object[] objects = client.invoke(method);
        //输出调用结果
        System.out.println("*****"+objects[0].toString());
//        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
//        SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
//        QName payloadName = new QName("http://processor.api7.camel/", "esbWService", "test");
//        SOAPBodyElement payload = body.addBodyElement(payloadName);
//        SOAPElement message = payload.addChildElement("responseType");
        System.out.println("*****" + objects[0].toString());
        return objects[0].toString();
    }
    public void responseWs(Exchange exchange) throws Exception {
    /**
     * 生成ws返回内容
     *
     * @param requestMessage
     * @return
     */
    public static SOAPMessage createDefaultSoapMessage(String requestMessage) {
        try {
            SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
            SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
            QName payloadName = new QName("http://gateway.api", "test", "ns1");
            SOAPBodyElement payload = body.addBodyElement(payloadName);
            SOAPElement message = payload.addChildElement("response");
            message.addTextNode(requestMessage);
            return soapMessage;
        } catch (SOAPException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

+ 1 - 6
hos-camel2/src/main/java/camel/api7/processor/WsProcessor.java

@ -2,23 +2,18 @@ package camel.api7.processor;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.cxf.CxfPayload;
import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import java.util.List;
@Component("wsProcessor")
public class WsProcessor implements Processor {
    public void process(final Exchange exchange) throws SOAPException {
        CxfPayload<SoapHeader> payload = exchange.getIn().getBody(CxfPayload.class);
        List<Source> elements = payload.getBodySources();
        Object body = exchange.getIn().getBody();
        SOAPMessage soapMessage = (SOAPMessage)exchange.getIn().getBody();
        SOAPBody soapBody = soapMessage.getSOAPBody();
        String method = exchange.getIn().getHeader("operatioNname", String.class);

+ 24 - 44
hos-camel2/src/main/java/camel/api7/route/CxfConsumerTest.java

@ -1,6 +1,7 @@
package camel.api7.route;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.springframework.stereotype.Component;
@Component
@ -8,53 +9,32 @@ import org.springframework.stereotype.Component;
public class CxfConsumerTest extends RouteBuilder {
    protected static final String SIMPLE_ENDPOINT_ADDRESS = "http://192.168.131.101:3333/soap/ws";
    protected static final String SIMPLE_ENDPOINT_URI = "cxf:" + SIMPLE_ENDPOINT_ADDRESS
            + "?serviceClass=camel.api7.processor.TestWServiceImpl"
            + "&dataFormat=PAYLOAD";
            + "?serviceClass=camel.api7.processor.TestWService"
            + "&dataFormat=CXF_MESSAGE";
    // START SNIPPET: example
            public void configure() {
                from(SIMPLE_ENDPOINT_URI).log("log:msg")
                .to("cxf:bean:testWService?dataFormat=CXF_MESSAGE");
            }
        };
    // END SNIPPET: example
//    @Test
//    public void testInvokingServiceFromCXFClient() throws Exception {
//        ClientProxyFactoryBean proxyFactory = new ClientProxyFactoryBean();
//        ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean();
//        clientBean.setAddress(SIMPLE_ENDPOINT_ADDRESS);
//        clientBean.setServiceClass(HelloService.class);
//        clientBean.setBus(BusFactory.newInstance().createBus());
//
//        HelloService client = (HelloService) proxyFactory.create();
//
//        String result = client.echo(TEST_MESSAGE);
//        assertEquals("We should get the echo string result from router", result, "echo " + TEST_MESSAGE);
//
//        Boolean bool = client.echoBoolean(Boolean.TRUE);
//        assertNotNull("The result should not be null", bool);
//        assertEquals("We should get the echo boolean result from router ", bool.toString(), "true");
//    }
//
//    @Test
//    public void testXmlDeclaration() throws Exception {
//        String response = template.requestBody(SIMPLE_ENDPOINT_ADDRESS, ECHO_REQUEST, String.class);
//        assertTrue("Can't find the xml declaration.", response.startsWith("<?xml version='1.0' encoding="));
//    }
//
//    @Test
//    public void testPublishEndpointUrl() throws Exception {
//        String response = template.requestBody(SIMPLE_ENDPOINT_ADDRESS + "?wsdl", null, String.class);
//        assertTrue("Can't find the right service location.", response.indexOf("http://www.simple.com/services/test") > 0);
//    }
    public void configure() {
//        CxfComponent cxfComponent = new CxfComponent(getContext());
//        CxfEndpoint serviceEndpoint = new CxfEndpoint("http://192.168.131.101:3333/soap/ws", cxfComponent);
//        serviceEndpoint.setServiceClass(TestWService.class);
//        serviceEndpoint.setDataFormat(DataFormat.CXF_MESSAGE);
        // Here we just pass the exception back, don't need to use errorHandler
        errorHandler(noErrorHandler());
        from(SIMPLE_ENDPOINT_URI)
                .choice()
                .when(header(CxfConstants.OPERATION_NAME).isEqualTo("plus"))
                .to("bean:toWsProcessor?method=plus")
                .when(header(CxfConstants.OPERATION_NAME).isEqualTo("test"))
                .to("restlet:http://192.168.1.221:10000/api/v1.0/admin/doctors/admin/13?autoCloseStream=true")
                .to("bean:toWsProcessor?method=test")
                ;
    }
};