Sending transport headers with JAX-WS
You can enable an existing Java™ API for XML-Based Web Services (JAX-WS) web services client to send application-defined information along with your web services requests by using transport headers. In addition, you can enable a JAX-WS Web services endpoint to send application-defined information along with the Web services response message by using transport headers.
Before you begin
You need a JAX-WS web services client that you can enable to send transport headers.
Sending transport headers is supported in JAX-WS Web services clients, and is supported for the HTTP and JMS transports. The web services client must call the JAX-WS APIs directly and not through any intermediary layers, such as a gateway function.
About this task
When using the JAX-WS programming model, the client must set a property on the BindingProvider's RequestContext object to send values in transport headers with the Web services request message. After you set the property, the values are set in all the requests for subsequent remote method invocations against the BindingProvider object until the associated property is set to null or the BindingProvider object is discarded.
To send values in the transport headers on outbound requests from a JAX-WS Web services client application, modify the client code as follows:
Procedure
Results
You have a JAX-WS web services client that is configured to send transport headers in Web services request messages.
Example
public class MyApplicationClass {
// Inject an instance of the service's port-type.
@WebServiceRef(EchoService.class)
private EchoPortType port;
// This method will invoke the web service operation and send transport headers on the request.
public void invokeService() {
// Set up the Map that will contain the request headers.
Map<String, Object> requestHeaders = new HashMap<String, Object>();
requestHeaders.put("MyHeader1", "This is a string value");
requestHeaders.put("MyHeader2", new Integer(33));
requestHeaders.put("MyHeader3", new Boolean(true));
// Set the Map as a property on the RequestContext.
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(com.ibm.websphere.webservices.Constants.REQUEST_TRANSPORT_PROPERTIES, requestHeaders);
// Invoke the web services operation.
String result = port.echoString("Hello, world!");
}
}
Sending response transport headers from a JAX-WS endpoint
Before you begin
Sending response transport headers from a JAX-WS endpoint is similar to sending request transport headers from a JAX-WS client. It is supported for the HTTP and JMS transports.
About this task
Procedure
- Create a java.util.Map object that contains the transport headers.
- Add an entry (key and value) to the Map object for each
transport header that you want to send in the response message.
This is similar to the previous procedure for the client.
- Retrieve the MessageContext (instance of
javax.xml.ws.handler.MessageContext
) associated with the invocation of the endpoint. - Set the Map object on the MessageContext using the com.ibm.websphere.webservices.Constants.RESPONSE_TRANSPORT_PROPERTIES property.
Results
Example
@WebService
public class EchoServiceImpl implements EchoServicePortType {
// Inject an instance of WebServiceContext so we can retrieve
// the MessageContext for each invocation of this endpoint.
@Resource
WebServiceContext ctxt;
/**
* Default constructor.
*/
public EchoServiceImpl() {
....
}
public String echoString(String input) {
String result = "Echo result: " + input;
// Retrieve the MessageContext from the injected WebServiceContext.
MessageContext mc = ctxt.getMessageContext();
// Send some headers back in the response message.
Map<String, Object> responseHeaders = new HashMap<String, Object>();
responseHeaders.put("MyHeader1", "This is a string response value");
responseHeaders.put("MyHeader2", new Integer(33));
responseHeaders.put("MyHeader3", new Boolean(false));
// Set the response header Map on the MessageContext.
mc.put(com.ibm.websphere.webservices.Constants.RESPONSE_TRANSPORT_PROPERTIES, responseHeaders);
return result;
}
}