Skip to content
Ryan Heaton edited this page Sep 17, 2015 · 2 revisions

Using the Generated Client-Side Services

This is a quick guide to using the generated client-side services.

Getting an instance of the client-side service.

The generated client-side services are nothing more than simple interfaces with an associated implementation class. The implementation classes are found in the "impl" subpackage of the package where the interfaces are found. Each implementation can be instantiated by choosing the appropriate constructor. The default constructor will make all service requests to the default URL of the SOAP endpoint. You can use the other constructor if you want to point service requests to a different URL.

Separating the interfaces from the implementation classes allows for maximum flexibility in obtaining instances of the service. Each service lends itself nicely to dependency injection and AOP interceptors.

For example, suppose a service interface com.mycompany.MyService:

To instantiate the service directly:

com.mycompany.MyService service = new com.mycompany.impl.MyServiceImpl();

To instantiate a service that points to a different URL:

com.mycompany.MyService service = new com.mycompany.impl.MyServiceImpl("http://localhost:8080/soap/MyService");
Configuring the underlying HTTP requests

You can configure static HTTP headers directly using the setHttpHeaders method on the implementation class.

com.mycompany.impl.MyServiceImpl serviceImpl = new com.mycompany.impl.MyServiceImpl();
Map<String, String> httpHeaders = new HashMap<String, String>();
httpHeaders.put("XMYCUSTOMHEADER1", "My custom header value 1");
httpHeaders.put("XMYCUSTOMHEADER2", "My custom header value 2");
serviceImpl.setHttpHeaders(httpHeaders);

You can set the username/password for HTTP basic authentication by using the setHttpAuthCredentials method on the implementation class.

com.mycompany.impl.MyServiceImpl serviceImpl = new com.mycompany.impl.MyServiceImpl();
serviceImpl.setHttpAuthCredentials("myusername", "mypassword");

Enunciate uses the Apache Commons HTTPClient API to handle the underlying HTTP requests and responses. Cookies are enabled by default. You can maintain your own http state (for handling cookies, authentication credentials, and proxy information) by setting the HttpState object on the implementation class.

com.mycompany.impl.MyServiceImpl serviceImpl = new com.mycompany.impl.MyServiceImpl();
org.apache.commons.httpclient.HttpState state = new org.apache.commons.httpclient.HttpState();
org.apache.commons.httpclient.Cookie cookie = new org.apache.commons.httpclient.Cookie("mydomain", "mycookiename", "mycookievalue");

state.addCookie(cookie);
serviceImpl.setHttpState(state);

And, for more advanced, low-level handling of requests, you can create your own instance of org.codehaus.enunciate.modules.xfire_client.RequestHandler that will be used to configure the request before the request is sent and the response after the response is received.

//handler that implements org.codehaus.enunciate.modules.xfire_client.RequestHandler
com.othercompany.MyRequestHandler requestHandler = new com.othercompany.MyRequestHandler();
com.mycompany.impl.MyServiceImpl serviceImpl = new com.mycompany.impl.MyServiceImpl();
serviceImpl.setRequestHandler(requestHandler);
Clone this wiki locally