views:

236

answers:

3

I am using axis2 to come up with a basic web service which will get the file name as parameter and produces a response SOAP packet which will have the file attached along with the SOAP.

Here is the way I am creating the service code (its simple and inspired by Axis2 sample code)

public String getFile(String name) throws IOException
{
MessageContext msgCtx = MessageContext.getCurrentMessageContext();
File file = new File (name);
System.out.println("File = " + name);
System.out.println("File exists = " + file.exists());
FileDataSource fileDataSource = new FileDataSource(file);
System.out.println("fileDataSource = " + fileDataSource);
DataHandler dataHandler = new DataHandler(fileDataSource);
System.out.println("DataHandler = " + dataHandler);
    String attachmentID = msgCtx.addAttachment(dataHandler);
    System.out.println("attachment ID = " + attachmentID);
    return attachmentID;
}

Now The client side code -

      MessageContext response = mepClient
            .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    SOAPBody body = response.getEnvelope().getBody();
    OMElement element = body.getFirstElement().getFirstChildWithName(
    new QName("http://service.soapwithattachments.sample","return"));
    String attachementId = element.getText();
    System.out.println("attachment id is " + attachementId);
    Attachments attachment = response.getAttachmentMap();
        DataHandler dataHandler = attachment.getDataHandler(attachementId);

Problem is that dataHandler is always null. Though I think at the server side, the file was read and attached along with the SOAP packet. Am I doing something wrong ?

EDIT : I have put <parameter name="enableSwA" locked="false">true</parameter> in the axis2.xml file.

A: 

Also configure temporary folder where attachment will be downloaded

Using axis2.xml or services.xml,

<parameter name="cacheAttachments" locked="false">true</parameter>
<parameter name="attachmentDIR" locked="false">temp directory</parameter>
<parameter name="sizeThreshold" locked="false">4000</parameter>

Programmatically in the client side,

options.setProperty(Constants.Configuration.CACHE_ATTACHMENTS,
                                                   Constants.VALUE_TRUE);
options.setProperty(Constants.Configuration.ATTACHMENT_TEMP_DIR,TempDir);
options.setProperty(Constants.Configuration.FILE_SIZE_THRESHOLD, "4000");
Ravi Gupta
A: 

I found the solution of this issue. The problem was, at the server side, by using MessageContext msgCtx = MessageContext.getCurrentMessageContext(); call, we get the handle of the incoming message context. I was adding the attachment on the incoming message context, whereas, the attachment needs to be added to the outgoing message context. To get the handle of the outgoing message context, following steps needs to be done -

   //this is the incoming message context
    MessageContext inMessageContext  = MessageContext.getCurrentMessageContext();
    OperationContext operationContext = inMessageContext.getOperationContext();
    //this is the outgoing message context
    MessageContext outMessageContext =     operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);

Once the outgoing message context is received, add the attachment here -

String attachmentID = outMessageContext.addAttachment(dataHandler);

Rest of the code remains the same.

More on this can be found here.

Shamik
A: 

Thank you very much you just made my life easier!!!

JavaMeat