You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Kevin Colussi <Ke...@volvo.com> on 2004/07/28 20:24:49 UTC

AttachmentUtil source.........

Hello,

I have created a class called AttachmentUtil... This class helps clients 
add attachments to there SOAP requests and services to save attachments 
to local disk....

Here is the source for the AttachmentUtil code (client and service
example included) use it as you wish:

--- snip -------
/**
  * AttachmentUtil.java
  */

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.attachments.AttachmentPart;
import org.apache.axis.attachments.Attachments;
import org.apache.axis.client.Call;
import org.apache.log4j.Logger;

/**
This class helps clients add attachements to there SOAP requests
and services to save attachments to local disk....

<pre>
Here is sample client code:
--- snip -------
String[] localAbsFileNames = { "/tmp/test.jpg", "/tmp/test.pdf" };
String[] attachmentFileNames =    new AttachmentUtil().addFile(stub, 
localAbsFileNames);
// make remote call w/ attachments
System.out.println(Arrays.asList(stub.saveFile(attachmentFileNames)));
--- snip -------

Here is sample service code:

--- snip -------
AttachmentUtil util = new AttachmentUtil();
util.setAttachmentsDir("/tmp/attachments/");
// write attachemnts to /tmp/attachments/
System.out.println(Arrays.asList(util.saveFile(attachmentFileNames));
--- snip -------
</pre>

*/

public class AttachmentUtil {
   private String attachmentsDir = "";
   /*
   * Reusing the method implementation for AttachmentPart[]
   * getMessageAttachments()
   * as provided by Steve Loughran in his mail to axis-user group
   * http://www.mail-archive.com/axis-user@xml.apache.org/msg08732.html
   */
   /**
   * extract attachments from the current request
   * @return a list of attachmentparts or
   * an empty array for no attachments support in this axis
   * buid/runtime
   */
   private AttachmentPart[] getMessageAttachments() throws AxisFault {
     MessageContext msgContext = MessageContext.getCurrentContext();
     Message reqMsg = msgContext.getRequestMessage();
     Attachments messageAttachments = reqMsg.getAttachmentsImpl();
     if (null == messageAttachments) {
         System.out.println("no attachment support");
         return new AttachmentPart[0];
     }
     int attachmentCount = messageAttachments.getAttachmentCount();
     AttachmentPart attachments[] =
          new AttachmentPart[attachmentCount];
     Iterator it = messageAttachments.getAttachments().iterator();
     int count = 0;
     while (it.hasNext()) {
         AttachmentPart part = (AttachmentPart) it.next();
         attachments[count++] = part;
     }
     return attachments;
   }

   public void setAttachmentsDir(String attachmentsDir) {
     this.attachmentsDir = attachmentsDir;
   }

   public String getAttachmentsDir() {
     return this.attachmentsDir;
   }

   /**
   * This method is used for adding files as attachments for SOAP calls
   *
   * @param stub
   * @param localAbsFileNames  list of files to be attached from
     the local client file system
   * @return attachment file names
   */
   public synchronized String[] addFile(org.apache.axis.client.Stub stub,
       String[] localAbsFileNames) {
     String[] fileNames = new String[localAbsFileNames.length];
     for (int i = 0; i < localAbsFileNames.length; i++) {
         File file = new File(localAbsFileNames[i]);
         String fileName = file.getName();
         fileNames[i] = fileName;
         // Use classes from the Java Activation Framework
         // (import activation.jar) to wrap the attachment.
         DataHandler attachmentFile =
             new DataHandler(new FileDataSource(file));

         // Tell the stub that the message being formed also
         // contains an attachment, and it is of type MIME
         // encoding.
         stub._setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT,
             Call.ATTACHMENT_ENCAPSULATION_FORMAT_MIME);

         //Add the attachment to the message
         stub.addAttachment(attachmentFile);
     }
     return fileNames;
   }

   /**
   * This method receives the MIME encoded attachment from the client and
   * saves it on the server.
   * @return String[] containing the remote abs path of file names of the
   +  attachments.
   * @param String[] containing the file names of the attachments.
   */
   public synchronized String[] saveFile(String[] attachmentFileNames)
     throws java.rmi.RemoteException {
     InputStream is = null;
     FileOutputStream os = null;
     try {
         //Get all the attachments
         AttachmentPart[] attachments = getMessageAttachments();
         int totalAttachments = attachments.length;
         String[] out = new String[totalAttachments];
         Logger.getRootLogger().info("Total Attachments" +
             " Received Are: " + totalAttachments);

         //Extract each attachment.
         for (int i = 0; i < totalAttachments; i++) {
             DataHandler dh =
                 attachments[i].getDataHandler();
             //Extract the file name of the first attachment.
             String name = attachmentFileNames[i];
             Logger.getRootLogger().info(
                 "File received on server is: " + name);

             is = dh.getInputStream();
             File file = new File(attachmentsDir + name);
             os = new FileOutputStream(file);
             int j = 0;
             while (j != -1) {
                 j = is.read();
                 os.write(j);
             }
             is.close();
             os.close();
             out[i] = file.getAbsolutePath();
             Logger.getRootLogger().info(
                 "File written to: " +
                 file.getAbsolutePath());
         }
         return out;
     } catch (Exception e) {
         Logger.getRootLogger().error("Unable to save file(s): "+
             e);
         AxisFault af = new AxisFault();
         af.setFaultCodeAsString("Server.UnableToSave");
         af.setFaultReason("Unable to save file(s): " + e);
         throw af;
     }
   }
}
--- snip -------

Enjoy,
-- Kevin