import java.io.FileInputStream;
import java.io.IOException;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;


public class SAAJClient
{
  public  void send()
  {

	    
	    try
	    {
	      MessageFactory msgFactory = MessageFactory.newInstance();
	      SOAPMessage msg = msgFactory.createMessage();
	      SOAPPart soapPart = msg.getSOAPPart();
	      SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

	      // for this tutorial web service, we don't have any headers,
	      // so detach the one we get for "free":
	      SOAPHeader soapHeader = soapEnvelope.getHeader();
	      soapHeader.detachNode();
	      
	      // to the body, add the document we want to send to the
	      // profile update web service:
	      SOAPBody body = msg.getSOAPBody();
	      SOAPFactory soapFactory = SOAPFactory.newInstance();
	      Name bodyName = soapFactory.createName("personalFotoUpdate", 
	                                             "swa", 
	                                             "http://example.com/swa/schema");
	      SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
	      // add the "profilePhotoRef" reference to the photo we will attach:
	      Name profilePhotoRefParamName = soapFactory.createName("profilePhotoRef");
	      SOAPElement profilePhotoRef = bodyElement.addChildElement(profilePhotoRefParamName);
	      profilePhotoRef.addTextNode("./pics/test.jpg");
	     

	      // get the image file and attach it:
	      AttachmentPart binaryAttachPart = null;
	        FileInputStream fis = new FileInputStream("./pics/test.jpg");
		      int fileSize = fis.available();  // should give us all the bytes in the file...
		      byte[] fileBytes = new byte[fileSize];
		      int bytesRead = fis.read(fileBytes);
		      if (bytesRead != fileSize)
	        {
	          System.err.println("Available bytes: " + fileSize + "; bytes read: " + bytesRead + "... exiting");
		        System.exit(-1);
	        }
	        // encode:
	        String encodedFile = Base64.encode(fileBytes);
		      DataHandler base64Handler = new DataHandler(encodedFile, "text/plain");
		      binaryAttachPart = msg.createAttachmentPart(base64Handler);
	        binaryAttachPart.setMimeHeader("Content-Transfer-Encoding", "base64");
	      binaryAttachPart.setContentId("./pics/test.jpg");
	      msg.addAttachmentPart(binaryAttachPart); 

	      if (msg.saveRequired())
	      {
	        msg.saveChanges();
	      }
	      
	      msg.writeTo(System.out);
	    }
	    catch (IOException ioe)
	    {
	      System.err.println("IOException writing SOAPMessage:  '" + ioe.getMessage() + "'");
	    }
	    catch (SOAPException se)
	    {
	      System.err.println("SOAPException: '" + se.getMessage() + "'");
	      System.err.println("  Cause:  '" + se.getCause().getClass().getName() + "': " + se.getCause().getMessage());
	    }
	  }
	  
	  public SAAJClient()
	  {
	  }
  }