Codehaus XFire

Documentation

Quicklinks

Developers

Sponsors

This section outlines how to use the XFire proxy classes. This makes it easy to reuse your existing service classes and invoke your service. If you need to invoke someone else's service, check out the Client and Server Stub Generation from WSDL and Dynamic Client sections. If you are using Spring, please check out the information relating to clients in Spring Remoting.

First you need to create a Service like you do on your server. This provides the metadata about how the service is structured:

Service serviceModel = new ObjectServiceFactory().create(YourService.class);

Next, you'll need to create a proxy for your service:

YourService service = (YourService)
    new XFireProxyFactory().create(serviceModel, "http://your/remote/url");

And its as simple as that! Please note that YourService should be an interface and not a class that implements that interface.

Using non-default configurations

If you have a services.xml that specificies non-default fields you may need to supply those to the ObjectServiceFactory. For instance take this service definition:

<service>
  <serviceClass>YourService</serviceClass>
  <name>SuperService</name>
  <namespace>urn:your:namespace</namespace>
</service>

This would translate into:

Service serviceModel = new ObjectServiceFactory().create(YourService.class, "SuperService", "urn:your:namespace", null);
YourService client = (YourService)
    new XFireProxyFactory().create(serviceModel, "http://your/remote/url");

Or to take another example. Say you are using JSR 181 annotations:

Service serviceModel = new AnnotationServiceFactory().create(YourService.class);
YourService client = (YourService)
    new XFireProxyFactory().create(serviceModel, "http://your/remote/url");

Or try mixing annotations with JSR 181:

Service serviceModel =
  new AnnotationServiceFactory(
   new Jsr181WebAnnotations(),
   XFireFactory.newInstance().getXFire().getTransportManager(),
   new AegisBindingProvider(new JaxbTypeRegistry())).create(YourService.class);

Using Proxies with the local transport

One additional step is needed if you're using XFire's local transport. You need to pass in the XFire instance you're using so the server and the client both have access to the same LocalTransport.

XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);

YourService service = (YourService) factory.create(serviceModel, "xfire.local://YourService");

Often you'll want to do this in unit tests. If you are extending one of XFire's unit tests you can arrang it like so:

public class MyTest extends AbstractXFireAegisTest
{
  public void setUp() throws Exception
  {
     super.setUp();
     // register service...
  }

  public void testClient()
  {
    XFireProxyFactory factory = new XFireProxyFactory(getXFire());

    YourService service = (YourService) factory.create(getServiceRegistry().getService("YourService"),
                                                       "xfire.local://YourService");
  }
}

HTTP with basic authentication but without JSR-181

If you're using XFire with HTTP and you use basic authentication, the following snippet of code will allow you to access a SOAP service as a client (for example in a unit test).

Service serviceModel = new ObjectServiceFactory().create(SoapTestImpl.class);
SoapTest soapTest = (SoapTest) new XFireProxyFactory().create(serviceModel, "http://localhost:8181/soap/soap/SoapTestImpl");
Client client = Client.getInstance(soapTest);
client.setProperty(Channel.USERNAME, "test");
client.setProperty(Channel.PASSWORD, "test");

You should now be able to invoke methods on 'soapTest'.
Be sure to

import java.lang.reflect.Proxy

otherwise Proxy.getInvocationHandler won't work. 

HTTP with basic authentication and JSR-181

If you're using XFire with HTTP and you use basic authentication, the following snippet of code will allow you to access a SOAP service as a client (for example in a unit test). This piece of code uses JSR-181 style annotations.

Service serviceModel = new AnnotationServiceFactory(new Jsr181WebAnnotations(),
XFireFactory.newInstance().getXFire().getTransportManager()).create(SoapTestImpl.class);
SoapTest soapTest = (SoapTest) new XFireProxyFactory().create(serviceModel, "http://localhost:8181/soap/soap/SoapTestImpl");
Client client = Client.getInstance(m_soapTest)).getClient();
client.setProperty(Channel.USERNAME, "test");
client.setProperty(Channel.PASSWORD, "test");

You should now be able to invoke methods on 'soapTest'.

Configuring HTTP client properties

You can modify behaviour of  HTTP client by specifying  your own HttpClientParams object:

HttpClientParams params = new HttpClientParams();
// Configure client params here
Client client = Client.getInstance(m_soapTest);
client.setProperty(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS, params);

 
Some properties of Http client , for easier use, are accesible as Client properties :

client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "300");
client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_HOST, "MY_PROXY_ADDRESS");
client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PORT, "MY_PROXY_PORT");;

Note: for other properties of Client object check Transports section of User Guide