SOAP Test in JAVA

package test;

import org.w3c.dom.Document;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import java.net.URL;

import java.nio.charset.Charset;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;


/**
 * QName 創建多個xmlns不成功
 * 使用String編寫SOAP Header
 * @author glacierliu
 *
 */
public class Ctest1 {
    // private static String ns = "http://service.soap.org/";
    // private static String wsdlUrl = "http://localhost:8989/ms?wsdl";
    private static final QName SERVICE_NAME = new QName("http://example1/",
            "MyServiceImpl");
    private static final QName PORT_NAME = new QName("http://example1/",
            "MyServiceImplPort");
    private static final String OTA = "http://www.opentravel.org/OTA/2003/05";
    private static final String SEEKDA = "http://connect.seekda.com/2009/04";
    private static final String EndPointURL = "https://switch.seekda.com/switch/1.009/ChannelConnect";

    public static void main(String[] args) {
        Ctest1.test1();
    }

    public static void test1() {
        try {
            //1 創建服務(Service)
            //URL url = new URL(wsdlUrl);
            //QName sname = new QName(ns,"MyServiceImplService");
            //Service service = Service.create(url,sname);
            Service service = Service.create(SERVICE_NAME);

            service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
                EndPointURL);

            //2 創建Dispatch
            Dispatch<SOAPMessage> dispatch = service.createDispatch(PORT_NAME,
                    SOAPMessage.class, Service.Mode.MESSAGE);

            //3 創建SOAPMessage
            SOAPMessage msg = formartSoapString(createMessage());

            msg.writeTo(System.out);
            System.out.println("\n invoking.....waiting response ......");

            //4 通過Dispatch傳遞消息,會返回響應消息
            SOAPMessage response = dispatch.invoke(msg);
            response.writeTo(System.out);
            System.out.println("\n----------------------------------------");

            //5 將響應的消息轉換爲dom對象
            Document doc = response.getSOAPPart().getEnvelope().getBody()
                                   .extractContentAsDocument();
            String str1 = doc.getElementsByTagName(
                    "ota:HotelDescriptiveContents").item(0).getTextContent();
            System.out.println(str1);

            String str2 = doc.getElementsByTagName("Description").item(0)
                             .getTextContent();
            System.out.println(str2);
        } catch (SOAPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test02() {
        try {
            //1 創建服務(Service)
            //URL url = new URL(wsdlUrl);
            //QName sname = new QName(ns,"MyServiceImplService");
            //Service service = Service.create(url,sname);
            Service service = Service.create(SERVICE_NAME);

            service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
                EndPointURL);

            //2 創建Dispatch
            Dispatch<SOAPMessage> dispatch = service.createDispatch(PORT_NAME,
                    SOAPMessage.class, Service.Mode.MESSAGE);

            //3 創建SOAPMessage
            SOAPMessage msg = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();

            //4 創建QName來指定消息中傳遞數據
            QName nameOta = new QName(OTA, "OTA_HotelDescriptiveInfoRQ", "ota"); //<nn:add xmlns="xx"/>
            QName nameSeekda = new QName(OTA, "OTA_HotelDescriptiveInfoRQ",
                    "ota"); //<nn:add xmlns="xx"/>
            SOAPBodyElement ele = body.addBodyElement(nameOta);
            ele = body.addBodyElement(nameSeekda);
            ele.addChildElement("a").setValue("22");
            ele.addChildElement("b").setValue("33");

            msg.writeTo(System.out);
            System.out.println("\n invoking.....");

            //5 通過Dispatch傳遞消息,會返回響應消息
            SOAPMessage response = dispatch.invoke(msg);
            response.writeTo(System.out);
            System.out.println("\n----------------------------------------");

            //將響應的消息轉換爲dom對象
            Document doc = response.getSOAPPart().getEnvelope().getBody()
                                   .extractContentAsDocument();
            String str = doc.getElementsByTagName("addResult").item(0)
                            .getTextContent();
            System.out.println(str);
        } catch (SOAPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
    * <pre>
         * 把soap字符串格式化爲SOAPMessage
         * 功能詳細描述
         * </pre>
         * @param message
         * @return
         * @see [類、類#方法、類#成員]
         */
    public static SOAPMessage formartSoapString(String message) {
        MessageFactory msgFactory;

        try {
            msgFactory = MessageFactory.newInstance();

            SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
                    new ByteArrayInputStream(message.getBytes(Charset.forName(
                                "UTF-8"))));
            reqMsg.saveChanges();

            return reqMsg;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String createMessage() {
        String soapHeader = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body>";

        String soapEnder = "</SOAP-ENV:Body></SOAP-ENV:Envelope>";

        StringBuffer msg = new StringBuffer();

        msg.append(soapHeader);

        msg.append(
            "<ota:OTA_HotelDescriptiveInfoRQ Version=\"6.003\" xmlns:ota=\"http://www.opentravel.org/OTA/2003/05\" xmlns:seekda=\"http://connect.seekda.com/2009/04\">");
        msg.append("<ota:POS>");
        msg.append("<ota:Source TerminalID=\"87.230.100.58\">");
        msg.append(
            "<ota:RequestorID URL=\"mailto:[email protected]\" ID=\"N/A\" Type=\"10\" MessagePassword=\"AA8YWWAR6BLW\"/>");
        msg.append("<ota:BookingChannel Type=\"7\">");
        msg.append("<ota:CompanyName Code=\"Integration\"/>");
        msg.append("</ota:BookingChannel>");
        msg.append("<ota:TPA_Extensions>");
        msg.append("<seekda:Lock>");
        msg.append("<seekda:LockRequest />");
        msg.append("</seekda:Lock>");
        msg.append("</ota:TPA_Extensions>");
        msg.append("</ota:Source>");
        msg.append("</ota:POS>");

        msg.append("<ota:HotelDescriptiveInfos>");
        msg.append("<ota:HotelDescriptiveInfo HotelCode=\"CHNL_TROPO\"/>");
        msg.append("</ota:HotelDescriptiveInfos>");

        msg.append("</ota:OTA_HotelDescriptiveInfoRQ>");

        msg.append(soapEnder);

        return msg.toString();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章