uCosminexus Application Server, Web Service Development Guide

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

30.7.1 How to create Java objects for data to be sent

This subsection describes how to create a Java object depending on the data to be sent as an attachment in the MTOM/XOP format.

Organization of this subsection
(1) Sending an existing text file
(2) Sending an existing image file
(3) Sending an existing XML file
(4) Sending java.lang.String object
(5) Precautions on generating the javax.activation.DataHandler object

(1) Sending an existing text file

You can send an existing text file by using either of the two methods such as byte[] or javax.activation.DataHandler. The details on the respective procedures are as follows:

(a) Using byte[]

The procedure to send an existing text file by using byte[] is as follows:

  1. Generating the java.io.FileInputStream object
    Generate the java.io.FileInputStream object by specifying the file path of the attachment to be sent, in an argument.
    java.io.FileInputStream fileInputStream =
        new java.io.FileInputStream("sample.txt");
  2. Generating byte[]
    Write the byte data read from the java.io.FileInputStream object to the java.io.ByteArrayOutputStream object.
    Then, generate byte[] by using the toByteArray() method of the java.io.ByteArrayOutputStream class.
    java.io.ByteArrayOutputStream byteArrayOutputStream =
        new java.io.ByteArrayOutputStream();
     
    int i = 0;
    while ((i = fileInputStream.read()) != -1) {
        byteArrayOutputStream.write(i);
    }
     
    byte[] bytes = byteArrayOutputStream.toByteArray();
(b) Using javax.activation.DataHandler

The procedure to attach and send an existing file by using javax.activation.DataHandler is as follows:

  1. Generating the javax.activation.DataSource object
    Generate the javax.activation.FileDataSource object by specifying the file path of the attachment to be sent, in an argument.
    javax.activation.FileDataSource fileDataSource =
        new javax.activation.FileDataSource("sample.txt");
  2. Generating the javax.activation.DataHandler object
    Generate the javax.activation.DataHandler object by specifying the javax.activation.FileDataSource object in an argument.
    javax.activation.DataHandler dataHandler =
        new javax.activation.DataHandler(fileDataSource);

(2) Sending an existing image file

You can send an existing image file by using any of the three methods such as byte[], javax.activation.DataHandler, or java.awt.Image. The details on the respective procedures are as follows:

(a) Using byte[]

The procedure to send an existing image file by using byte[] is as follows:

  1. Generating the java.io.FileInputStream object
    Generate the java.io.FileInputStream object by specifying the file path of the attachment to be sent, in an argument.
    java.io.FileInputStream fileInputStream =
        new java.io.FileInputStream("sample.png");
  2. Generating byte[]
    Write the byte data read from the java.io.FileInputStream object to the java.io.ByteArrayOutputStream object.
    Then, generate byte[] by using the toByteArray() method of the java.io.ByteArrayOutputStream class.
    java.io.ByteArrayOutputStream byteArrayOutputStream =
        new java.io.ByteArrayOutputStream();
     
    int i = 0;
    while ((i = fileInputStream.read()) != -1) {
        byteArrayOutputStream.write(i);
    }
     
    byte[] bytes = byteArrayOutputStream.toByteArray();
(b) Using javax.activation.DataHandler

The procedure to send an existing image file by using javax.activation.DataHandler is as follows:

  1. Generating the javax.activation.DataSource object
    Generate the javax.activation.FileDataSource object by specifying the file path of the attachment to be sent, in an argument.
    javax.activation.FileDataSource fileDataSource =
        new javax.activation.FileDataSource("sample.png");
  2. Generating the javax.activation.DataHandler object
    Generate the javax.activation.DataHandler object by specifying the javax.activation.FileDataSource object in an argument.
    javax.activation.DataHandler dataHandler =
        new javax.activation.DataHandler(fileDataSource);
(c) Using java.awt.Image

