Beruflich Dokumente
Kultur Dokumente
org)
Home > Stories > Downloading a Binary File from a Web Service using Axis2 and SOAP with Attachments
Downloading a Binary File from a Web Service using Axis2 and SOAP with Attachments
By thilina gunarathne Created 2007-06-05 04:02
Introduction
The sample application we are going to develop through this tutorial includes writing an Apache Axis2/Java [1] [1] Web service, which will respond to the requests by a SOAP with Attachments (SwA) [2] [1] type message containing a binary file. The sample application also includes writing an Apache Axis2 Web service client, which can receive the above SOAP with Attachments type response. The sample use case which we are going to implement is a Download Statistics Service. This service provides data relating to monthly download statistics of a set of software products. These statistics includes a graph image of the distribution of downloads over a period of one month together with some other data. [2] Other similar use cases of this kind of a service includes providing graph images of stock market data through a Web service, providing hospital patients' profiles together with images from a central national server, etc.
Background
SOAP with Attachments or SwA is a Note submitted to the W3C by Microsoft and HP Labs. The objective of the SOAP with Attachments note is to describe a standard way to associate one or more binary attachments with a SOAP message in their original formats. SOAP with Attachments utilizes the Multipart/Related MIME [3] [1] packaging to package the SOAP message together with the binary attachments, and utilizes URI based mechanisms for referencing the MIME parts. More information about Apache Axis2/Java SOAP with Attachments support can be found in the Using SOAP with Attachments in Apache Axis2[4] [1] article.
adding it to the attachment map of the outgoing org.apache.axis2.context.MessageContext. Open StatisticsService.java implementation class. to have a look at the complete source listing of the Web service
[3]
OMElement graphElement = factory.createOMElement("graph", omNs, wrapperElement); String graphCID = "cid:"+graphImageID; graphElement.addAttribute("href",graphCID, NOTE: Make sure to copy the "StatisticsServiceResources" directory provided with this sample[5] in to your local machine. Then set the value of the "sampleResourcePath" variable of the service implementation class point to the full path of the "StatisticsServiceResources" directory in your local machine file system.
<operation name="getStats"> <actionMapping>urn:getStats</actionMapping> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> </operation> We need to add the "enableSwA" parameter to the services.xml with a value of TRUE for the service to be able to send SOAP with Attachment type messages. Alternatively, you can also set this parameter in the axis2 server's axis2.xml. Enabling this parameter in the axis2.xml will enable sending of SOAP with Attachments messages from all the services deployed in your Axis2 server. <parameter name="enableSwA">true</parameter> Open services.xml [6] to have a look at the complete source listing of the service descriptor.
based installation, start the servlet container.) Visit the services listing (http://localhost:{port_the_server_is_running}/axis2/services/listServices) to check the status of the deployed service.
ServiceClient sender = new ServiceClient(); //org.apache.axis2.client.Options object can be used to configure the service c sender.setOptions(options); OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP); We create the request MessageContext object. Then we create the SOAP Envelope for the request message and set it to the request MessageContext. After that, we can add the request MessageContext to the OperationClient object. Now we are ready to execute the OperationClient in order to send the request message. MessageContext mc = new MessageContext(); SOAPEnvelope env = createEnvelope(projectName); mc.setEnvelope(env); mepClient.addMessageContext(mc); mepClient.execute(true);
2. Retrieving the Attachment Content from the Web Service Response Message
After the execution, we can access the incoming MessageContext from the OperationClient API.
MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL We can access the attachments of the incoming message through the attachment API of the MessageContext by giving the corresponding content-ID values. Once again we need to remember to remove the "cid:" prefix from the value taken out of the "href" attribute.
//retrieving the ID of the attachment String graphImageID = graphElement.getAttributeValue(new QName("href")); //remove the "cid:" prefix graphImageID = graphImageID.substring(4); //Accessing the attachment from the response message context using the ID DataH dataHandler = response.getAttachment(graphImageID);
Resources
Source Code Package [5]
References
Apache Axis2/Java [8] SOAP Messages with Attachments [9] MIME [10](see the references section) Using SOAP with Attachments in Apache Axis2 [11] JavaBeans Activation Framework [12] Content-ID and Message-ID Uniform Resource Locators [13] Apache Ant [14] Axis2 Deployment Model [15] Writing Web Service Clients Using Axis2's Primary APIs [16] SOAP Message Transmission Optimization Mechanism [8]
Author
Thilina Gunarathne, Senior Software Engineer, WSO2 Inc. thilina at wso2 dot com Articles Introductory 2010 WSO2 Inc.
footer Licenses Privacy Policy Terms of Use Community Guidelines Feedback wso2.com
__compete_code = 'a56ed378f1a9259cd4db80722cbf2623'; (function () { var s = document.createElement('script'), d = document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0], t = 'https:' == document.location.protocol ? 'https://c.compete.com/bootstrap/' : 'http://c.compete.com/bootstrap/'; s.src = t + __compete_code + '/bootstrap.js'; s.type = 'text/javascript'; s.async = 'async'; if (d) { d.appendChild(s); } }()); document.write(unescape('%3Cscript type="text/javascript" "'+document.location.protocol+'//dnn506yrbagrg.cloudfront.net/pages/scripts/0011/4922.js"%3E%3C%2Fscript%3 var rw_ext_id=''; var pkBaseURL = (("https:" == document.location.protocol) ?"https://connect.wso2.com/wso2/" : "http://connect.wso2.com/wso2/"); document.write(unescape("%3Cscript src='" + pkBaseURL +"std/resource/script/rwts.js' type='text/javascript'%3E%3C/script%3E")); rw_log(pkBaseURL, 4220);
Source URL: http://wso2.org/library/1675 Links: [1] http://wso2.org/library/1675#ref [2] http://wso2.com/products/create/wso2-web-services-application-server-wsas/?libaddate=09082009 [3] http://wso2.org/files/StatisticsService.java_.txt [4] http://ws.apache.org/axis2/download.cgi [5] http://wso2.org/files/soap_with_attachments_tutorial.zip [6] http://wso2.org/files/services.xml_0.txt [7] http://wso2.org/files/StatisticsServiceClient.java_.txt [8] http://www.w3.org/TR/soap12-mtom/ [9] http://www.w3.org/TR/SOAP-attachments [10] http://en.wikipedia.org/wiki/MIME [11] http://wso2.org/library/1148 [12] http://java.sun.com/products/javabeans/jaf/ [13] http://www.ietf.org/rfc/rfc2392.txt [14] http://ant.apache.org/ [15] http://jaxmag.com/itr/online_artikel/psecom,id,757,nodeid,147.html [16] http://ws.apache.org/axis2/1_2/dii.html