uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

28.5.1 Method of generating the attachment instance (wsi:swaRef format)

The javax.activation.DataHandler object is generated as an attachment object. The following table lists the constructors of the javax.activation.DataHandler class:

Table 28-10 Constructors of the javax.activation.DataHandler class

No. Use case Constructor Description of the argument
1 Attaching a file DataHandler(javax.activation.DataSource ds) [First argument]
This is the javax.activation.DataSource object.
The object of the javax.activation.FileDataSource class can be specified.
2 Attaching an object that is on memory DataHandler(java.lang.Object obj, java.lang.String mimeType) [First argument]
This is the Java object.
[Second argument]
This is the MIME type of the object.
For details about the MIME type that can be specified, see 28.4.2(3) Mapping between the attachment extension and MIME types.

Depending on the objects sent as attachments, the method of generating the javax.activation.DataHandler object is different. The following points describe the generation method for each object to be sent:

Organization of this subsection
(1) Method of sending an existing file as an attachment
(2) Method of sending a Java object as an attachment
(3) Method of sending the java.lang.String object as an attachment
(4) Precautions on creating the javax.activation.DataHandler object

(1) Method of sending an existing file as an attachment

To attach and send an existing file:

  1. Generate the javax.activation.FileDataSource object.
    In the argument, specify the file path of attachment to be sent and generate the javax.activation.FileDataSource object.
    javax.activation.FileDataSource fdSource =
     new javax.activation.FileDataSource("/tmp/sample.jpg");
  2. Generate the javax.activation.DataHandler object.
    Specify the javax.activation.FileDataSource object in the argument and generate javax.activation.DataHandler object.
    javax.activation.DataHandler dhandler =
     new javax.activation.DataHandler(fdSource);

(2) Method of sending a Java object as an attachment

To send a Java object as an attachment:

  1. Generate the Java object.
    Here, you generate the java.awt.Image object to be sent as an attachment:
    java.awt.Image attachments =
     Toolkit.getDefaultToolkit().createImage("sample.jpg");
  2. Generate the javax.activation.DataHandler object.
    Specify the Java object and MIME type corresponding to the Java object in the argument and generate javax.activation.DataHandler object.
    javax.activation.DataHandler dhandler = new
     javax.activation.DataHandler(attachments, "image/jpeg");

(3) Method of sending the java.lang.String object as an attachment

To send the java.lang.String object as an attachment:

  1. Generate the java.lang.String object.
    Generate the java.lang.String object of the string to be sent as an attachment.
    java.lang.String attachments = new java.lang.String("abcde");
  2. Generate the javax.activation.DataHandler object.
    Specify the java.lang.String object and MIME type in the argument and generate the javax.activation.DataHandler object. The java.lang.String object is encoded using the character code specified in charset parameter.
    javax.activation.DataHandler dhandler = new javax.activation.DataHandler(attachments, "text/plain; charset=UTF-8");

(4) Precautions on creating the javax.activation.DataHandler object

If the object that you want to send as an attachment in the wsi:swaRef format is a text file, an XML file, or a java.lang.String object (string), you can specify the character code of the string included in the object by using the DataHandler (Object, String) constructor of the javax.activation.DataHandler class.

If you do not specify the character code when creating the javax.activation.DataHandler object, the object to be sent is encoded by the default character code (US-ASCII). If the object contains characters other than the default character code (US-ASCII), the attachment that is sent is treated as invalid. Therefore, specify an appropriate character code especially when you want to include Japanese characters.

If the object to be sent is an XML file, the character code specified in the XML declaration is not used. The XML file is encoded using the default character code (US-ASCII), or the character code specified when creating the javax.activation.DataHandler object, and then sent.

To specify a character code, assign the charset parameter to the MIME type of the object to be specified in the second argument of the DataHandler(Object, String) constructor. The following is the coding format of the second argument of the DataHandler(Object, String) constructor.

Mime type of the object to be sent+";"+"charset"+"="+character code#

#
The character code is not case sensitive. Also, you cannot specify the character code that is not supported by the JDK and a blank character.