You can use JAX-WS contexts to customize the properties of a consumer proxy. In particular, contexts can be used to modify connection properties and to send data in protocol headers. For example, you could use contexts to add a SOAP header, either to a request message or to a response message. The following types of context are supported on the client side:
Request context — on the client side, the request context enables you to set properties that affect outbound messages. Request context properties are applied to a specific port instance and, once set, the properties affect every subsequent operation invocation made on the port, until such time as a property is explicitly cleared. For example, you might use a request context property to set a connection timeout or to initialize data for sending in a header.
Response context — on the client side, you can access the response context to read the property values set by the inbound message from the last operation invocation. Response context properties are reset after every operation invocation. For example, you might access a response context property to read header information received from the last inbound message.
To set a particular request context property, ContextPropertyName
, to the value, PropertyValue
, use the code shown in Example 8.1, “Setting a Request Context Property on the Client Side”.
Example 8.1. Setting a Request Context Property on the Client Side
// Set request context property. java.util.Map<String, Object> requestContext = ((javax.xml.ws.BindingProvider)port).getRequestContext(); requestContext.put(ContextPropertyName, PropertyValue); // Invoke an operation. port.SomeOperation();
You have to cast the port object to javax.xml.ws.BindingProvider
in order to access the request context. The request context itself is of type, java.util.Map<String, Object>
, which is a hash map that has keys of String and values of arbitrary type. Use java.util.Map.put()
to create a new entry in the hash map.
To retrieve a particular response context property, ContextPropertyName
, use the code shown in Example 8.2, “Reading a Response Context Property on the Client Side”.
Example 8.2. Reading a Response Context Property on the Client Side
// Invoke an operation. port.SomeOperation(); // Read response context property. java.util.Map<String, Object> responseContext = ((javax.xml.ws.BindingProvider)port).getResponseContext(); PropertyType propValue = (PropertyType) responseContext.get(ContextPropertyName);
The response context is of type, java.util.Map<String, Object>
, which is a hash map that has keys String and values of arbitrary type. Use java.util.Map.get()
to access an entry in the hash map of response context properties.