Apache CXF Client for Dynamic Endpoints
Asked Answered
K

3

27

I'm now using Apache CXF as a web services client for a .NET service to get around NTLM authentication. It works great, but I'm wondering why I can't seem to be able to set the web service target endpoint. CXF seems to want the WSDL at runtime for some strange reason - not sure. It takes the physical endpoint from the WSDL, which works fine in test environments I guess, but at deployment time it's sure to change.

Here's some code to demonstrate:

        MyWebServices service = new MyWebServices ();
        MyWebServicesSoap port = service.getMyWebServicesSoap12();

        // Turn off chunking so that NTLM can occur
        Client client = ClientProxy.getClient(port);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(36000);
        httpClientPolicy.setAllowChunking(false);
        http.setClient(httpClientPolicy);

        port.doSomethingUseful();

Again, there is no place that I can see in the CXF client API that allows me to set the service endpoint. Not that I can see anyway. In this case, the target is http://localhost/integration/webservices/mywebservices.asmx, but I could be anywhere. Surely this pedestrian problem is solved somehow?

Kimono answered 10/6, 2010 at 22:5 Comment(0)
D
49

Try the following:

MyWebServicesSoap port = service.getMyWebServicesSoap12();
BindingProvider provider = (BindingProvider) port;
provider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint); 

Alternatively, MyWebServices might have other getXXX methods that take a URL for the WSDL location

Defense answered 10/6, 2010 at 22:21 Comment(5)
In the MyServices class, are there other methods that will return a port that accept a URL parameter? Can you edit your post to paste the signature of that class?Defense
Seeing how the CXF team do things always makes me feel better about my own needlessly over-engineered code :)Patent
... aaaand I'm back here ... for the same GD* info. Unfortunately I can only give points once :)Patent
What kind of object is "endpoint" ? A String containing the URL? A special EndPoint (or some similar class) instance?Freetown
From another example endpoint looks to be a simple String containing the URL for the new endpoint.Dictionary
F
13

Working in cxf 2.6.1

Client client = ClientProxy.getClient(port);
client.getRequestContext().put(Message.ENDPOINT_ADDRESS, "http://some-valid-endpoint") ;
Freddie answered 15/6, 2012 at 10:34 Comment(1)
Thank you. You just saved me a day. This is the way to do it if you would like to use the generated factory class and change the end point address dynamically.Abject
D
1

This worked for me.

String customerEndPoint = "https://localhost:8080/customerService/v1"

customerWebService = service.getCustomerWebServicePort();

((BindingProvider) customerWebService).getRequestContext()
                        .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                                customerEndPoint);

Decease answered 7/2, 2020 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.