/**
 * AttachmentSoapBindingImpl.java
 * This file was auto-generated from WSDL
 * by the Apache Axis WSDL2Java emitter.
 */

package ws;

import java.io.*;
import org.apache.axis.AxisFault;

//For Iterator that reads MimeHeaders
import java.util.*;


//For attachments
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import javax.activation.DataSource;
import javax.activation.DataHandler;
import org.apache.axis.attachments.Attachments;
import org.apache.axis.attachments.AttachmentPart;


/**
 *This program displays how to receive the files sent by a client as an attachment.
 *@author: Rommel Sharma.
 * In this program, the getMessageAttachments() implementation as provided
 * by Steve Loughran in his mail to axis-user group
 * http://www.mail-archive.com/axis-user@xml.apache.org/msg08732.html
 */
public class AttachmentSoapBindingImpl implements ws.FileUploadServerInterface
{
	//The status message for attachment download that we will send back to the client.
	String status = "The atachment has been received and saved.";

	/**
	* This method receives the MIME encoded attachment from the client and saves it on the server.
	* @return status A string informing the client if the attachment was received correctly.
	* @param obj An object to receive the Vector containing the file names of the attachments.
	*/
    public java.lang.String saveFile(java.lang.Object obj) throws java.rmi.RemoteException
    {
		InputStream is =null;
		FileOutputStream os = null;

		try
		{
			//File names vector
			Vector fileInfo = (Vector)obj;

			//Get all the attachments
			AttachmentPart[] attachments = getMessageAttachments();
			/*
			 * 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
			*/

			//Put the logic in a loop for totalAttachments for multiple attachments.
			int totalAttachments = attachments.length;
			System.out.println("Total Attachments Received Are: "+totalAttachments);

			//Extract the first attachment. (Since in this case we have only one attachment sent)
			DataHandler dh = attachments[0].getDataHandler();

			//Extract the file name of the first attachment.
			String name = (String)fileInfo.get(0);
			System.out.println("File received on server is: "+name);

			is =dh.getInputStream();
			File file = new File(name);
			os = new FileOutputStream(file);
			int i=0;
			while(i!=-1)
			{
				i=is.read();
				os.write(i);
			}
			is.close();
			os.close();
		}
		catch(Exception e)
		{
			status="File Could Not Be Saved: "+e.getMessage();
			System.out.println("In Impl: "+e);
		}

		return status;
    } //saveFile

/*
 * 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;
	}
}
