Interstage Application Server SOAPサービス ユーザーズガイド
目次 索引 前ページ次ページ

第4章 Messaging方式のアプリケーションの実装> 4.2 受信アプリケーションの作成

4.2.3 添付ファイルを使用する場合

 添付ファイルは、SOAPメッセージに付加できる添付データです。Oneway方式、RequestResponse方式のいずれの受信アプリケーションも、添付ファイルを使用することができます。

 以下、サンプルプログラムSampleMsgSV3useAttachment.javaの例を用いて処理を説明します。

 import javax.xml.soap.*;
 import javax.xml.messaging.*;
 import java.util.*;
 import java.io.*;
 import java.awt.Image;
 import javax.xml.transform.Source;
 import javax.xml.transform.stream.StreamSource;
 import javax.activation.DataHandler;
 import javax.activation.FileDataSource;

 public class SampleMsgSV3useAttachment implements ReqRespListener //**(1)**
 {
  public SampleMsgSV3useAttachment () { }

  public SOAPMessage onMessage( SOAPMessage message )
  {
   String basePath = "C:\\temp\\";
   String basePath = "/tmp/";
   Hashtable _attachments = new Hashtable();

   try{
    //**(2)**
    Iterator _i = message.getAttachments();
    int _num = 0;
    while(_i.hasNext()){
     StringBuffer _name = new StringBuffer();
     AttachmentPart _at = (AttachmentPart)_i.next();
     Object _content = _at.getContent();
     String _contentType = _at.getContentType();
     if ( _contentType == null ) {
      return makeFault( "Server.Internal",
          "attachment has no content type." );
     }
     _contentType = _contentType.toLowerCase();
     _name.append("attachment").append(_num++);
     //**(3)**
     if( _content instanceof String ){
      String _data = (String)_content;
      _name.append(".txt");
      FileOutputStream atFile =
        new FileOutputStream(basePath+_name);
      atFile.write(_data.getBytes("UTF-8"));
      atFile.close();
      _attachments.put( new String(basePath+_name),
          _at.getContentType());
     }
     //**(4)**
     else if ( _contentType.startsWith( "text/xml" )
         || _contentType.startsWith( "application/xml" ) ) {
      StreamSource _source = ( StreamSource ) _at.getContent();
      _name.append(".xml");
      FileOutputStream atFile =
        new FileOutputStream(basePath+_name);
      InputStream _is = _source.getInputStream();
      byte[] _ba = new byte[_is.available()];
      _is.read(_ba);
      atFile.write(_ba);
      atFile.close();
      _attachments.put( new String(basePath+_name),
      _at.getContentType());
     }
     //**(5)**
     else if ( _contentType.startsWith( "image/jpeg" ) ) {
      Image _image = (Image)_content;
      _name.append(".jpeg");
      FileOutputStream atFile =
         new FileOutputStream(basePath+_name);
      DataHandler _imageHandler = _at.getDataHandler();
      _imageHandler.writeTo( atFile );
      atFile.close();
      _attachments.put( new String(basePath+_name),
         _at.getContentType());
     }
     else{
      return makeFault( "Server.Internal",
      "unexpected attachment type.");
     }
    }//while

    //**(6)**
    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage resp = mf.createMessage();
    SOAPEnvelope env = resp.getSOAPPart().getEnvelope();
    SOAPBody body = env.getBody();
    SOAPBodyElement elm = body.addBodyElement(
       env.createName( "ResponseBody", "m", "urn:Sample1" ));
    elm.addChildElement( env.createName("Response")).addTextNode("response string...");

    //**(7)**
    Enumeration _e = _attachments.keys();
    while(_e.hasMoreElements()) {
     String _path = (String)_e.nextElement();
     String _mime = (String)_attachments.get(_path);
     FileDataSource _fds = new FileDataSource(_path);
     AttachmentPart _ap = resp.createAttachmentPart(
        new DataHandler(_fds));
     _ap.setContentType(_mime);
     resp.addAttachmentPart(_ap);
    }

    resp.saveChanges();

    return resp;

   } catch( Exception e ) {
    e.printStackTrace();
    return makeFault( "Server.Internal", e.toString());
   }
  }

  public SOAPMessage makeFault(String faultCode,String faultString )
  {
   try{
    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage resp = mf.createMessage();
    SOAPEnvelope env = resp.getSOAPPart().getEnvelope();
    SOAPBody body = env.getBody();
    SOAPFault fault = body.addFault();
        fault.setFaultCode(faultCode);
    fault.setFaultString(faultString);
    resp.saveChanges();
    return resp;
   } catch( Exception e ) {
    e.printStackTrace();
    return null;
   }
  }
 }

