|
@ -0,0 +1,138 @@
|
|
|
package camel.gateway.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.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
import javax.xml.soap.MessageFactory;
|
|
|
import javax.xml.soap.SOAPBody;
|
|
|
import javax.xml.soap.SOAPException;
|
|
|
import javax.xml.soap.SOAPMessage;
|
|
|
import java.io.StringReader;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component("wsProcessor")
|
|
|
public class WsProcessor 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));
|
|
|
}
|
|
|
|
|
|
//webservice 接口请求返回
|
|
|
public SOAPBody responseWs(Exchange exchange) {
|
|
|
XmlMapper xmlMapper = new XmlMapper();
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
try {
|
|
|
Object errorCode = exchange.getIn().getHeader("errorCode");
|
|
|
if (errorCode!=null){
|
|
|
//错误信息返回
|
|
|
LinkedHashMap<Object, Object> errorMap = new LinkedHashMap<>();
|
|
|
errorMap.put("errorCode",errorCode);
|
|
|
String xmlResponse = xmlMapper.writeValueAsString(errorMap);
|
|
|
return createDefaultSoapMessage(xmlResponse);
|
|
|
}
|
|
|
//返回请求结果
|
|
|
Map<String,Object> map = objectMapper.readValue(exchange.getIn().getBody().toString(),Map.class);
|
|
|
String xmlResponse = xmlMapper.writeValueAsString(map);
|
|
|
return createDefaultSoapMessage(xmlResponse);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return createDefaultSoapMessage(e.getClass().getName());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
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());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 请求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(method);
|
|
|
//输出调用结果
|
|
|
System.out.println("*****" + objects[0].toString());
|
|
|
return objects[0].toString();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成ws返回内容
|
|
|
*
|
|
|
* @param requestMessage
|
|
|
* @return
|
|
|
*/
|
|
|
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");
|
|
|
body.addDocument(parseXMLDocument(requestMessage));
|
|
|
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();
|
|
|
}
|
|
|
try {
|
|
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
|
dbf.setNamespaceAware(true);
|
|
|
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
|
|
|
return documentBuilder.parse(
|
|
|
new InputSource(new StringReader(xmlString)));
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|