Programming the JWS File 4-9
4.4.1 Accessing the Protocol Binding Context
The javax.xml.ws.BindingProvider interface enables you to access from the client application the request and response context of the protocol binding. For more
information, see http:download.oracle.comjavaee5apijavaxxmlwsBindingProv
ider.html . For more information about developing Web service client files, see
Invoking Web Services on page 6-1.
The following example shows a simple Web service client application 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.hello_world.client; import javax.xml.namespace.QName;
import java.net.MalformedURLException; import java.net.URL;
import java.util.Map;
import javax.xml.ws.BindingProvider; import javax.xml.ws.handler.MessageContext;
import com.sun.xml.ws.developer.JAXWSProperties; import com.sun.xml.ws.client.BindingProviderProperties;
This is a simple standalone client application that invokes the the codesayHelloWorldcode operation of the Simple Web service.
public class Main { public static void mainString[] args {
HelloWorldService service; try {
service = new HelloWorldServicenew URLargs[0] + ?WSDL, new QNamehttp:hello_world.webservices.examples,
HelloWorldService ; } catch MalformedURLException murl { throw new RuntimeExceptionmurl; }
HelloWorldPortType port = service.getHelloWorldPortTypePort; String result = null;
result = port.sayHelloWorldHi there; System.out.println Got result: + result ;
Map requestContext = BindingProviderport.getRequestContext;
requestContext.putBindingProvider.ENDPOINT_ADDRESS_PROPERTY, http:examples.comHelloWorldImplHelloWorldService;
requestContext.putJAXWSProperties.CONNECT_TIMEOUT, 300; requestContext.putBindingProviderProperties.REQUEST_TIMEOUT, 300;
Map responseContext = BindingProviderport.getResponseContext; Integer responseCode =
IntegerresponseContext.getMessageContext.HTTP_RESPONSE_CODE; Note:
The com.sun.xml.ws.developer.JAXWSProperties and com.sun.xml.ws.client.BindingProviderProperties
APIs are supported as an extension to the JDK 6.0, provided by Sun Microsystems. Because the APIs are not provided as part of the JDK
6.0 kit, they are subject to change. For more information, see
http:www.java2s.comOpen-SourceJava-Document6.0- JDK-Modulesjax-ws-runtimecom.sun.xml.ws.developer.
htm
4-10 Getting Started With JAX-WS Web Services for Oracle WebLogic Server
... }
}
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.xml.ws.BindingProvider API, as well as any other related APIs that you might use:
import java.util.Map; import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext; import com.sun.xml.ws.developer.JAXWSProperties;
import com.sun.xml.ws.client.BindingProviderProperties; import com.sun.xml.ws.client.BindingProviderProperties;
■
Use the methods of the BindingProvider class to access the binding protocol context information. The following example shows how to get the request and
response context for the protocol binding and subsequently set the target service endpoint address used by the client for the request context, set the connection and
read timeouts in milliseconds for the request context, and set the HTTP response status code for the response context:
Map requestContext = BindingProviderport.getRequestContext; requestContext.putBindingProvider.ENDPOINT_ADDRESS_PROPERTY,
http:examples.comHelloWorldImplHelloWorldService; requestContext.putJAXWSProperties.CONNECT_TIMEOUT, 300;
requestContext.putBindingProviderProperties.REQUEST_TIMEOUT, 300; Map responseContext = BindingProviderport.getResponseContext;
Integer responseCode = IntegerresponseContext.getMessageContext.HTTP_RESPONSE_CODE;
The following table summarizes the methods of the javax.xml.ws.BindingProvider
that you can use in your JWS file to access runtime information about the Web service.
One you get the request or response context, you can access the BindingProvider property values defined in the following table and the MessageContext property
values defined in Section 4.4.3, Using the MessageContext Property Values.
Table 4–3 Methods of the BindingProvider
Method Returns
Description
getBinding Binding
Returns the binding for the binding provider. getRequestContext
java.Util.Map Returns the context that is used to initialize the message and
context for request messages. getResponseContext
java.Util.Map Returns the response 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