第 8 章 使用 SOAP 使用附件发送二进制数据

摘要

SOAP 附加功能提供了作为 SOAP 消息的一部分发送二进制数据的机制。将 SOAP 与附件一起使用需要将您的 SOAP 消息定义为 MIME 多部件消息。

概述

SOAP 消息通常不包含二进制数据。但是,W3C SOAP 1.1 规范允许使用 MIME 多部件/相关消息在 SOAP 消息中发送二进制数据。使用带有附件的 SOAP 调用这一技术。SOAP 附加在 W3C 的 SOAP 消息及附件 》中定义。

命名空间

用于定义 MIME 多部件/相关消息的 WSDL 扩展在命名空间 http://schemas.xmlsoap.org/wsdl/mime/ 中定义。

在下面的讨论中,假设此命名空间带有 mime 的前缀。设置此选项的 WSDL 定义 元素中的条目显示在 例 8.1 “合同中的 MIME 命名空间规格” 中。

例 8.1. 合同中的 MIME 命名空间规格

xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"

更改消息绑定

在默认的 SOAP 绑定中,输入、输出和错误 元素的第一个子元素是描述 SOAP 消息正文的 soap:body 元素,表示数据。将 SOAP 与附件一起使用时,soap:body 元素将被 mime:multipartRelated 元素替代。

注意

WSDL 不支持 对故障 消息使用 mime:multipartRelated

mime:multipartRelated 元素告知 Apache CXF,消息正文是可能包含二进制数据的多部分消息。元素的内容定义消息及其内容的部分。MIME:multipartRelated 元素包含一个或多个 mime:part 元素,用于描述消息的各部分。

第一个 mime:part 元素必须包含 soap:body 元素,它们通常出现在默认的 SOAP 绑定中。剩余的 mime:part 元素定义消息中发送的附件。

描述 MIME 多部件消息

使用 mime:multipartated 元素(包含 mime:part 元素)介绍了 MIME 多部件消息。要完全描述 MIME 多部件的信息,您必须执行以下操作:

  1. 在您将作为 MIME 多部件消息的输入或输出消息内,添加 mime:mulipartRelated 元素作为保护消息的第一个子元素。
  2. mime:part 子元素添加到 mime:multipartRelated 元素,并将其 name 属性设置为唯一字符串。
  3. 添加 soap:body 元素作为 mime:part 元素的子项,并相应地设置其属性。

    注意

    如果合同有默认的 SOAP 绑定,您可以将来自默认绑定中对应的消息中的 soap:body 元素复制到 MIME 多部分消息。

  4. 将另一个 mime:part 子元素添加到 mime:multipartReleated 元素,并将其 name 属性设置为唯一字符串。
  5. mime:content 子元素添加到 mime:part 元素,以描述消息此部分的内容。

    要完全描述 MIME 消息部分的内容: mime:content 元素具有以下属性:

    表 8.1. MIME:内容 属性

    属性描述 +

    part

    指定来自父消息定义中的 WSDL 消息 部分 的名称,该消息作为放置在线路上的 MIME 多部分消息的内容。

    +

    type

    此消息中数据的 MIME 类型。MIME 类型定义为类型,使用语法类型/类型来定义。

    +

    有多个预定义的 MIME 类型,如 image/jpegtext/plain。MIME 类型由互联网编号分配机构(IANA)维护,在 多用途 Internet 邮件扩展(MIME)形式中详细介绍:互联网消息正文和 多用途 Internet 邮件扩展(MIME)第 2 部分:媒体类型

    +

  6. 对于每个额外的 MIME 部分,重复步骤 [i303819][i303821]

示例

例 8.2 “使用带有附件的 SOAP 的合同” 显示 WSDL 片段定义以 JPEG 格式存储 Xrays 的服务。镜像数据 xRay 作为 xsd:base64binary 存储,并打包成 MIME 多部件消息的第二个部分 imageData。作为 SOAP 正文的一部分,输入消息的其余两个部分( patientNamepatientNumber 是 MIME 多部件)。

例 8.2. 使用带有附件的 SOAP 的合同

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="XrayStorage"
    targetNamespace="http://mediStor.org/x-rays"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://mediStor.org/x-rays"
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <message name="storRequest">
    <part name="patientName" type="xsd:string"/>
    <part name="patientNumber" type="xsd:int"/>
    <part name="xRay" type="xsd:base64Binary"/>
  </message>
  <message name="storResponse">
    <part name="success" type="xsd:boolean"/>
  </message>

  <portType name="xRayStorage">
    <operation name="store">
      <input message="tns:storRequest" name="storRequest"/>
      <output message="tns:storResponse" name="storResponse"/>
    </operation>
  </portType>

  <binding name="xRayStorageBinding" type="tns:xRayStorage">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="store">
      <soap:operation soapAction="" style="document"/>
      <input name="storRequest">
        <mime:multipartRelated>
          <mime:part name="bodyPart">
            <soap:body use="literal"/>
          </mime:part>
          <mime:part name="imageData">
            <mime:content part="xRay" type="image/jpeg"/>
          </mime:part>
        </mime:multipartRelated>
      </input>
      <output name="storResponse">
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>

  <service name="xRayStorageService">
    <port binding="tns:xRayStorageBinding" name="xRayStoragePort">
      <soap:address location="http://localhost:9000"/>
    </port>
  </service>
</definitions>