MTOM/XOP (MTOM: Message Transmission Optimization Mechanism、XOP: XML-binary Optimized Packaging) は、XML インフォセットを維持しつつ SOAP メッセージの送信/ワイヤー・フォーマットを最適化するための、W3C (World Wide Web Consortium) によって標準化された方法です。以下は MTOM/XOP の方法に従うことによるメリットをいくつか挙げたものです。
添付が許可される非常に単純な Web サービスでは、以下のようになります (Axis2 は添付を使用する場合の引数として byte [] をサポートしています)。
public class WSAttachmentService {
public void sendAttachments(String customerId, byte[] attachment){
try{
System.out.println(" attachment 0 "+new String(attachment));
}catch(Exception ex){
ex.printStackTrace();
}
}
}
|
上記のサンプル・コードでは、ランタイムがシステム出力文を出力すると、attachment パラメーターは初期化されます。そのため、Axis2 の MessageContext API を使って添付を取り出す必要はありません。これは MTOM/XOP を使う場合も MTOM を使わない場合も同じですが、上記でわかるように、MTOM/XOP 対応の手法を使った方が、メリットがあります。
以下のコード・リストでは、MTOM/XOP 対応のメッセージと MTOM 非対応のメッセージのワイヤー・フォーマットの違いを強調し、違いをわかりやすくしています。
MTOM/XOP 対応のワイヤー・フォーマットと MTOM 非対応のワイヤー・フォーマット
MTOM/XOP の手法では、添付は、物理的には SOAP エンベロープの外部にありますが、論理的には <xop:Include> タグによって SOAP エンベロープの中にあります。
<attachment>
<xop:Include
href="cid:1.urn:uuid:CDA722FAE92D8929D81261259412433@apache.org"
xmlns:xop="http://www.w3.org/2004/08/xop/include" />
</attachment>
--MIMEBoundaryurn_uuid_CDA722FAE92D8929D81261259411181
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <1.urn:uuid:CDA722FAE92D8929D81261259412433@apache.org>
...binary content...
|
MTOM 非対応の場合、添付は下記のようにインラインのバイナリー・コンテンツになります:
<attachment>
...binary content...
</attachment>
|
MTOM 対応の SOAP メッセージの場合、サービスは添付をトランスペアレントに取得することができます。
Axis2 ランタイムは Content-Type が「Content-Type: multipart/related;」であり、type が (Content-Type の中でのみ) 「type="application/xop+xml";」となっているかどうかを調べ、メッセージが MTOM/XOP 対応かどうかを判断します。
POST /IdMWSAttachment/services/WSAttachmentService HTTP/1.1
Content-Type: multipart/related;
boundary=MIMEBoundaryurn_uuid_CDA722FAE92D8929D81261259411181;
type="application/xop+xml";
start="<0.urn:uuid:CDA722FAE92D8929D81261259411182@apache.org>";
start-info="text/xml"
SOAPAction: "urn:anonOutInOp"
User-Agent: Axis2
Host: localhost:8181
Transfer-Encoding: chunked
1ecf
--MIMEBoundaryurn_uuid_CDA722FAE92D8929D81261259411181
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:CDA722FAE92D8929D81261259411182@apache.org>
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body><impl:sendAttachments xmlns:impl="http://attachment.ws.idm.sample.com">
<customerId>Vikas</customerId>
<attachment>
<xop:Include
href="cid:1.urn:uuid:CDA722FAE92D8929D81261259412433@apache.org"
xmlns:xop="http://www.w3.org/2004/08/xop/include" />
</attachment>
</impl:sendAttachments></soapenv:Body></soapenv:Envelope>
--MIMEBoundaryurn_uuid_CDA722FAE92D8929D81261259411181
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <1.urn:uuid:CDA722FAE92D8929D81261259412433@apache.org>
...binary...
|
ランタイムは「href」属性の値 <xop:Include> を使用します。上記の場合、その値は cid:1.urn:uuid:CDA722FAE92D8929D81261259412433@apache.org です。またランタイムはその値を、「cid」の値を使用して添付に関連付けます。添付は SOAP メッセージの「--MIMEBoundaryurn_uuid_CDA722FAE92D8929D81261259411181」の後にあります。
下記のコード・リストは Axis2 を使って MTOM 対応のメッセージを送信する方法を示しています。
opts.setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE); |
また、OMElement を使って添付要素を作成する場合の重要な点として、SOAPFactory のメソッドの呼び出しに「true」を使用する必要があります。これを示したものが下記のコードです。
createOMText(new DataHandler(fileDataSource), true);
// Start, Use with MTOM
FileDataSource fileDataSource = new FileDataSource("c:/dev2_aud.txt");
OMText data = fac.createOMText(new DataHandler(fileDataSource),
true); // parameter "true" is crucial
attachmentOM.addChild(data);
// End, Use with MTOM
|
次に、MTOM/XOP 対応の添付を使用する場合に、WS-Security による署名と暗号化を生成します。
public OperationClient createSOAPRequestMsg(){
OperationClient operationClient = null;
String serverEPR =
"http://localhost:8181/IdMWSAttachment/services/WSAttachmentService";
try{
ServiceClient client = new ServiceClient();
operationClient = client.createClient(ServiceClient.ANON_OUT_IN_OP);
MessageContext outMsgCtx = new MessageContext();
Options opts = outMsgCtx.getOptions();
opts.setTo(new EndpointReference(serverEPR));
//opts.setProperty(Constants.Configuration.ENABLE_SWA,Constants.VALUE_TRUE);
opts.setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);
SOAPEnvelope envelope = creatSOAPEnvelopeAndReturnDocument();
// Now as the WSS4J API takes W3C Document object to work with, so now we have to cre
// W3C document using the above "envelope" (which is Axis2's API).
ByteArrayOutputStream baos = new ByteArrayOutputStream();
envelope.serialize(baos);
byte[] byts = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(byts);
// Here JAXP API
DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance();
docFac.setValidating(false);
docFac.setNamespaceAware(true);
DocumentBuilder builder = docFac.newDocumentBuilder();
Document document = builder.newDocument();
// Here now we would get the W3C document Object.
document = builder.parse(bais);
document = signSOAPEnvelopeMTOM(document);
outMsgCtx.setEnvelope(soapEnvelope);
// Next we convert the above W3C document object back to SOAPEnvelope
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
OutputFormat of = new OutputFormat("XML","UTF-8",true);
of.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(baos1,of);
serializer.asDOMSerializer();
serializer.serialize(document.getDocumentElement());
StringReader sr = new StringReader(baos1.toString());
OMXMLParserWrapper omBuilder = getOMBuilderStringReader(sr);
SOAPEnvelope soapEnvelope = (SOAPEnvelope) omBuilder.getDocumentElement();
// very important step
outMsgCtx.setEnvelope(soapEnvelope);
//outMsgCtx.setEnvelope(envelope); // Non-Signed
operationClient.addMessageContext(outMsgCtx);
}catch(Exception ex){
ex.printStackTrace();
}
return operationClient;
}
|
この連載の第 1 回で、WS-Security による署名の生成方法と WS-Security によって暗号化されたメッセージの生成方法を詳細に説明しました。下記はそのコード・サンプルです。
signSOAPEnvelopeMTOM(document); |
WS-Security による署名付きで MTOM/XOP を使った SOAP メッセージの例
POST /IdMWSAttachment/services/WSAttachmentService HTTP/1.1
Content-Type: multipart/related;
boundary=MIMEBoundaryurn_uuid_AD3F29B8C627E2FE371261519053021; type="application/xop+xml";
start="<0.urn:uuid:AD3F29B8C627E2FE371261519053022@apache.org>"; start-info="text/xml"
SOAPAction: "urn:anonOutInOp"
User-Agent: Axis2
Host: localhost:8181
Transfer-Encoding: chunked
20e5
--MIMEBoundaryurn_uuid_AD3F29B8C627E2FE371261519053021
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:AD3F29B8C627E2FE371261519053022@apache.org>
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security
xmlns:wsse=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
soapenv:mustUnderstand="1">
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod
Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<ds:SignatureMethod
Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<ds:Reference URI="#id-1">
<ds:Transforms>
<ds:Transform
Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</ds:Transforms>
<ds:DigestMethod
Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<ds:DigestValue>ufKMxezZOs7P68NINmvWCRN3hYE=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
mW5oBhMCiH9RU0+VFcBWy+Ce7RjmwjubQic7eE2z9OcHpcBkpGJc9QDliVtaaStmAUBWFbHPvG56
ZcAxXoQB0tJ+QS8Du+lvyKoKQ41AfEe91chOK5K0dz1x71ttaplRB3oGveETq6RAPPeBAAjh1PK3
IAsqg/R7QI4YZyQhkRM=
</ds:SignatureValue>
<ds:KeyInfo Id="KeyId-3AE1F9051C9DBDD6C812615190528592">
<wsse:SecurityTokenReference
xmlns:wsu=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
wsu:Id="STRId-3AE1F9051C9DBDD6C812615190528593">
<wsse:KeyIdentifier
EncodingType=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0
#Base64Binary"
ValueType=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3">
MIICKDCCAZECBEspM08wDQYJKoZIhvcNAQEEBQAwWzELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklMMRIwEAY
DVQQHEwlXb29kIERhbGUxDjAMBgNVBAoTBURlVnJ5MQswCQYDVQQLEwJJVDEOMAwGA1UEAxMFVmlrYXMwHh
cNMDkxMjE2MTkyMTUxWhcNMTAwMzE2MTkyMTUxWjBbMQswCQYDVQQGEwJVUzELMAkGA1UECBMCSUwxEjAQB
gNVBAcTCVdvb2QgRGFsZTEOMAwGA1UEChMFRGVWcnkxCzAJBgNVBAsTAklUMQ4wDAYDVQQDEwVWaWthczCB
nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEArQFF9a+2DmMW2mWlSo3OA722ejV1cq+XDOc4iEaLEdPni8w
GBv1VX/WGKXmem6Xjp6UfqMF3UPKk8qOV/vThzZPJVO+HVfrTqZ2cFAxEgFwgrYFFirrKgoGlyIuiMD7Xrn
0A2vl2o2dBY4LUDQSrJ2YoBSs/K9QVj0lfGKn5gksCAwEAATANBgkqhkiG9w0BAQQFAAOBgQAhx8EN8Riea
1mg0wJCalkcMc5U60HlBQb2BDNMh+e6qUGGJey6ry6GM1RKBH5ewW5Cx7ZxLxSTUiHUvc7vDJz6PfxNQ
GuKESlwK7IrThuHamJo7nsAXpj8obsCjoGVDIR8yTTVEwFYfbvVZ54koyAJWKYA7SkCzJf8ouJNc3ZtLA==
</wsse:KeyIdentifier>
</wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>
</wsse:Security>
</soapenv:Header>
<soapenv:Body
xmlns:wsu=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
wsu:Id="id-1">
<impl:sendAttachments xmlns:impl="http://attachment.ws.idm.sample.com">
<customerId>Vikas</customerId>
<attachment>
...Q0UgIlhYRFZfSURNIiA7...
</attachment>
</impl:sendAttachments>
</soapenv:Body>
</soapenv:Envelope>
|
学ぶために
- メッセージ送信最適化メカニズムについて学んでください。
- developerWorks の SOA and web services ゾーンでスキルを磨いてください。
- さまざまな IBM 製品や IT 業界の話題に焦点を絞った developerWorks の Technical events and webcasts で最新情報を入手してください。
- 無料の developerWorks Live! ブリーフィングに出席し、IBM の製品やツール、また IT 業界のトレンドに関する最新情報を入手してください。
- developerWorks を Twitter でフォローしてください。
- developerWorks On demand demos をご覧ください。初心者のための製品インストール方法やセットアップのデモから、上級開発者のための高度な機能に至るまで、多様な話題が解説されています。
製品や技術を入手するために
- 皆さんの目的に最適な方法で IBM 製品を評価してください。製品の試用版をダウンロードする方法、オンラインで製品を試す方法、クラウド環境で製品を使う方法、あるいは SOA Sandbox で数時間を費やし、サービス指向アーキテクチャーの効率的な実装方法を学ぶ方法などがあります。
議論するために
- My developerWorks コミュニティーに参加してください。そして開発者向けのブログ、フォーラム、グループ、ウィキなどを利用しながら、他の developerWorks ユーザーとやり取りしてください。

Vikas は過去 10 年間、J2EE、WebSphere Application Server 5.1/6.0/7.0、WebSphere MQ 5.3/6.0、WebSphere ESB、Mule ESB、Oracle、ORM Tools、Spring などに幅広く従事してきました。また彼は IBM インドの、WebSphere Partner Gateway Express 6.0/ Enterprise 6.0 に関するシニア開発者でした。彼の関心領域は J2EE、Web サービスのアーキテクチャー (セキュリティー、TX、アドレス、WebSphere App Server/ESB/MQ、Mule ESB など) であり、また Java、Perl、C などの言語やスクリプトを使用して多様な事項を解決するための革新的な方法を探ることです。彼は現在、Java 拡張機能に関する SAXON XSLT パーサーの機能を検討しています。