I have a custom SOAP message handler for incoming messages that will run different code based on which operation is being called. My first try to get the operation name looked something liket this:
public boolean handleMessage(SOAPMessageContext context)
{
String op = context.get(MessageContext.WSDL_OPERATION);
...
This failed because the property MessageContext.WSDL_OPERATION
appears to never be set. I then tried using this:
public boolean handleMessage(SOAPMessageContext context)
{
Map<?, ?> headers = (Map<?, ?>)context.get(MessageContext.HTTP_REQUEST_HEADERS);
ArrayList<String> SOAPAction = ((ArrayList<String>) headers.get("SOAPAction"));
String opName = SOAPAction.get(0);
//opName will be formatted like "urn#myOperation", so the prefix must be removed
opName = ((opName.replace("\"","").split("#"))[1]);
This works, but I'm concerned there could be situations where the header property "SOAPAction" isn't set (or doesn't even exist), or does not have the value that I'm expecting it to. I'm also a little concerned because I don't know if this is an "official" way to get the operation name - I figured it out by looking at the contents of context
in the debugger.
Is there any better way to get the operation name when handling incoming SOAP messages?
Java™ API for XML Web Services (JAX-WS) provides you with a standard way of developing interoperable and portable web services. Use JAX-WS handlers to customize web services requests or response handling.
Overview of SOAP Message Handlers A SOAP message handler provides a mechanism for intercepting the SOAP message in both the request and response of the Web Service. A simple example of using handlers is to access information in the header part of the SOAP message.
SOAPMessage has writeTo() method which is used to print its content into a stream.
This annotation is typically used in scenarios where embedding the handler configuration directly in the Java source is not appropriate; for example, where the handler configuration needs to be shared across multiple Web Services, or where the handler chain consists of handlers for multiple transports.
You could call body.getElementName().getLocalName()
to retrieve the name of SOAP body element of the message payload. It's a little bit verbose and manual but it works. You could have the following in your handler
if ((boolean) context.get(MessageContext.MESSAGE_INBOUND_PROPERTY){ //for requests only
SOAPEnvelope msg = context.getMessage().getSOAPPart().getEnvelope(); //get the SOAP Message envelope
SOAPBody body = msg.getBody();
String operationName = body.getChildNodes().item(1).getLocalName();
}
The result of the above code is guaranteed to carry the name of the operation as specified in your WSDL
EDIT: This solution is based solely on the condition that the web service is implemented as document/literal-wrapped or RPC/literal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With