How to create MIME-atachment text/xml in Java?

707 views Asked by At

How can I create mime-atachment text/xml for my SOAPMessage?

I have a function, which sends binary file of XML. But I don't know how can I do it.

1

There are 1 answers

1
Ranjeet On
  1. Use a DataHandler/DataSource to push the binary data into the message on the client side.

  2. On the server side, you need to create a DataContentHandler implementation and register it with the activation framework.

Step 1 - Adding the binary attachment

Implement a simple DataSource for getting the data:

import javax.activation.*;

    class BinaryDataSource implements DataSource {
        InputStream _is;

        public BinaryDataSource(InputStream is) {
            _is = is;
        }
        public String getContentType() { return "application/binary"; }
        public InputStream getInputStream() throws IOException { return _is; }
        public String getName() { return "some file"; }
        public OutputStream getOutputStream() throws IOException {
            throw new IOException("Cannot write to this file");
        }
    }

Now use this code to add the attachment:

        InputStream data = ...
        SOAPMessage msg = ...
        DataHandler dh = new DataHandler(new BinaryDataSource(data));
        AttachmentPart attachment = msg.createAttachmentPart(dh);
        msg.addAttachmentPart(attachment);

Step 2 - Setup the server side

[Note: this worked for me]

Create a DataContentHandler which handles the incoming attachment of type "application/binary".

import javax.activation.*;
import java.io.*;

public class BinaryDataHandler implements DataContentHandler {

    /** Creates a new instance of BinaryDataHandler */
    public BinaryDataHandler() {
    }

    /** This is the key, it just returns the data uninterpreted. */
    public Object getContent(javax.activation.DataSource dataSource) throws java.io.IOException {
        System.out.println("BinaryDataHandler: getContent called with: " + dataSource);
        return dataSource.getInputStream();
    }

    public Object getTransferData(java.awt.datatransfer.DataFlavor dataFlavor, 
                         javax.activation.DataSource dataSource) 
                          throws java.awt.datatransfer.UnsupportedFlavorException, 
   java.io.IOException {
        return null;
    }

    public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors() {
        return new java.awt.datatransfer.DataFlavor[0];
    }

    public void writeTo(Object obj, String str, java.io.OutputStream outputStream) 
     throws java.io.IOException {
        // You would need to implement this to have
        // the conversion done automatically based on
        // mime type on the client side.
    }
}    

Now, you can use this code to get the data of the attachment:

     SOAPMessage msg = ... //received message
     Iterator ats = msg.getAttachments();
     if( ats.hasNext() ){
          AttachmentPart attachment = (AttachmentPart)ats.next();
          InputStream contents = (InputStream)attachment.getContent();
     }

Finally, you need to register your DataContentHandler so that the activation framework will use it. There are a couple of ways (see MailcapCommandMap in the activation framework javadocs). What I did was to create a file called "mailcap" in the lib directory used by my "java" interpreter.

This file looks like this:

application/binary; BinaryDataHandler application/binary;; x-java-content-handler=BinaryDataHandler

This tells the activation framework to use your handler for the indicated MIME type.