views:

1776

answers:

1

So I need to consume a Web Service that uses a custom SoapHeader, as described below. What is the simplest way to pass the correct values through this header using Java. I'm using Netbeans.

<?xml version="1.0" encoding="utf-8"?\>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  <soap:Header>
    <CustomSoapHeader xmlns="http://snip"&gt;
      <UserName>"string"</UserName>
      <Password>"string"</Password>
    </CustomSoapHeader>
  </soap:Header>
  <soap:Body>
    <SomeWebMethod xmlns="http://snip" />
  </soap:Body>
</soap:Envelope>

EDIT: What's the best way to display XML on Stack Overflow?

It might help to add that the Web Service is implemented in .NET and I cannot change the server side code.

+1  A: 

Here are the basic steps, assuming you're doing this on the client side:

  • Install a HandlerResolver on your service interface (service.setHandlerResolver())
  • Override HandlerResolver.getHandlerChain() to insert your own implementation of SOAPHandler
  • Implement SOAPHandler.handleMessage() to modify the SOAP header before it's sent out

You can pass parameters to your handler through the request context:

Map<String, Object> context = ((BindingProvider) port).getRequestContext();
context.put("userName', "foo");
...

in handleMessage() you can get at the header like this:

public boolean handleMessage(SOAPMessageContext context) {
    ...

    SOAPMessage msg = context.getMessage();
    msg.getSoapHeader();
    ...

}

Hope that helps. I'm guessing there's also a way to do this stuff with annotations as well.

Dave Ray