`

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 {

        }
      
    }
分享到:
评论

相关推荐

    soap-build生成Soap接口请求报文

    在IT行业中,SOAP(Simple Object Access Protocol)是一种用于交换结构化信息的标准协议,常用于Web服务。它基于XML,提供了一种在不同应用程序之间交换数据的统一方式。本篇文章将详细探讨如何使用“soap-build”...

    SOAP toolkit 3.0

    在传统的Web服务调用中,开发者需要手动组装符合SOAP(Simple Object Access Protocol)规范的请求报文,这是一个既耗时又容易出错的过程。而SOAP Toolkit 3.0则通过提供自动化的处理机制,使得开发者可以直接调用...

    根据excel表格生成xml报文,支持soap。

    在IT行业中,XML(eXtensible Markup Language)和SOAP(Simple Object Access Protocol)是两种重要的数据交换格式,尤其在Web服务中应用广泛。本文将详细介绍如何根据Excel表格生成XML报文并支持SOAP协议,以及...

    SOAPdenovo2基因组装

    cp ~/data/test-soap2/*.fastq ~/users/qianfg/soapdenovo cp ~/users/xieshangqian/soapdenovo/SOAPdenovo.config config_file ``` 这两行命令分别用于复制测序数据文件(`.fastq`格式)和配置文件到当前工作...

    SOAPdenovo-v1.03.gz_SOAPdeno_SOAPdenovo_soapdenovo download_soap

    SOAPdenovo是一款由深圳华大基因研究院(BGI)开发的组装工具,专门针对新一代高通量平行测序产生的短片段reads进行组装。这个工具在生物信息学领域中有着广泛的应用,尤其是在基因组组装和比较基因组研究中扮演着...

    基于SOA异构服务组装系统体系结构

    2. **服务接口**:定义服务之间通信的标准协议和格式,通常采用XML、SOAP等标准。 3. **服务发现**:提供机制使服务请求者能够找到所需的服务。 4. **服务组合**:根据业务需求将多个服务组装成复杂的服务流程或应用...

    分布式异构数据库集成

    - 组装模块:负责将来自不同数据库的数据结果组装为一个完整的视图提供给用户。 综上所述,分布式异构数据库集成是一个复杂的工程,它结合了Web Services的技术优势、多数据库的独立性和联邦数据库的跨域集成能力,...

    华为MMS开发指南,mms开发必备

    综上所述,华为MMS开发涉及到SOAP协议的使用、MMS消息的组装、华为VAS SDK的集成以及应用的兼容性、安全性和性能优化等多个层面的知识,开发者需要深入学习这些内容,才能成功地开发出高效且可靠的MMS应用。

    servestr:SOAP服务日志

    build.gradle使用Gradle shadowJar插件将应用程序及其所有依赖项组装到单个“胖” jar中。 制作“胖子罐” ./gradlew shadowJar 运行胖子罐: java -jar target/servestr-3.0.0-SNAPSHOT-fat.jar (您可以将...

    Axis中文技术文档

    服务引擎处理SOAP消息,消息引擎解析和组装SOAP消息,而绑定框架则负责将Java对象与SOAP消息对应起来。 3. **创建Web服务** 使用Axis,开发者可以通过简单的注解或XML配置文件将Java类转换为Web服务。 Axis支持JAX...

    用于实现Web服务的SOA编程模型.

    Spring的IOC(Inversion of Control)和AOP(Aspect-Oriented Programming)特性使得服务的管理和组装变得更加灵活。 **6. ESB(Enterprise Service Bus)** ESB是SOA的关键组件,它作为一个中介,负责消息的路由、...

    SCA产品部署

    3. **接口与服务契约**:讨论如何定义组件间的通信协议,可能是基于SOAP、REST或者其他协议。 4. **绑定机制**:说明如何配置组件的输入和输出端口,以便它们能接收和发送消息。 5. **部署描述符**:解释如何使用XML...

    axis2 所有jar包整理

    消息处理器负责解析和组装SOAP消息。它使用XML解析器解析输入消息,并将其转换为Axis2内部数据结构,然后将结果传递给服务实现。 5. **模块(Module)** Axis2的模块系统允许添加额外的功能,如安全、缓存或事务...

    pb webservice

    2. 组装XML:根据WSDL定义,手动编写XML请求消息,确保符合SOAP规范。 3. 发送HTTP请求:利用PB的HTTP API(如`HTTPTrans`对象)发送POST请求到Web Service的URL,将XML数据作为请求体。 4. 处理响应:接收到HTTP...

    c# 彩信发送简单实例程序

    你需要创建一个方法来组装这个消息,确保所有必要字段正确填充。 步骤3:发送彩信 使用之前创建的接口调用类,调用发送方法并传入构建好的彩信内容。这里可能需要处理异步操作,因为发送彩信可能需要一段时间,并且...

    SOA-SCA-Assemblymodel

    SCA是一种规范,它定义了如何构建、组装和部署基于SOA的应用程序。SCA的主要目标是提供一种统一的编程模型,使得开发人员能够以声明性的方式编写组件,这些组件可以跨多种语言和平台无缝协作。 ### SOA与SCA的关系 ...

    图解soapui创建接口自动化用例及方法

    SOAPUI 支持SOAP(Simple Object Access Protocol)以及HTTP协议,能够方便地进行接口测试,并且可以无缝集成到多种开发环境中,如Eclipse、Maven2.X、NetBeans 和 IntelliJ IDEA。本文将详细讲解如何使用SOAPUI ...

    PHP实现WebService的简单示例和实现步骤

    WebService是Web服务的一种实现,它提供了一种基于网络的分布式系统应用之间的互操作性,通常是通过网络上的简单对象访问协议(SOAP)进行交互。 首先,文章指出在进行WebService开发时,如果使用ThinkPHP框架会...

    axis2_jars

    - **服务组件架构(SCA)**:支持服务组装,使得复杂服务可以通过组合简单服务来创建。 - **多协议支持**:除了SOAP,还支持RESTful Web服务、HTTP传输、SMTP、JMS等多种通信协议。 - **强大的消息处理**:AXIOM提供...

Global site tag (gtag.js) - Google Analytics