Soap组装

固定格式(cwmp)的简单封装

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
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.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

/**
 * <SOAP操作接口实现类>
 * <用于实现soap的拼装和消息发送>
 * <该实现类封装了创建AP和网管之间通信的公共方法>
 */
public class OperationSoapImpl implements OperationSoap
{
    /**
     * {@inheritDoc}
     */
    @Override
    public SOAPMessage createMessage()
        throws SOAPException
    {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        return message;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public SOAPMessage createMessage(String cwmpID)
        throws SOAPException
    {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        SOAPEnvelope envelope = createEnvelope(message);
        createHeader(envelope, cwmpID);
        createBody(envelope);
        return message;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public SOAPEnvelope createEnvelope(SOAPMessage message)
        throws SOAPException
    {
        SOAPPart soapPart = message.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.addNamespaceDeclaration("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
        envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
        envelope.addNamespaceDeclaration("cwmp", "urn:dslforum-org:cwmp-1-1");
        return envelope;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public SOAPHeader createHeader(SOAPEnvelope envelope, String cwmpID)
        throws SOAPException
    {
        SOAPHeader header = envelope.getHeader();
        QName qname = new QName("urn:dslforum-org:cwmp-1-1", "ID", "cwmp");
        SOAPElement element = header.addHeaderElement(qname);
        element.addTextNode(cwmpID);
        return header;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public SOAPBody createBody(SOAPEnvelope envelope)
        throws SOAPException
    {
        SOAPBody body = envelope.getBody();
        body.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
        return body;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public SOAPMessage sendMessage(SOAPMessage message, String address)
        throws SOAPException
    {
        // 保存所需发送的消息
        message.saveChanges();
        
        // 创建链接
        SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = connectionFactory.createConnection();
        
        // 发送消息  
        SOAPMessage responseMessage = connection.call(message, address);
        return responseMessage;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public SOAPMessage sendMessage(String message, String address)
        throws SOAPException
    {
        MessageFactory msgFactory;
        try
        {
            msgFactory = MessageFactory.newInstance();
            SOAPMessage reqMsg =
                msgFactory.createMessage(new MimeHeaders(),
                    new ByteArrayInputStream(message.getBytes(Charset.forName("UTF-8"))));
            return sendMessage(reqMsg, address);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * <用于测试输出拼装的XML内容,该方法没有什么意义>
     * <消息输出到D盘soapMessage.xml文件中>
     * @param message
     * @see [类、类#方法、类#成员]
     */
    public void writeTestXml(SOAPMessage message, String file)
    {
        File respFile = new File(file);
        OutputStream respSoap;
        try
        {
            respSoap = new FileOutputStream(respFile);
            if (message != null)
            {
                
                message.writeTo(respSoap);
            }
            else
            {
                System.out.println("the message is null.");
            }
        }
        catch (Exception e)
        {
            System.out.println("Error");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}



调用 :
public void createConn(String internetID)
    {
        try
        {

          
            SOAPMessage message = operaSoap.createMessage();
            SOAPEnvelope envelope = operaSoap.createEnvelope(message);
            SOAPBody body = operaSoap.createBody(envelope);
            
            QName qname = new QName("urn:dslforum-org:cwmp-1-1", "Inform", "cwmp");
            SOAPElement element = body.addBodyElement(qname);
            createDeviceId(element, internetID);
            createEvent(element, internetID);
            createOther(element, internetID);
            createParameterList(element, internetID);
            message.saveChanges();
            SOAPMessage responseMessage = operaSoap.sendMessage(message, "http://18.250.0.137:7547/");
            operaSoap.writeTestXml(responseMessage, "D:\\soapMessageResponse.xml");     
        }
        catch (Exception e)
        {
            e.printStackTrace();
        } finally {

        }
      
    }

猜你喜欢

转载自zengyouyuan.iteye.com/blog/1520706