■OnewayListenerまたはReqRespListenerの実装

 添付ファイルの処理を行わない受信アプリケーションと同様に、OnewayListenerインタフェース(Oneway方式の場合)またはReqRespListenerインタフェース(RequestResponse方式の場合)を実装します。−(1)

 Oneway方式では、受信したSOAPメッセージの添付ファイルを処理できます。RequestResponse方式では、受信したSOAPメッセージの添付ファイルを処理できるほか、添付ファイルを付加したSOAPメッセージを返信することができます。

■受信SOAPメッセージから添付ファイルの取得

 受信したSOAPメッセージを表すSOAPMessageオブジェクトから添付ファイルを取り出します。−(2)

 getAttachmentsメソッドにより取り出した添付ファイルはjava.util.Iteratorオブジェクトにまとめて表されます。添付ファイルがない場合はnullが返されます。
 Iteratorとして返される添付ファイルの要素はjavax.xml.soap.AttachmentPartオブジェクトとして保持されています。アプリケーションではAttachmentPart.getContentメソッドで実際の添付ファイルを取得します。

■MIMEタイプがtext/plainの場合の処理

 受信した添付ファイルのMIMEタイプがtext/plainである場合、AttachmentPart.getContentメソッドで取得した添付ファイルオブジェクトはjava.lang.Stringオブジェクトとして表されます。−(3)

 サンプルでは添付ファイルをテキストファイルへ出力しています。

■MIMEタイプがtext/xmlまたはapplication/xmlの場合の処理

 受信した添付ファイルのMIMEタイプがtext/xmlまたはapplication/xmlである場合、AttachmentPart.getContentメソッドで取得した添付ファイルオブジェクトはjavax.xml.transform.Source(このサンプルではjavax.xml.transform.stream.StreamSource)インスタンスとして表されます。−(4)

 サンプルでは添付ファイルをXMLファイルへ出力しています。

■DataHandlerオブジェクトとしての取得(MIMEタイプがimage/gifまたはimage/jpegの場合の処理)

 受信した添付ファイルは、MIMEタイプに関わらずAttachmentPart.getDataHandlerメソッドによって、javax.activation.DataHandlerオブジェクトとして取得することができます。javax.activation.DataHandlerは、Java Activation Framework(JAF)のAPIクラスです。APIを使用してストリームデータなどを取得することができます。APIについては付属のJavaDOCを参照してください。−(5)

 サンプルでは添付ファイルをストリームとしてファイルへ出力しています。
 受信した添付ファイルのMIMEタイプが"image/gif"または"image/jpeg"である場合は、AttachmentPart.getContentメソッドで取得すると、添付ファイルオブジェクトはjava.awt.Imageインスタンスとして表されます。

■返信SOAPメッセージの作成と返信

 送信元に返信するSOAPメッセージをSOAPMessageオブジェクトとして作成します。−(6)

■返信SOAPメッセージへの添付ファイルの設定

 送信元に返信するSOAPメッセージを表すMessageオブジェクトにSAAJ-APIを使用して添付ファイルを設定します。1つのSOAPメッセージには任意の数の添付ファイルを設定することができます。それぞれの添付ファイルはjavax.xml.soap.AttachmentPartオブジェクトとして表され、SOAPMessage.createAttachmentPartメソッドで生成することができます。

 生成されたAttachmentPartオブジェクトはSOAPMessage.addAttachmentPartメソッドによりSOAPMessageオブジェクトに設定されます。サンプルでは送信元から送られてきた添付ファイルを返信SOAPMessageオブジェクトに設定しています。−(7)


目次 索引 前ページ次ページ

Copyright 2003 FUJITSU LIMITED