import java.io.FileInputStream;
import java.io.StringWriter;

import javax.activation.DataHandler;
import javax.xml.stream.XMLStreamException;

import org.apache.axiom.attachments.Attachments;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axiom.om.impl.MIMEOutputUtils;
import org.apache.axiom.om.util.Base64;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPMessage;
import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;



public class AxiomClient {
	
	  public  void send()
	  	  {
		  SOAPFactory factory=new SOAP11Factory();
		  
		  SOAPMessage sp=factory.createSOAPMessage();
		  SOAPEnvelope envelope=factory.createSOAPEnvelope();
		  sp.addChild(envelope);
		  SOAPBody body=factory.createSOAPBody();
		  sp.getSOAPEnvelope().addChild(body);
	       OMFactory fac = OMAbstractFactory.getOMFactory();
	        OMNamespace omNs = fac.createOMNamespace(
	                      "http://example.com/swa/schema", "swa");
	        OMElement bodyelem = fac.createOMElement("personalFotoUpdate", omNs);
	        OMElement profileref = fac.createOMElement("profilePhotoRef", omNs);
	        profileref.setText("./pics/test.jpg");
	        bodyelem.addChild(profileref);
	        body.addChild(bodyelem);

	        byte[] fileBytes=null;
		      try {
				FileInputStream fis = new FileInputStream("./pics/test.jpg");
				int fileSize = fis.available(); // should give us all the bytes in the file...
				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:
			} catch (Exception e) {
				// TODO: handle exception
			}
			DataHandler dataHandler = 
				new DataHandler(Base64.encode(fileBytes), 
						"text/plain");

			OMElement fotolink;
			fotolink = fac.createOMElement("fotolink", omNs);
			fotolink.addAttribute("href", "cid:pic", omNs);
			body.addChild(fotolink);
			Attachments attachments = new Attachments();
			attachments.addDataHandler("pic", dataHandler);
	        StringWriter writer = new StringWriter();
	        OMOutputFormat format=new OMOutputFormat ();
	        format.setCharSetEncoding("utf-8");
	        try {
				envelope.serializeAndConsume(writer, format);
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			MIMEOutputUtils.writeSOAPWithAttachmentsMessage(
	        		writer, System.out, attachments, format);

		  
		  
	  }

}
