|
@ -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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|