uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

10.21.2 Injecting a Web Services context

The javax.xml.ws.WebServiceContext interface is one of the service APIs explained in the JAX-WS 2.2 specifications, section 5.3. For using the Web services context, inject a resource by using the javax.annotation.Resource annotation.

Specify the javax.annotation.Resource annotation in the javax.xml.ws.WebServiceContext type field or in the setter method of Web Services Implementation Class or Provider Implementation Class. The setter method uses the javax.xml.ws.WebServiceContext type as an argument. By specifying the javax.annotation.Resource annotation in the above mentioned field or method, you can inject the information regarding the request being processed in the field that corresponds to the specified field or the specified setter method. You can access the message context information by acquiring the message context by using the getMessageContext method of the javax.xml.ws.WebServiceContext interface. However, you cannot acquire the message context by using the getMessageContext method when accessing EJB Web Services Implementation Class as an EJB You can optionally do the following to access the message context information:

For the getMessageContext method, see 19.2.3(2) javax.xml.ws.WebServiceContext interface, and for the message contexts, see 19.2.5 Using a message context.

Organization of this subsection
(1) Specifying the javax.annotation.Resource annotation
(2) Notes when adding a user-defined message context property

(1) Specifying the javax.annotation.Resource annotation

When using the javax.annotation.Resource annotation to inject a Web Services context, specify the javax.annotation.Resource annotation in the following fields or methods of Web Services Implementation Class or Provider Implementation Class (including the parent Implementation Class). The operation is not guaranteed if you specify the javax.annotation.Resource annotation in the following fields or methods. Also, you can specify the annotation either in the following fields or in the setter method corresponding to these fields.

Further, when using the javax.annotation.Resource annotation to inject a Web Services context, you cannot specify any elements in the javax.annotation.Resource annotation. If you specify any element, the operation is not guaranteed.

The specification example of the javax.annotation.Resource annotation is as follows:

(2) Notes when adding a user-defined message context property

This subsection describes the notes when adding a user-defined message context property:

(a) Adding a property in the service-side handler when inbounding

You can add the user-defined message context properties by using the service-side handler for inbounding. When referencing these properties from Web Services Implementation Class or Provider Implementation Class, you must first set the user- defined message context properties in the service-side handler and then use the setScope(java.lang.String name, MessageContext.Scope scope)method, which is an API of the javax.xml.ws.handler.MessageContext interface, for setting the user-defined properties as the APPLICATION scope.

For referencing the user-defined message context properties (added in the service-side handler) from the service-side handler when outbounding, set only the user-defined message context properties in the service-side handler. You need not set the scope by using the setScope(java.lang.String name, MessageContext.Scope scope)method. For the setScope(java.lang.String name, MessageContext.Scope scope)method, see 19.2.4(8) javax.xml.ws.handler.MessageContext interface.

The following example shows how to add the user-defined properties in the service-side handler when inbounding:

import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
 
public class ServiceSOAPHandlerImpl implements SOAPHandler<SOAPMessageContext> {
 
    public boolean handleMessage(SOAPMessageContext smContext) {
        // Acquire the message direction
        boolean outbound = (boolean)smContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if(outbound) {
           // Outbounding processes
           ...
        } else {
           // Inbounding processes
 
           // Specify the property key in ("userPropKey")
           // and a value in ("userPropValue")
           smContext.put("userPropKey", "userPropValue");
           // Set the property scope as the APPLICATION scope
           smContext.setScope("userPropKey", MessageContext.Scope.APPLICATION);
           ...
        }
        ...
    }
    ...
}
(b) Adding a property in Web Services Implementation Class or Provider Implementation Class

For referencing the user-defined message context properties that are added in Web Services Implementation Class or Provider Implementation Class from the service-side handler when outbounding, set only the user-defined message context properties in Web Services Implementation Class or Provider Implementation Class. Do not set the scope by using the setScope(java.lang.String name, MessageContext.Scope scope)method. The operation is not guaranteed if you use the setScope(java.lang.String name, MessageContext.Scope scope)method.