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


how can create mime-atachment text/xml soapmessage?

i have function, sends binary file of xml. don't know how can it.

  1. use datahandler/datasource push binary data message on client side.

  2. on server side, need create datacontenthandler implementation , register activation framework.

step 1 - adding binary attachment

implement simple datasource getting 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 file");         }     } 

now use code add attachment:

        inputstream data = ...         soapmessage msg = ...         datahandler dh = new datahandler(new binarydatasource(data));         attachmentpart attachment = msg.createattachmentpart(dh);         msg.addattachmentpart(attachment); 

step 2 - setup server side

[note: worked me]

create datacontenthandler handles incoming attachment of type "application/binary".

import javax.activation.*; import java.io.*;  public class binarydatahandler implements datacontenthandler {      /** creates new instance of binarydatahandler */     public binarydatahandler() {     }      /** key, returns 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 {         // need implement have         // conversion done automatically based on         // mime type on client side.     } }     

now, can use code data of attachment:

     soapmessage msg = ... //received message      iterator ats = msg.getattachments();      if( ats.hasnext() ){           attachmentpart attachment = (attachmentpart)ats.next();           inputstream contents = (inputstream)attachment.getcontent();      } 

finally, need register datacontenthandler activation framework use it. there couple of ways (see mailcapcommandmap in activation framework javadocs). did create file called "mailcap" in lib directory used "java" interpreter.

this file looks this:

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

this tells activation framework use handler indicated mime type.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -