Fórum

Accessing attachments of WebService response

thumbnail
Basanagowda Patil, modificado 12 Anos atrás.

Accessing attachments of WebService response

New Member Postagens: 4 Data de Entrada: 13/04/11 Postagens Recentes
I am using Axis2 environment to access the Attachments present in the WebService Response, The code works fine when I run the client from main program. But when I call the same method from the portlet the 0byte fill will get downloaded.

So to make Portlet aware of the attachment present in the WebService Response Do I need to make any Configuration changes?


/*
* Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

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;

import com.liferay.util.servlet.ServletResponseUtil;

public class StatisticsServiceClient {

private static EndpointReference targetEPR = new EndpointReference(
"http://localhost:9090/axis2/services/StatisticsService");

public static void main(String[] args) throws Exception {
if (args.length == 0) {
//System.out.println(args[0]);
//getMonthlyDownloadStats(args[0]);
getMonthlyDownloadStats("axis2");
} 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);


/*
* Uncomment to enable client side file caching for the response.
*/
/*
options.setProperty(Constants.Configuration.CACHE_ATTACHMENTS,
Constants.VALUE_TRUE);
options.setProperty(Constants.Configuration.ATTACHMENT_TEMP_DIR,"temp_dir");
options.setProperty(Constants.Configuration.FILE_SIZE_THRESHOLD, "4000");
*/

// 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("D:/patil/SVN/trunk/docs/GMC/ResearchresponseGraph.png");
//InputStream is = new FileInputStream(graphFile);
//ServletResponseUtil.sendFile(response, "ResearchresponseGraph.png", is, "application/pdf");
FileOutputStream outputStream = new FileOutputStream(graphFile);
dataHandler.writeTo(outputStream);
outputStream.flush();
System.out.println("Download statistics graph saved to :" + graphFile.getAbsolutePath());
if (outputStream != null) {
outputStream.close();
}


}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;
}
}


Thanks,
Patil
thumbnail
Basanagowda Patil, modificado 12 Anos atrás.

RE: Accessing attachments of WebService response

New Member Postagens: 4 Data de Entrada: 13/04/11 Postagens Recentes
As such no configuration required. The above code works fine with the pdf, it was not working with .png images.

Thanks,
Patil
Portal and CMS consultant