SOAP attachments Question

G

gabsaga.tata

I have a client application that sends a SOAP message with an
attachment to a web service (Apache Axis framework)
The web service:
1) Receives the message
2) Retrieves the attachment
3) Make modifications to the attachment
4) Deletes old attachment from the message
5) Attaches the new modified file to the message

On the client response, when I count the number of attachments, it
gives me zero.

Question I have is that can a client get back MIME attachment from a
web service?

Thanks.

Gabsaga


********************* Client Code ***********************

/*
* Copyright 2003-2005 Simpaq Inc.
*
* This software is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either expressed or implied.
*/

package com.simpaq.message;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Source;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.Name;
import javax.xml.soap.AttachmentPart;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io_OutputStream;
import java.io.BufferedInputStream;
import java.util.Iterator;


/**
* A test driver for the CryptoMessageService message service.
*/
public class ClientAttachmentApp
{

public void msgEnvelope(String[] args) throws Exception
{
if (args.length !=1 )
{
System.out.println("USAGE: java ClientAttachmentApp
<file_to_attach>");
System.exit(1);
}
String inputFile = args[0];

File f = new File(inputFile);
if (!f.exists())
{
System.out.println("File " + inputFile + " does not exists");
System.exit(2);
}

MessageFactory mf = MessageFactory.newInstance();
SOAPMessage smsg = mf.createMessage();
SOAPPart sp = smsg.getSOAPPart();
SOAPEnvelope se = (SOAPEnvelope)sp.getEnvelope();
SOAPBody body = se.getBody();

//Add a namespace declaration to the envelope
se.addNamespaceDeclaration("xsi",
"http://www.w3.org/2001/XMLSchema-instance");

Name bodyName = se.createName("processMessage", "nsg",
"http://com.simpaq.message");
SOAPElement bodyElement = body.addBodyElement(bodyName);

Name childName = se.createName("file");
SOAPElement fileNm = bodyElement.addChildElement(childName);

DataHandler dh = new DataHandler(new FileDataSource(inputFile));
AttachmentPart attachment = smsg.createAttachmentPart(dh);
attachment.setContentId("attached_file");
smsg.addAttachmentPart(attachment);

Name attName = se.createName("href");
fileNm.addAttribute(attName, "cid:" + attachment.getContentId());

System.out.println("REQUEST:");
//Display Request Message
displayMessage(smsg);

SOAPConnection conn =
SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = conn.call(smsg,
"http://localhost/axis/services/CryptoMessageService");

System.out.println("\n");
System.out.println("RESPONSE:");
//Display Response Message
displayMessage(response);

SOAPPart responseSp = response.getSOAPPart();
SOAPEnvelope responseSe = responseSp.getEnvelope();
SOAPBody responseBody = responseSe.getBody();

//Get attached message
if(!responseBody.hasFault())
{
int numOfAttachments = response.countAttachments();
System.out.println("\n\nNumber of attachments in message = " +
numOfAttachments);
Iterator iterator = response.getAttachments();
if(iterator.hasNext())
{
DataHandler dataHandler =
((AttachmentPart)iterator.next()).getDataHandler();
String attachedFileName = dataHandler.getName();
System.out.println("Processed file name = " + attachedFileName);
}
}
}

public String createOutputFileName(String input)
{
String inputFilePath = input.substring(0,
input.lastIndexOf("\\")+1);
String inputFileName = input.substring(input.lastIndexOf("\\")+1);
System.out.println("Input File Path = " + inputFilePath);
System.out.println("Input File Name = " + inputFileName);
String outputFileName = inputFilePath + "attached_" +
inputFileName;

return outputFileName;
}

public void displayMessage(SOAPMessage message) throws Exception
{
TransformerFactory tFact = TransformerFactory.newInstance();
Transformer transformer = tFact.newTransformer();
Source src = message.getSOAPPart().getContent();
StreamResult result = new StreamResult( System.out );
transformer.transform(src, result);
}

public static void main(String[] args)
{
ClientAttachmentApp clientApp = new ClientAttachmentApp();
try
{
clientApp.msgEnvelope(args);
}
catch (Exception e)
{
System.out.println("Exception in main: " + e.toString());
}
}
}

************************* Server side Code (Service)************

/*
* Copyright 2003-2005 Simpaq Inc.
*
* This software is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either expressed or implied.
*/

package com.simpaq.message;

import org.w3c.dom.Element;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.AttachmentPart;
import org.apache.axis.MessageContext;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import java.util.Iterator;
import java.util.Vector;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.IOException;

/**
* Simple message-style service sample.
*/
public class CryptoMessageService
{
public void processMessage(SOAPEnvelope req, SOAPEnvelope resp)
throws javax.xml.soap.SOAPException, Exception
{
MessageContext msgCntxt = MessageContext.getCurrentContext();
SOAPMessage msg = msgCntxt.getMessage();
String attachedFile = getAttachment(msg, req);

System.out.println("\n*********************************************\n");
System.out.println("Attached file = " + attachedFile);
if (success)
{
System.out.println("\nEncryption successful");
}
else
{
System.out.println("\nEncryption unsuccessful");
}

msg.removeAllAttachments();
msg.saveChanges();

SOAPBody body = resp.getBody();
Name bodyName = resp.createName("processMessageResponse", "nsg",
"http://simpaq.com");
SOAPElement bodyElement = body.addBodyElement(bodyName);

Name childName = resp.createName("file");
SOAPElement fileNm = bodyElement.addChildElement(childName);

DataHandler dataHander = new DataHandler(new
FileDataSource("C:\\Tomcat_4_1\\webapps\\axis\\WEB-INF\\attachments\\res_message.xml"));
AttachmentPart attachment = msg.createAttachmentPart(dataHander);
attachment.setContentId("attached_processed_file");
msg.addAttachmentPart(attachment);

msg.saveChanges();

Name attName = resp.createName("href");
fileNm.addAttribute(attName, "cid:" + attachment.getContentId());

Iterator iter = msg.getAttachments();
int attachmentCount = msg.countAttachments();
System.out.println("Server attached [ " + attachmentCount + " ]
file(s).");

if(iter.hasNext())
{
dataHander = ((AttachmentPart)iter.next()).getDataHandler();
String serverFileName = dataHander.getName();
System.out.println("Server file name attached = " +
serverFileName);
}

}

public String getAttachment(SOAPMessage sMsg, SOAPEnvelope msgEnv)
throws javax.xml.soap.SOAPException, Exception
{
SOAPBody body = msgEnv.getBody();

DataHandler dh = null;
String attachedFileName = null;

if(!body.hasFault())
{
Iterator iterator = sMsg.getAttachments();
if(iterator.hasNext())
{
dh = ((AttachmentPart)iterator.next()).getDataHandler();
attachedFileName = dh.getName();
}
}
return attachedFileName;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,357
Latest member
RuthEsteve

Latest Threads

Top