It turns out that it is very simple to extract SOAP attachments. All you have to do is to write a handler that will be called during the web service call. Any number of handlers can be set in the BindingProvider. For example, this is my code to set the handlers:
MyServicePort port = mws.getMyServicePort();
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
DEFAULT_WS_URL);
Binding binding = bp.getBinding();
// Add the logging handler
List handlerList = binding.getHandlerChain();
if (handlerList == null) {
handlerList = new ArrayList();
binding.setHandlerChain(handlerList);
}
handlerList.add(handler);
With this code, we only need to write our handler which will handle the message and can do whatever it wants with it - including accessing attachments.
public class SOAPAttachmentHandler
implements SOAPHandler<SOAPMessageContext> {
private Collection<Attachment> attachments;
@Override
public boolean handleFault(SOAPMessageContext context) {
context.getMessage();
return true;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
attachments = ((SOAPMessageContextImpl)context).
getWrappedMessage().getAttachments();
return true;
}
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public void close(MessageContext context) {
// blank
}
public Collection<Attachment> getAttachments() {
return attachments;
}
The class above extracts the attachments and store then in a class variable. After invoking the web service, I can access the attachments.
2 comments:
I'd rather use water to extract soap from my axis, but hey, this is a free country! :D
Hahaha, I didn't see your comment until today, because I had the wrong email account. Very funny :-P
Post a Comment