The procedure to send an existing image file by using java.awt.Image is as follows:

  1. Generating the java.awt.Image object
    Generate the java.awt.Image object by specifying the file path of the attachment to be sent to an argument of the createImage(String) method in the java.awt.Toolkit class.
    java.awt.Image image =
        Toolkit.getDefaultToolkit().createImage("sample.png");

(3) Sending an existing XML file

You can send an existing XML file by using any of the three methods such as byte[], javax.activation.DataHandler, or javax.xml.transform.Source. The details on the respective procedures are as follows:

(a) Using byte[]

The procedure to send an existing XML file by using byte[] is as follows:

  1. Generating the java.io.FileInputStream object
    Generate the java.io.FileInputStream object by specifying the file path of the attachment to be sent, in an argument.
    java.io.FileInputStream fileInputStream =
        new java.io.FileInputStream("sample.xml");
  2. Generating byte[]
    Write the byte data read from the java.io.FileInputStream object to the java.io.ByteArrayOutputStream object. Then, generate byte[] by using the toByteArray() method of the java.io.ByteArrayOutputStream class.
    java.io.ByteArrayOutputStream byteArrayOutputStream = 
        new java.io.ByteArrayOutputStream();
     
    int i = 0;
    while ((i = fileInputStream.read()) != -1) {
        byteArrayOutputStream.write(i);
    }
     
    byte[] bytes = byteArrayOutputStream.toByteArray();
(b) Using javax.activation.DataHandler

The procedure to send an existing XML file by using javax.activation.DataHandler is as follows:

  1. Generating the javax.activation.DataSource object
    Generate the javax.activation.FileDataSource object by specifying the file path of the attachment to be sent, in an argument.
    javax.activation.FileDataSource fileDataSource =
        new javax.activation.FileDataSource("sample.xml");
  2. Generating the javax.activation.DataHandler object
    Generate the javax.activation.DataHandler object by specifying the javax.activation.FileDataSource object in an argument.
    javax.activation.DataHandler dataHandler =
        new javax.activation.DataHandler(fileDataSource);
(c) Using javax.xml.transform.Source

The procedure to send an existing XML file by using javax.xml.transform.Source is as follows:

  1. Generating the javax.xml.transform.Source object
    Generate the javax.xml.transform.stream.StreamSource object by specifying the file path of the attachment to be sent, in an argument.
     javax.xml.transform.stream.StreamSource streamSource =
        new javax.xml.transform.stream.StreamSource("sample.xml");

(4) Sending java.lang.String object

You can send the java.lang.String object by using either of the two methods such as byte[] or javax.activation.DataHandler. The details on the respective procedures are as follows:

(a) Using byte[]

The procedure to send the java.lang.String object by using byte[] is as follows:

  1. Generating the java.lang.String object
    java.lang.String str =
        new java.lang.String("abcde");
  2. Generating byte[]
    Generate byte[] by using the getBytes() method of the java.lang.String class.
    byte[] bytes =
        str.getBytes();
(b) Using javax.activation.DataHandler

The procedure to send the java.lang.String object by using javax.activation.DataHandler is as follows:

  1. Generating the java.lang.String object
    java.lang.String str =
        new java.lang.String("abcde");
  2. Generating the javax.activation.DataHandler object
    Generate the javax.activation.DataHandler object by specifying the java.lang.String object and the MIME type, in an argument.
    javax.activation.DataHandler dataHandler =
        new javax.activation.DataHandler(str, "text/plain; charset=UTF-8");

(5) Precautions on generating the javax.activation.DataHandler object

When sending a text file, an XML file or a java.lang.String object (String) as a javax.activation.DataHandler object through an attachment in the MTOM/XOP specification format, you can specify the character code of the characters to be included in the object by using the DataHandler (Object, String) constructor of the javax.activation.DataHandler class in the same way as the codes are specified in an attachment in the wsi:swaRef format.

For details on how to specify a character code where the DataHandler (Object, String) constructor was used, see 28.5.1(4) Precautions on generating the javax.activation.DataHandler object.