user10753685
user10753685

Reputation: 39

DataHandler in SOAP request

The webservice requires me to set a DataHandler type that should be an xml attachment.

DataSource dataSource = new FileDataSource(tempFile.getAbsolutePath()); 
DataHandler dataHandler = new DataHandler(dataSource);
request.setDataHandler(dataHandler);

The problem is that the SOAPMessage generated from Axis2 has the value as base64

<dataHandler>big string64 string representing my content</dataHandler>

where instead it should be

<dataHandler><inc:Include href="cid:attachmentid" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></dataHandler>

Content-Type: text/xml; charset=us-ascii; name=Sample.xml
Content-Transfer-Encoding: 7bit
Content-ID: <attachmentid>
Content-Disposition: attachment; name="Sample.xml"; filename="Sample.xml"

... the xml content....

WSDL

  <xsd:element name="dataHandler" type="xsd:base64Binary" maxOccurs="1" minOccurs="1" xmime:expectedContentTypes="application/octet-stream"/>

What can I do to solve this issue?

Upvotes: 1

Views: 3083

Answers (1)

Svit
Svit

Reputation: 81

I had a value in my xmlobject which were a DataHandler, this is my solution for putting the datahandler into the value as an object with xml inside it:

XmlObject with the rootelement:

RootXmlObject xml = new RootXmlObject();

Then you setup marshaller to convert to string:

JAXBContext context = JAXBContext.newInstance(RootXmlObject.class);
Marshaller mar= context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
mar.marshal(xml, sw);
String xmlString = sw.toString();

Create a custom Datahandler:

DataHandler.setDataContentHandlerFactory(new YourDatahandler());
private class YourDatahandler implements DataContentHandlerFactory {

    @Override
    public DataContentHandler createDataContentHandler(String mimeType) {

        return new XmlDataContentHandler();
    }
}

public static class XmlDataContentHandler implements DataContentHandler {


    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] {DataFlavor.stringFlavor};
    }
    @Override
    public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
        return new String("Whateverstring");
    }

    @Override
    public Object getContent(DataSource dataSource) throws IOException {
        return new String("Whateverstring");

Which "writeTo" method works like this:

    @Override
    public void writeTo(Object o, String s, OutputStream outputStream) throws IOException {
        byte[] stringByte = (byte[]) ((String) o).getBytes("UTF-8");
        outputStream.write(stringByte);
    }

Finally insert the xmlString to the Datahandler:

DataHandler testHandler = new DataHandler(xmlString, "text/xml");

Here is link to my project: https://github.com/habelo/domibusdemo

Upvotes: 2

Related Questions