Accessing the Web Service Context

Programming the JWS File 4-11 In addition, in the previous example: ■ The JAXWSProperties.CONNECT_TIMEOUT property is used to define the connection timeout. For a complete list of JAXWSProperties that you can set, see the com.sun.xml.ws.developer.JAXWSProperties Javadoc at http:www.java2s.comOpen-SourceJava-Document6.0-JDK-Modul esjax-ws-runtimecomsunxmlwsdeveloperJAXWSProperties.ja va.java-doc.htm . ■ The BindingProviderProperties.REQUEST_TIMEOUT property is used to define the request timeout. For a complete list of BindingProviderProperties that you can set, see the com.sun.xml.ws.client.BindingProviderProperties Javadoc at http:www.java2s.comOpen-SourceJava-Document6.0-JDK-Modul esjax-ws-runtimecomsunxmlwsclientBindingProviderProper ties.java.java-doc.htm .

4.4.2 Accessing the Web Service Context

The javax.xml.ws.WebServiceContext interface enables you to access from the Web service runtime message context and security information relative to a request being served. Typically, a WebServiceContext is injected into an endpoint using the Resource annotation. For more information, see http:download.oracle.comjavaee5apijavaxxmlwsWebServiceC ontext.html . The following example shows a simple JWS file that uses the context to access HTTP request header information. The code in bold is discussed in the programming guidelines described following the example. package examples.webservices.jws_context; import javax.jws.WebMethod; import javax.jws.WebService; import java.util.Map; import javax.xml.ws.WebServiceContext; import javax.annotation.Resource; import javax.xml.ws.handler.MessageContext; WebServicename=JwsContextPortType, serviceName=JwsContextService, targetNamespace=http:example.org Simple web service to show how to use the Context annotation. Table 4–4 Properties of BindingProvider Property Type Description ENDPOINT_ADDRESS_PROPERTY java.lang.String Target service endpoint address. PASSWORD_PROPERTY java.lang.String Password used for authentication. SESSION_MAINTAIN_PROPERTY java.lang.Boolea n Flag that specifies whether a service client wants to participate in a session with a service endpoint. Defaults to false, indicating that the service client does not want to participate. SOAPACTION_URI_PROPERTY java.lang.String Property for SOAPAction specifying the SOAPAction URI. This property is valid only if SOAPACTION_USE_ PROPERTY is set to true. SOAPACTION_USE_PROPERTY java.lang.Boolea n Property for SOAPAction specifying whether or not SOAPAction should be used. USERNAME_PROPERTY java.lang.String User name used for authentication. 4-12 Getting Started With JAX-WS Web Services for Oracle WebLogic Server public class JwsContextImpl { Resource private WebServiceContext ctx; WebMethod public String msgContextString msg { MessageContext context=ctx.getMessageContext; Map requestHeaders = Mapcontext.getMessageContext.HTTP_REQUEST_HEADERS; } } Use the following guidelines in your JWS file to access the runtime context of the Web service, as shown in the code in bold in the preceding example: ■ Import the javax.annotation.Resource JWS annotation: import javax.annotation.Resource; ■ Import the javax.xml.ws.WebServiceContext API, as well as any other related APIs that you might use: import java.util.Map; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; ■ Annotate a private variable, of data type javax.xml.ws.WebServiceContext, with the field-level Resource JWS annotation: Resource private WebServiceContext ctx; ■ Use the methods of the WebServiceContext class to access runtime information about the Web service. The following example shows how to get the message context for the current service request and subsequently access the HTTP request headers: MessageContext context=ctx.getMessageContext; Map requestHeaders = Mapcontext.getMessageContext.HTTP_REQUEST_HEADERS For more information about the MessageContext property values, see Section 4.4.3, Using the MessageContext Property Values. The following table summarizes the methods of the javax.xml.ws.WebServiceContext that you can use in your JWS file to access runtime information about the Web service. For more information, see http:download.oracle.comjavaee5apijavaxxmlwsWebServiceC ontext.html . Programming the JWS File 4-13

4.4.3 Using the MessageContext Property Values