On querying the web service from which I expect to download binary content using SOAP with Attachments, I get the following XML response at the client end:
Code:
<swa:getStatsResponse xmlns:swa="http://service.sample/xsd">
<swa:projectName>Apache Axis2</swa:projectName>
<swa:month>2007 January</swa:month>
<swa:downloads>34000</swa:downloads>
<swa:graph href="cid:urn:uuid:F12ADB3500A9B80EDE1211954919086" />
</swa:getStatsResponse>
From the above XML Response, I can extract the attachment id from the 'href' part of the swa:graph tag.
The binary attachment needs this attachmentId for identification, to be pulled out of the SwA response.
I have 2 questions at this stage:
1) I used SmartSniff, and saw that all I was getting at my client end was the above XML Response when I queried the server. If I am not able to get the entire response, how do I even extract the attachment out of it?
2) If I manage to get the attachment part of the response, how exactly would I be able to retrieve it?
I referred to the J2SE Apache Axis Client code that gets the binary attachment using SwA. Doing it on the BlackBerry is the task at hand..
For reference, the J2SE code of the Apache Axis client which is used to download the attachment, is as follows:
Code:
import java.io.File;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.wsdl.WSDLConstants;
public class StatisticsServiceClient {
private static EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/StatisticsService");
public static void main(String[] args) throws Exception {
if (args.length == 1) {
System.out.println(args[0]);
getMonthlyDownloadStats(args[0]);
} else {
throw new IllegalArgumentException("Please provide the project name as an argument.");
}
}
public static void getMonthlyDownloadStats(String projectName) throws Exception {
Options options = new Options();
options.setTo(targetEPR);
options.setAction("urn:getStats");
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
// Increase the time out to receive large attachments
options.setTimeOutInMilliSeconds(10000);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);
MessageContext mc = new MessageContext();
SOAPEnvelope env = createEnvelope(projectName);
mc.setEnvelope(env);
mepClient.addMessageContext(mc);
mepClient.execute(true);
// Let's get the message context for the response
MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
SOAPBody body = response.getEnvelope().getBody();
OMElement element = body.getFirstChildWithName(new QName("http://service.sample/xsd","getStatsResponse"));
if (element!=null) {
processResponse(response, element);
} else {
throw new Exception("Malformed response.");
}
}
private static void processResponse(MessageContext response, OMElement element) throws Exception {
System.out.println("Project Name : " + element.getFirstChildWithName(new QName("http://service.sample/xsd","projectName")).getText());
System.out.println("Month : " + element.getFirstChildWithName(new QName("http://service.sample/xsd","month")).getText());
System.out.println("Downloads : " + element.getFirstChildWithName(new QName("http://service.sample/xsd","downloads")).getText());
OMElement graphElement = element.getFirstChildWithName(new QName("http://service.sample/xsd","graph"));
//retrieving the ID of the attachment
String graphImageID = graphElement.getAttributeValue(new QName("href"));
//remove the "cid:" prefix
graphImageID = graphImageID.substring(4);
//Accesing the attachment from the response message context using the ID
System.out.println(graphImageID);
DataHandler dataHandler = response.getAttachment(graphImageID);
if (dataHandler!=null){
// Writing the attachment data (graph image) to a file
File graphFile = new File("responseGraph.png");
FileOutputStream outputStream = new FileOutputStream(graphFile);
dataHandler.writeTo(outputStream);
outputStream.flush();
System.out.println("Download statistics graph saved to :" + graphFile.getAbsolutePath());
} else {
throw new Exception("Cannot find the data handler.");
}
}
private static SOAPEnvelope createEnvelope(String destinationFile) {
SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope env = fac.getDefaultEnvelope();
OMNamespace omNs = fac.createOMNamespace("http://service.sample/xsd", "swa");
OMElement statsElement = fac.createOMElement("getStats", omNs);
OMElement nameEle = fac.createOMElement("projectName", omNs);
nameEle.setText(destinationFile);
statsElement.addChild(nameEle);
env.getBody().addChild(statsElement);
return env;
}
}