Thank you !!!
using LABEL_IN_VALUE is coreect !!
Then in my client file, I only have to perform the following code and the image is created on my hard disk !

               OMElement xop = (OMElement)imageEl.getFirstOMChild();
               OMAttribute attr = xop.getAttribute(new QName("href"));
               String contentID = attr.getAttributeValue();
Attachments attachment = (Attachments) msgcts.getProperty(MTOMConstants.ATTACHMENTS);
               contentID = contentID.trim();
               if (contentID.substring(0, 3).equalsIgnoreCase("cid")) {
                   contentID = contentID.substring(4);
               }
DataHandler dataHandler = attachment.getDataHandler(contentID);

Image image = new ImageIO().loadImage(dataHandler.getDataSource().getInputStream()); FileOutputStream imageOutStream = new FileOutputStream("/home/souillac/thinkAttachment.jpg"); new ImageIO().saveImage("image/jpeg", image, imageOutStream);

Thanks again,
Regards,
Charles

Thilina Gunarathne wrote:
Hi,
I can suggested three ways to access the binary content according to my choice priority. 1. I don't have a much of a knowledge in Data Bounded code. But if the data binding is properly handled for MTOM then there should be a getter for a Data Handler or a Byte[] in your helloworld.InitiateHelloWorldDocument .. 2. Regarding your server code, You can access the OMText object containing the Binary data from imageEle element by calling it's getFirstChild() method. Then you can call the getDataHandler() method of that OMText instance to get the DataHandler. This should work. 3.I don't understand why you are trying to access the attachments Map in the msgContext. This method is to use with SwA type attachments, though it should work even in this scenario(with loads of unnecessary overhead) . I noticed that you are setting the out message context in the setOperationContext method using WSDLConstants.MESSAGE_LABEL_OUT_VALUE. Try using LABEL_IN_VALUE. I personally don't recommend the 3rd method to access MTOM attachments. It's lot easier than that. :) HTH,
~Thilina

    >             for (Iterator it =
    payload.getChildElements();it.hasNext();) {
    >                 OMElement el = (OMElement) it.next();
    >                 if (el.getLocalName().equalsIgnoreCase("image")) {
    >                     imageEl = el;
    >                 } else if
    (el.getLocalName().equalsIgnoreCase("name")) {
    >                     nameEl = el;
    >                 }
    >             }
    >
    >             String out = "<initiateHelloWorldResponse
    xmlns=\"urn:helloworld:bpel:bsoap\">";
    >             out += "<result>";
    >
    >             try {
    >                 OMElement xop =
    (OMElement)imageEl.getFirstOMChild();
    >
    >                 OMAttribute attr = xop.getAttribute (new
    QName("href"));
    >                 out += "\nattr = "+attr;
    >                 String contentID = attr.getAttributeValue();
    >                 out += "\ncontentID = "+contentID;
    >                 Attachments attachment = (Attachments)
    msgcts.getProperty(MTOMConstants.ATTACHMENTS);
    >                 out += "\nattachment = "+attachment;
    >                 contentID = contentID.trim ();
    >
    >                 if (contentID.substring(0,
    3).equalsIgnoreCase("cid")) {
    >                     contentID = contentID.substring(4);
    >                 }
    >                 DataHandler dataHandler =
    attachment.getDataHandler(contentID);
    >                 out += "\n dataHandler = "+dataHandler;
    >                 OMText textNode = new OMTextImpl(dataHandler,
    xop.getOMFactory());
    >                 imageEl.build();
    >                 xop.detach();
    >                 imageEl.addChild(textNode);
    >
    >                 OMText binaryNode = (OMText) xop.getFirstOMChild();
    >
    >                 DataHandler dh =
    (DataHandler)binaryNode.getDataHandler();
    >                 Image image = new
    ImageIO().loadImage(dh.getDataSource().getInputStream());
    >                 FileOutputStream imageOutStream = new
    FileOutputStream("/home/souillac/thinkAttachment.jpg");
    >                 new ImageIO().saveImage("image/jpeg", image,
    imageOutStream);
    >
    >             } catch (Exception e) {
    >                 out += "\ncatch : "+getExceptionAsString(e);
    >             }
    >
    >             out += "</result>";
    >             out += "</initiateHelloWorldResponse>";
    >             retDoc =
    InitiateHelloWorldResponseDocument.Factory.parse (out);
    >         } catch (Exception e) {
    >             e.printStackTrace();
    >         }
    >         return retDoc;
    >     }
    >
    >     private String getExceptionAsString(Throwable ex) {
    >         String excep = "\n"+ex.getClass()+" :
    "+ex.getMessage()+"\n";
    >         while (ex != null) {
    >             StackTraceElement[] steTab = ex.getStackTrace();
    >             for (int j=0;j< steTab.length;j++) {
    >                 StackTraceElement ste = steTab[j];
    >                 excep += "at " + ste.toString() + "\n";
    >             }
    >             ex = ex.getCause ();
    >             if (ex != null) {
    >                 excep += "caused by \n";
    >             }
    >         }
    >         return excep;
    >     }
    > }
    >
    >
    ------------------------------------------------------------------------

    >
    > package helloworld;
    >
    > import java.awt.Image;
    > import java.io.FileInputStream;
    > import java.io.StringWriter;
    >
    > import javax.activation.DataHandler;
    > import javax.xml.stream.XMLOutputFactory;
    >
    > import org.apache.axiom.attachments.utils.ImageDataSource;
    > import org.apache.axiom.attachments.utils.ImageIO;
    > 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.OMText;
    > import org.apache.axiom.soap.SOAP11Constants ;
    > import org.apache.axis2.Constants;
    > import org.apache.axis2.addressing.EndpointReference;
    > import org.apache.axis2.client.Options;
    > import org.apache.axis2.client.ServiceClient;
    >
    > public class HelloWorldClient {
    >
    >     private static EndpointReference targetEPR = new
    EndpointReference("http://localhost:8080/axis2/services/HelloWorldServiceBP
    ");
    >
    >     public static void main(String[] args) {
    >         System.out.println("In Axis2 HelloWorld Client...");
    >         if (args.length != 1) {
    >             System.out.println ("You must specify one and only
    one argument");
    >         } else {
    >             try {
    >                 callAXIOMWithAttachment(args[0]);
    >             } catch (Exception e) {
    >                 e.printStackTrace();
    >             }
    >         }
    >     }
    >
    >     private static void callAXIOMWithAttachment(String name)
    throws Exception {
    >         OMFactory fac = OMAbstractFactory.getOMFactory ();
    >         OMNamespace omNs =
    fac.createOMNamespace("urn:helloworld:bpel:bsoap", "tns");
    >         OMElement payload =
    fac.createOMElement("initiateHelloWorld", omNs);
    >
    >         //name
    >         OMElement nameEl = fac.createOMElement("name", omNs);
    >         nameEl.addChild(fac.createOMText(nameEl, name));
    >
    >         //image
    >         OMElement image = fac.createOMElement("image", omNs);
    >         FileInputStream fis = new
    FileInputStream("/home/souillac/divers/images/think.jpg");
    >         Image expectedImage = new ImageIO().loadImage(fis);
    >         ImageDataSource dataSource = new
    ImageDataSource("think.jpg",expectedImage);
    >         DataHandler expectedDH = new DataHandler(dataSource);
    >         OMText textData = fac.createOMText (expectedDH, true);
    >
    >         image.addChild(textData);
    >
    >
    >         payload.addChild(image);
    >         payload.addChild(nameEl);
    >
    >         Options options = new Options();
    >         options.setTo(targetEPR);
    >         options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
    >         options.setProperty(Constants.Configuration.ENABLE_MTOM,
    Constants.VALUE_TRUE);
> options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
    >         options.setAction("initiate");
    >
    >         ServiceClient sender = new ServiceClient();
    >         sender.setOptions (options);
    >
    >         OMElement result = sender.sendReceive(payload);
    >
    >         StringWriter writer = new StringWriter();
> result.serialize(XMLOutputFactory.newInstance().createXMLStreamWriter(writer));

    >         writer.flush();
    >
    >         System.out.println(writer.toString());
    >
    >     }
    >     /*
    >     private static void callAXIOMWithAttachment(String name)
    throws Exception {
    >         OMFactory fac = OMAbstractFactory.getOMFactory();
    >         OMNamespace omNs =
    fac.createOMNamespace("urn:helloworld:bpel:bsoap", "tns");
    >         OMElement payload = fac.createOMElement
    ("initiateHelloWorld", omNs);
    >
    >         //name
    >         OMElement nameEl = fac.createOMElement("name", omNs);
    >         nameEl.addChild(fac.createOMText(nameEl, name));
    >
    >         //image
    >         OMElement image = fac.createOMElement("image", omNs);
    >         FileInputStream fis = new
    FileInputStream("/home/souillac/divers/images/think.jpg");
    >         Image expectedImage = new ImageIO().loadImage(fis);
    >         ImageDataSource dataSource = new
    ImageDataSource("think.jpg",expectedImage);
    >         DataHandler expectedDH = new DataHandler(dataSource);
    >         OMText textData = fac.createOMText (expectedDH, true);
    >
    >         image.addChild(textData);
    >
    >
    >         payload.addChild(image);
    >         payload.addChild(nameEl);
    >
    >         Options options = new Options();
    >         options.setTo(targetEPR);
    >         options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
    >         options.setProperty(Constants.Configuration.ENABLE_MTOM,
    Constants.VALUE_TRUE);
> options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
    >         options.setAction("initiate");
    >
    >         ServiceClient sender = new ServiceClient();
    >         sender.setOptions (options);
    >
    >         OMElement result = sender.sendReceive(payload);
    >
    >         StringWriter writer = new StringWriter();
> result.serialize(XMLOutputFactory.newInstance().createXMLStreamWriter(writer));

    >         writer.flush();
    >
    >         System.out.println(writer.toString());
    >
    >     }
    > */
    > }
    >
    ------------------------------------------------------------------------
    >
    > <definitions name="helloworld"
    >       targetNamespace="urn:helloworld:bpel:bsoap"
    >       xmlns:tns="urn:helloworld:bpel:bsoap"
    >       xmlns=" http://schemas.xmlsoap.org/wsdl/";
    >       xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/";
    >       xmlns:bsoa="urn:bsoa:bull:com"
    >       xmlns:jbi="http://servicemix.org/wsdl/jbi/";
    >       xmlns:xsd="http://www.w3.org/2001/XMLSchema "
    >       xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/";
    >       xmlns:xmime="http://www.w3.org/2004/06/xmlmime
    <http://www.w3.org/2004/06/xmlmime>">
    >
    >       <types>
    >               <schema xmlns="http://www.w3.org/2001/XMLSchema";
    >                       targetNamespace="urn:helloworld:bpel:bsoap">
    >                       <complexType name="initiateHelloWorldT">
    >                               <sequence>
    >                                       <element name="name"
    type="xsd:string" />
    >                                       <element name="image"
    type="xsd:base64Binary" />
    >                               </sequence>
    >                       </complexType>
    >                       <complexType
    name="initiateHelloWorldResponseT">
    >                               <sequence>
    >                                       <element name="result"
    type="xsd:string" />
    >                               </sequence>
    >                       </complexType>
    >                       <element name="initiateHelloWorld"
    >                               type="tns:initiateHelloWorldT" />
    >                       <element name="initiateHelloWorldResponse"
> type="tns:initiateHelloWorldResponseT" />
    >
    >               </schema>
    >       </types>
    >
    >       <message name="initiateHelloWorldSoapRequest">
    >               <part name="parameters"
    element="tns:initiateHelloWorld" />
    >               <!--<part name="attach" type="xsd:base64Binary"/>-->
    >       </message>
    >
    >       <message name="initiateHelloWorldSoapResponse">
    >               <part name="parameters"
    >                       element="tns:initiateHelloWorldResponse" />
    >       </message>
    >
    >       <!-- portType implemented by the HelloWorld BPEL process -->
    >       <portType name="HelloWorld">
    >               <operation name="initiate">
    >                       <input
    message="tns:initiateHelloWorldSoapRequest" />
    >                       <output
    message="tns:initiateHelloWorldSoapResponse" />
    >               </operation>
    >       </portType>
    >
    >       <binding name="HelloWorldPTSOAPBinding"
    type="tns:HelloWorld">
    >               <soap:binding style="document"
> transport="http://schemas.xmlsoap.org/soap/http"; />
    >               <operation name="initiate">
    >                       <input>
    >                               <soap:body use="literal"
> namespace="urn:helloworld:bpel:bsoap" />
    >
    >                       </input>
    >                       <output>
    >                               <soap:body use="literal"
> namespace="urn:helloworld:bpel:bsoap" />
    >                       </output>
    >               </operation>
    >       </binding>
    >
    >       <service name="HelloWorldServiceBP">
    >               <port name="HelloWorld"
    binding="tns:HelloWorldPTSOAPBinding">
    >                       <soap:address
> location="http://localhost:8080/axis2/services/HelloWorldServiceBP?wsdl
    <http://localhost:8080/axis2/services/HelloWorldServiceBP?wsdl>" />
    >               </port>
    >       </service>
    >
    > </definitions>
    >




--
"May the SourcE be with u" http://webservices.apache.org/~thilina/ <http://webservices.apache.org/%7Ethilina/> http://thilinag.blogspot.com/ http://www.bloglines.com/blog/Thilina

Reply via email to