Question

I am building a Java web service. My environment is Java EE6, Glassfish 3.1.2 I need to send some files to my web service so I used SOAP attachment method. In my web service class, I need to get all my attachment parts. The below is my code:

@WebService
//@MTOM
public class UploadServiceImpl implements UploadService {

@Resource
WebServiceContext wsctx;


@Override
public String upload() {
            // This below line is CASTING error
    SOAPMessageContext soapMessageContext = (SOAPMessageContext)wsctx.getMessageContext();

    SOAPMessage soapMessage = soapMessageContext.getMessage();

    soapMessage.getAttachments();
    // ... 

    return "done";
}
}

I received CASTING error becayse I am casting wsctx.getMessageContext() to SOAPMessageContext. wsctx.getMessageContext() is NOT instance of SOAPMessageContext.

My question is: WHY in handlers (SOAPHandler). I am able to cast MessageContext to SOAPMessageContext but in WebService class, I am not able to?

Anyone know what is the issue? How can I access SOAPMessage in webservice class? (Annotated by @WebService). Thank you very much!

Was it helpful?

Solution

The reason why is that the web services API is designed to be protocol-independent, and SOAP is just one of many potential protocols you could use to implement your web services, so it makes sense that the message context is not a SOAPMessageContext unless the container is sure that you're dealing exclusively with the SOAP protocol, like in the case of a SOAPHandler.

But it's ok, you can still retrieve your attachments, try this,

@Override
public String upload() {

  MessageContext mc = wsctx.getMessageContext();
  Map<String, DataHandler> atts = mc.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);

  // ... 

  return "done";
}

the map atts maps the Content-ID for every attachment to its respective DataHandler. To see all the other properties you can get from the message context, check the docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top