Axis2 attachments disappear in response

I use axis2 to create a basic web service that will receive the file name as a parameter and give the response a SOAP package in which the file will be attached along with SOAP.

This is how I create utility code (its simple and inspired by the Axis2 code sample)

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 is

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

The problem is that dataHandler is always null. Although I think that on the server side the file was read and attached along with the SOAP package. Am I doing something wrong?

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

+4
source share
2 answers

I found a solution to this problem. The problem was server-side using the MessageContext msgCtx = MessageContext.getCurrentMessageContext(); call MessageContext msgCtx = MessageContext.getCurrentMessageContext(); , we get the handle to the context of the incoming messages. I added the attachment to the context of the incoming message, while the attachment should be added to the context of the outgoing message. To get the context handle of outgoing messages, you must perform the following steps -

  //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); 

After receiving the context of the outgoing message, add the application here -

 String attachmentID = outMessageContext.addAttachment(dataHandler); 

The rest of the code remains the same.

More about this can be found here .

+2
source

Also configure the temporary folder where the 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> 

Client side software

 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"); 
0
source

Source: https://habr.com/ru/post/1303741/


All Articles