anirvan
anirvan

Reputation: 4877

SAAJ compatibility with JAXB

I've been working on creating a SAAJ based client. Everything seemed to be working fine, until I implemented the logic to send attachments as part of a web-service request.

The web-service operation is simple - it expects a string element for file-location, and a base64binary element for the file content.

I've tested the ws operation using SoapUI, and everything seems to be in order. However, when i send the file attachment from my SAAJ-based client, the web-service operation would only receive the file-location element's value. I wrote a handler at the ws-server to intercept the WS operation request, in order to see whether the attachment even reaches the web-service. As expected, the attachment was reaching fine, and i could access its contents using the SAAJ api within the handler.

That just leads me to wonder - is there any compatibility issue when sending attachments using SAAJ and receiving them through JAXB bindings? is there something i'm missing out?

thanks for any help!

Upvotes: 1

Views: 1423

Answers (1)

bdoughan
bdoughan

Reputation: 149047

You need to ensure that an AttachmentUnmarshaller is registered on your Unmarshaller to receive attachments in JAXB.

import javax.activation.DataHandler;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.attachment.AttachmentUnmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(Demo.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setAttachmentUnmarshaller(new MyAttachmentUnmarshaller());
    }

    private static class MyAttachmentUnmarshaller extends AttachmentUnmarshaller {

        @Override
        public DataHandler getAttachmentAsDataHandler(String cid) {
            // TODO - Lookup MIME content by content-id, cid, and return as a DataHandler.
            ...
        }

        @Override
        public byte[] getAttachmentAsByteArray(String cid) {
            // TODO - Retrieve the attachment identified by content-id, cid, as a byte[]
            ...
        }

    }

}

Upvotes: 1

Related Questions