package wsclient;

import java.io.File;
import java.net.URL;
import java.util.Vector;

//For attachments
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import org.apache.axis.attachments.AttachmentPart;

/*
 *Call: This class can be used to invoke the Web Service.
 *It can be prefilled by a WSDL document (on the constructor to the Service object)
 * or you can fill in the data yourself.
 */
import org.apache.axis.client.Call;

/**
*This program displays how to send files as an attachment.
*The code is part of a webservice deployed on Axis 1.1 and Tomcat 1.4.29
*@author Rommel Sharma
*/
class FileUploadClient
{
	public static void main(String args[]) throws Exception
	{
            //Specify the URL to your webservice that is needed by the Stub
			String endpoint = "http://serverIP:serverPort/axis/services/Attachment";
			URL targetURL = new URL(endpoint);

            //Use a vector in case more than one attachment is being added to store the file names.
			Vector fileNames = new Vector();

			//In our example, we add just one attachment.
			File file = new File("C:"+File.separator+"code"+File.separator+"wsclient"+File.separator+"attach.pdf");
			String name = file.getName();

			/* Add the filename to the Vector, so that when the webservice on the server
			 * receives the attachments, it can resolve the filenames (name and extension)
			 * for the attachments received.
			*/
			fileNames.add(name);

			//--- Add some comments to see what we've got ---
			System.out.println(name);
			System.out.println("File exists?: "+file.exists());
			//--- Above: some comments to see what we've got ---

            /*
             *Create a Service object using the Service and ServiceLoactor classes generated using
             *the wsdl2java tool. We also use the stub, generated from the same tool.
             *{
			 *	FYR: I used the following options:
             *	java org.apache.axis.wsdl.WSDL2Java -d Session --server-side --skeletonDeploy true MyWSDL.wsdl
			 *}
             */
			ws.FileUploadServerInterfaceService service = new ws.FileUploadServerInterfaceServiceLocator();

			//Create a stub, for the webservice at the URL specified.
			ws.AttachmentSoapBindingStub stub = new ws.AttachmentSoapBindingStub(targetURL, service);

			// 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);

		    System.out.println("Calling WS Method next...");

		    //Invoke the remote method through the stub, passing in the filenames.
 		    System.out.println("I got: "+stub.saveFile(fileNames));

	}
}
