Quantcast
Channel: Apache Timeline
Viewing all articles
Browse latest Browse all 5648

Attachments for Dispatch not supported?

$
0
0
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>

Viewing all articles
Browse latest Browse all 5648

Trending Articles