package gov.hn12396.appintegration.mule.client;
import gov.hn12396.appintegration.mule.util.EncoderUtil;
import java.net.URL;
import java.util.Calendar;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;
/**
* 功能描述:模擬客戶端A-即服務調用者,通過該類模擬客戶端發送soap報文給mule,
* 同時把mule的響應報文打印出來做測試
* @author liuxp
*
*/
public class SynClient {
public static void main(String args[]) {
try {
// 創建連接
// ==================================================
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// 創建消息對象
// ===========================================
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
// message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");
// 創建soap消息主體==========================================
SOAPPart soapPart = message.getSOAPPart();// 創建soap部分
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
// 根據要傳給mule的參數,創建消息body內容。具體參數的配置可以參照應用集成接口技術規范1.1版本
// =====================================
SOAPElement bodyElement = body.addChildElement(envelope.createName(
"process", "Request", "http://esb.service.com/"));
bodyElement.addChildElement("ServiceCode").addTextNode("10000061");
bodyElement.addChildElement("OrigAppId").addTextNode("999");
bodyElement.addChildElement("HomeAppId").addTextNode("998");
Calendar c = Calendar.getInstance();
String reqTime = String.valueOf(c.getTimeInMillis());
bodyElement.addChildElement("ReqTime").addTextNode(reqTime);
bodyElement.addChildElement("IpAddress").addTextNode("10.212.40.112");
bodyElement.addChildElement("OrigSerialNo").addTextNode("201205242011");
//(ServiceCode+ OrigAppId+ ReqTime+ IpAddress)簽名
String AppSignature = "10000061"+"999"+reqTime+"10.212.40.112"+"123456";
bodyElement.addChildElement("AppSignature").addTextNode(EncoderUtil.md5(AppSignature));
bodyElement.addChildElement("Version").addTextNode("014");
// bodyElement.addChildElement("RelSessionId").addTextNode("RelSessionId");
// bodyElement.addChildElement("ReplyCode").addTextNode("ReplyCode");
bodyElement.addChildElement("ReplyVersion").addTextNode("05");
bodyElement.addChildElement("TimeOut").addTextNode("30");
// bodyElement.addChildElement("FtpDir").addTextNode("FtpDir");
// bodyElement.addChildElement("FileList").addTextNode("FileList");
bodyElement.addChildElement("serviceParas").addTextNode("<param><name>apptest</name><password>apptest</password></param>");
// Save the message
message.saveChanges();
// 打印客戶端發出的soap報文,做驗證測試
System.out.println(" REQUEST: ");
message.writeTo(System.out);
System.out.println(" ");
/*
* 實際的消息是使用 call()方法發送的,該方法接收消息本身和目的地作為參數,並返回第二個 SOAPMessage 作為響應。
* call方法的message對象為發送的soap報文,url為mule配置的inbound端口地址。
*/
URL url = new URL("http://localhost:9003/WebServiceSyn/process");
System.out.println(url);
// 響應消息
// ===========================================================================
SOAPMessage reply = connection.call(message, url);
//reply.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");
// 打印服務端返回的soap報文供測試
System.out.println("RESPONSE:");
// ==================創建soap消息轉換對象
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// Extract the content of the reply======================提取消息內容
Source sourceContent = reply.getSOAPPart().getContent();
// Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
// Close the connection 關閉連接 ==============
System.out.println("");
connection.close();
/*
* 模擬客戶端A,異常處理測試
*/
SOAPBody ycBody = reply.getSOAPBody();
Node ycResp = ycBody.getFirstChild();
System.out.print("returnValue:"+ycResp.getTextContent());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}