javasoapsaaj

How to fix file not attaching itself to a SAAJ SOAP message in Java?


I'm trying to build a client for a SOAP web service. I'm reading a request from an xml file to String and converting it to the SOAPMessage class. The body of the SOAP message has two elements, one of which is supposed to be an attached file (element has tag DataFile). Therefore I'm creating an AttachmentPart from a file and adding it to the SOAPMessage. However, the receiving application says that the message doesn't have a file attached (the message's DataHandler==null). Can anyone please tell me why the file isn't being properly sent along with the request?

Here's the SOAP request from the xml file:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xro="http://x-road.eu/xsd/xroad.xsd" xmlns:iden="http://x-road.eu/xsd/identifiers" xmlns:stat="http://stat-v6.x-road.eu">
   <soapenv:Header>
      <xro:protocolVersion>4.0</xro:protocolVersion>
      <xro:issue></xro:issue>
      <xro:userId>EE700031011</xro:userId>
      <xro:id>123</xro:id>
      <xro:service iden:objectType="SERVICE">
         <iden:xRoadInstance>ee-test</iden:xRoadInstance>
         <iden:memberClass>GOV</iden:memberClass>
         <iden:memberCode>70000332</iden:memberCode>
         <!--Optional:-->
         <iden:subsystemCode>estat</iden:subsystemCode>
         <iden:serviceCode>SubmitData</iden:serviceCode>
         <!--Optional:-->
         <iden:serviceVersion>v1</iden:serviceVersion>
      </xro:service>
     <xro:client iden:objectType="SUBSYSTEM"> 
         <iden:xRoadInstance>ee-test</iden:xRoadInstance>
         <iden:memberClass>GOV</iden:memberClass>
         <iden:memberCode>70000332</iden:memberCode>
         <!--Optional:-->
         <iden:subsystemCode>sa-client</iden:subsystemCode>
      </xro:client>
   </soapenv:Header>
   <soapenv:Body>
      <stat:SubmitDataRequest>
         <DataFile>cid:Report.xml</DataFile>
         <XSDValidationOnly>1</XSDValidationOnly>
      </stat:SubmitDataRequest>
   </soapenv:Body>
</soapenv:Envelope>

Report.xml is the file I'm trying to attach to the SOAP message.

And here's my SoapClient class:

import com.sun.istack.internal.Nullable;
import org.apache.commons.io.IOUtils;
import org.xml.sax.InputSource;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.transform.sax.SAXSource;
import java.io.*;
import java.net.URL;

public class SoapClient {
// soapMessage is the request read from an xml file
    SOAPMessage callSoapWebServiceFromString(String soapEndpointUrl, String soapMessage, @Nullable File attachment) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage message = messageFactory.createMessage();
            message.getSOAPPart().setContent(new SAXSource(new InputSource(new StringReader(soapMessage))));

            if (attachment != null) {
                DataHandler handler = new DataHandler(new FileDataSource(attachment));
                AttachmentPart attachPart = message.createAttachmentPart(handler);
                message.addAttachmentPart(attachPart);
                message.saveChanges();

                // String stringToWrite = IOUtils.toString(handler.getInputStream(), "utf-8");
                // System.out.println("stringToWrite: " + stringToWrite);
            }
            URL endpoint = new URL(soapEndpointUrl);
            SOAPMessage response = soapConnection.call(message, endpoint);
            // System.out.println("Response SOAP Message:");
            // response.writeTo(System.out);

            soapConnection.close();

            return response;
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
            e.printStackTrace();
        }
        return null;
    }


}

Solution

  • Found the answer: attachPart.setContentId("<" + attachment.getName() + ">");