Bläddra i källkod

camel-ws修復

huangzhiyong 8 år sedan
förälder
incheckning
a34074198c

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

@ -14,8 +14,19 @@ public interface TestWService {
    @WebMethod
    public Object test();
    @WebMethod
    public Object requestWs (
            @WebParam(name = "api") String api,
            @WebParam(name = "param") String param,
            @WebParam(name = "timestamp") String timestamp,
            @WebParam(name = "v") String v,
            @WebParam(name = "appKey") String appKey,
            @WebParam(name = "sign") String sign
    );
    @WebMethod
    public int plus(@WebParam(name = "a") int a, @WebParam(name = "b") int b);
    public Object AddProvider (@WebParam(name = "doctor_id") long doctor_id );
}

+ 0 - 21
hos-camel2/src/main/java/camel/api7/processor/TestWServiceImpl.java

@ -1,21 +0,0 @@
package camel.api7.processor;
import javax.jws.WebService;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/4/20.
 */
@WebService(serviceName = "esbWService",targetNamespace="http://processor.api7.camel",endpointInterface = "camel.api7.processor.TestWService")
public class TestWServiceImpl implements TestWService {
    @Override
    public Object test() {
        return "ESB 中的webservice。。。。。";
    }
    @Override
    public int plus(int a, int b) {
        return a+b;
    }
}

+ 11 - 15
hos-camel2/src/main/java/camel/api7/processor/TestWsConfig.java

@ -2,20 +2,16 @@ package camel.api7.processor;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
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;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/4/20.
 */
@Configuration
//@Configuration
public class TestWsConfig {
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
@ -27,15 +23,15 @@ public class TestWsConfig {
        return new SpringBus();
    }
    @Bean
    public TestWService testWService() {
        return new TestWServiceImpl();
    }
//    @Bean
//    public TestWService testWService() {
//        return new TestWServiceImpl();
//    }
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), testWService());
        endpoint.publish("/ws");
        return endpoint;
    }
//    @Bean
//    public Endpoint endpoint() {
//        EndpointImpl endpoint = new EndpointImpl(springBus(), testWService());
//        endpoint.publish("/ws");
//        return endpoint;
//    }
}

+ 24 - 44
hos-camel2/src/main/java/camel/api7/processor/ToWsProcessor.java

@ -3,12 +3,13 @@ package camel.api7.processor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.message.MessageContentsList;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.*;
@ -17,46 +18,20 @@ import java.util.List;
import java.util.Map;
@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());
        }
public class ToWsProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        MessageContentsList args = exchange.getIn().getBody(MessageContentsList.class);
        exchange.getOut().setHeader("api", args.get(0));
        exchange.getOut().setHeader("param", args.get(1));
        exchange.getOut().setHeader("timestamp", args.get(2));
        exchange.getOut().setHeader("v", args.get(3));
        exchange.getOut().setHeader("appKey", args.get(4));
        exchange.getOut().setHeader("sign", args.get(5));
    }
    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");
//        }
    public SOAPBody responseWs(Exchange exchange) {
        try {
//            String requestText = requestWs(exchange, wsdl, "test");
            XmlMapper xmlMapper = new XmlMapper();
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,Object> map = objectMapper.readValue(exchange.getIn().getBody().toString(),Map.class);
@ -68,7 +43,7 @@ public class ToWsProcessor {
        }
    }
    public SOAPMessage plus(Exchange exchange) {
    public SOAPBody request(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) {
@ -111,21 +86,26 @@ public class ToWsProcessor {
     * @param requestMessage
     * @return
     */
    public static SOAPMessage createDefaultSoapMessage(String requestMessage) {
    public static SOAPBody 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("body");
//            QName payloadName = new QName("http://gateway.api", "test", "ns1");
//            SOAPBodyElement payload = body.addBodyElement(payloadName);
//            SOAPElement message = payload.addChildElement("body");
            body.addDocument(parseXMLDocument(requestMessage));
            return soapMessage;
            return body;
        } catch (SOAPException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 解析XML字符串
     * @param xmlString
     * @return
     */
    public static Document parseXMLDocument(String xmlString) {
        if (xmlString == null) {
            throw new IllegalArgumentException();

+ 0 - 59
hos-camel2/src/main/java/camel/api7/processor/WsProcessor.java

@ -1,59 +0,0 @@
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.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
@Component("wsProcessor")
public class WsProcessor implements Processor {
    public void process(final Exchange exchange) throws SOAPException {
        Object body = exchange.getIn().getBody();
        SOAPMessage soapMessage = (SOAPMessage)exchange.getIn().getBody();
        SOAPBody soapBody = soapMessage.getSOAPBody();
        String method = exchange.getIn().getHeader("operatioNname", String.class);
        if("test".equals(method))
        {
        }else {
        }
    }
    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");
        //getUser 为接口中定义的方法名称  张三为传递的参数   返回一个Object数组
        Object[] objects=client.invoke("test");
        //输出调用结果
        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");
    }
    public void responseWs(Exchange exchange) throws Exception {
    }
}

+ 29 - 15
hos-camel2/src/main/java/camel/api7/route/CxfConsumerTest.java

@ -1,7 +1,11 @@
package camel.api7.route;
import camel.api7.processor.ToWsProcessor;
import camel.gateway.processor.GatewayProcessor;
import camel.log.TracerFormatter;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.apache.camel.processor.interceptor.Tracer;
import org.springframework.stereotype.Component;
@Component
@ -10,26 +14,36 @@ 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.TestWService"
            + "&dataFormat=CXF_MESSAGE";
//            + "&dataFormat=CXF_MESSAGE"
            ;
    // START SNIPPET: example
    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());
        this.getContext().setUseMDCLogging(true);
        this.getContext().setTracing(true);
        Tracer tracer = new Tracer();
        TracerFormatter formatter = new TracerFormatter();
        formatter.setShowHeaders(true);
        formatter.setShowBody(true);
        formatter.setShowBodyType(true);
        formatter.setShowOutHeaders(true);
        formatter.setShowOutBody(true);
        formatter.setShowOutBodyType(true);
        formatter.setMultiline(true);
        tracer.setFormatter(formatter);
        this.getContext().addInterceptStrategy(tracer);
        this.getContext().getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true");
        this.getContext().getProperties().put(Exchange.BEAN_MULTI_PARAMETER_ARRAY, "true");
        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")
                ;
                .setHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY, constant(true))
                .process(new ToWsProcessor())
                .routingSlip(method(GatewayProcessor.class, "route"))
                .to("bean:toWsProcessor?method=responseWs")
        ;
    }

+ 0 - 1
hos-camel2/src/main/java/camel/gateway/processor/GatewayProcessor.java

@ -37,7 +37,6 @@ public class GatewayProcessor implements Processor {
        exchange.getOut().setHeader("v", headers.get("v"));
        exchange.getOut().setHeader("appKey", headers.get("appKey"));
        exchange.getOut().setHeader("sign", headers.get("sign"));
    }