I use Dispatch and got attachments to work inbound/outbound for it. For
outbound, it's basically these steps (carving the relevant lines from my code,
leaving out the rest):
Collection<Attachment> attachments = new LinkedList<Attachment>();
// do a loop here to create your attachments
String id = attachmentEntry.getKey();
AttachmentImpl newAttachment = new AttachmentImpl(id);
attachments.add(newAttachment);
// do a loop to set any headers you have
newAttachment.setHeader(headerName, headerValue);
// there are a couple ways to provide the data for an attachment, I use
a couple of them, but here's the easiest one
byte[] attachmentContent = // your byte content
dataHandler = new DataHandler(new ByteDataSource(attachmentContent,
contentType));
newAttachment.setDataHandler(dataHandler);
// attach that list to your message
dispatch.getRequestContext().put(Message.ATTACHMENTS, attachments);
Do this as about the last thing before you send the message, I recall some sort
of ordering trouble with attachments although maybe that was on the response.
Good luck,
Jesse
-----Original Message-----
From: Daniel Kulp [mailto:[email protected]]
Sent: Tuesday, June 11, 2013 10:50 AM
To: [email protected]; Dennis Sosnoski
Subject: Re: Attachments for Dispatch not supported?
This SHOULD be handled in the WrappedMessageContext, but it looks potentially
incomplete. The "put" is certainly not wired in for the
MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS key. However, it looks like the
get should have returned a viable Map. I guess the suggestion is to breakpoint
in the AttachmentOutInterceptor and see if it's managed to see if an attachment
is there or not.
Dan
On Jun 8, 2013, at 10:41 PM, Dennis Sosnoski <[email protected]> wrote:
> I've been trying to use attachments with the Dispatch api, and haven't been
> able to make it work. After digging through the code I couldn't see anywhere
> that it was even trying to make it work. Is this just not supported?
>
> Attachments work without any problems on the Provider side.
>
> Here's a sample for Dispatch:
>
> // create and configure the dispatch object with actual service
> endpoint address
> Service service = Service.create(new QName("http://cxf.apache.org",
> "attachmentService"));
> QName portQName = new QName("http://cxf.apache.org", "attachmentPort");
> service.addPort(portQName, SOAPBinding.SOAP12HTTP_BINDING, args[0]);
> Dispatch<Source> dispatch = service.createDispatch(portQName,
> Source.class, Service.Mode.PAYLOAD,
> new AddressingFeature(true));
> SOAPBinding binding = (SOAPBinding)dispatch.getBinding();
> binding.setMTOMEnabled(true);
>
> // configure logging to view messages being exchanged
> ...
>
> // set attachment to be sent
> Map<String, Object> requestContext = dispatch.getRequestContext();
> Map<String, DataHandler> attachments = (Map<String,
> DataHandler>)requestContext.
> get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
> if (attachments == null) {
> attachments = new HashMap<String, DataHandler>();
> requestContext.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, attachments);
> }
> byte[] bytes = new byte[1024];
> for (int i = 0; i < bytes.length; i++) {
> bytes[i] = (byte)i;
> }
> attachments.put("attachment-1", new DataHandler(new
> ByteArrayDataSource(bytes)));
>
> // configure the WS-Addressing Action to select operation
> ...
>
> // execute the request
> Source response = dispatch.invoke(new StreamSource(new
> ByteArrayInputStream("<test/>".getBytes("UTF-8"))));
> System.out.println("Success");
>
> The request gets sent using mime, but without the attachment. Here's the
> Provider code that works:
>
> @MTOM(threshold = 0, enabled = true)
> @WebServiceProvider(
> portName = AttachConstants.PORT_LOCALNAME, serviceName =
> AttachConstants.SERVICE_LOCALNAME,
> targetNamespace = AttachConstants.SERVICE_NAMESPACE)
> @ServiceMode(value = Service.Mode.PAYLOAD)
> @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
> public class AttachProvider implements Provider<StreamSource> {
> /** Injected context for invocations. */
> @Resource
> private WebServiceContext serviceContext;
>
> private void setAttachment(DataHandler handler) {
> Map<String, DataHandler> attachments = new HashMap<String,
> DataHandler>();
> attachments.put(AttachConstants.OUT_ATTACH_ID, handler);
> serviceContext.getMessageContext().put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS,
> attachments);
> }
>
> public StreamSource invoke(StreamSource request) {
> ...
> byte[] bytes = new byte[AttachConstants.ATTACHMENT_SIZE];
> for (int i = 0; i < AttachConstants.ATTACHMENT_SIZE; i++) {
> bytes[i] = (byte)i;
> }
> setAttachment(new DataHandler(new
> ByteArrayDataSource(bytes)));
>
> Has anyone else gotten Dispatch attachments to work?
>
> Thanks,
>
> - Dennis
>
> --
>
> Dennis M. Sosnoski
> Java SOA and Web Services Consulting
> <http://www.sosnoski.com/consult.html>
> CXF and Web Services Security Training
> <http://www.sosnoski.com/training.html>
> Web Services Jump-Start <http://www.sosnoski.com/jumpstart.html>
>
--
Daniel Kulp
[email protected] - http://dankulp.com/blog Talend Community Coder -
http://coders.talend.com