Friday August 17, 2007
TOTD #2: Change the endpoint address on a pre-generated Web services Stub
The client-side stubs generated by JAX-WS (part of Metro) are pre-configured with the endpoint address specified in the WSDL. This allows you to invoke the endpoint directly without any additional configuration. However there may be a need to send the exact same Web service request to an endpoint hosted at a different address. For example, the stubs may be generated from a WSDL hosted in the test environment and the request needs to be sent to an endpoint hosted in production environment that is very likely to be on a different machine.
This tip tells you how to change the endpoint address on a pre-generated stub so that it invokes a different endpoint.
The code to invoke a pre-configured stub (using the endpoint address from the WSDL) looks like:
Hello port = new HelloService().getHelloPort();
String result = port.sayHello("Duke!");
As per the JAX-WS 2.1 specification, each proxy implements javax.xml.ws.BindingProvider interface. In order to invoke an endpoint whose address is different from the one specified in the WSDL, you need to insert the following line between the two lines shown above:
((javax.xml.ws.BindingProvider)port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"NEW_ADDRESS_HERE");
Just replace NEW_ADDRESS_HERE with your new endpoint address and the exact same request will be directed to the new endpoint. The WSDL contract must be exactly same though otherwise you may get an exception back.
Please leave suggestions on TOTD that you'd like to see. A complete archive is available here.
Technorati: totd webservices metro soap glassfish
Posted by Arun Gupta in webservices | Comments[3]
|
|
|
|
Hi, Arun!
I found some limits in your suggestion posted to Change the endpoint address. In your case, the app server where the pre-generated stubs come from must be started. If not, this line "Hello port = new HelloService().getHelloPort();" could not be excuted correctly because the method firstly would access the wsdl pre-defineded and if it failed, then exception was thrown. So, "((javax.xml.ws.BindingProvider)port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "NEW_ADDRESS_HERE");" would never be excuted.
I tried another way, and found it was OK and didn't need the original app server be started. It needed to define the new endpoint URL and the QName. For example,
URL endpoint_new = new URL("NEW_ADDRESS_HERE");
QName qname = new QName("http://"+"ORIGINAL_PACKAGE","SERVICENAME");
ORIGINAL_PACKAGE means the package where the service published in, SERVICENAME means the name of the service we need, for example,HelloService.
Put these two line in top of the method, and do not need the "((javax.xml.ws.BindingProvider)port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "NEW_ADDRESS_HERE");"
Posted by Qil.Wong on August 24, 2007 at 01:51 AM PDT #
Sorry, I lost some information.
After definition of new endpoint URL and QName, we create service object by:
Hello port = new HelloService(endpoint_new,qname).getHelloPort();
replace "Hello port = new HelloService().getHelloPort();"
Posted by Qil.Wong on August 24, 2007 at 01:56 AM PDT #
Thanks for the tip on TOTD :)
Posted by Arun Gupta on August 27, 2007 at 01:28 PM PDT #