Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

  • Redirect

Handler Configuration

TBDThe following example demonstrates how to register a custom handler.

Code Block
xml
xml
1  ClientConfig config = new ClientConfig();
// Create new JAX-RS Application
2  config.handlers(new DummyHandler());
// create the rest client instance
3  RestClient client = new RestClient(config);
// create the resource instance to interact with
4  Resource resource = client.resource("http://services.com/HelloWorld");
// perform a GET on the resource
// the resource will be returned as plain text
5  String response = resource.accept("text/plain").get(String.class);

Explanation

This example demonstrates how to register a custom handler. First, a new instance of a ClientConfig is created as it appears in line 1. Then the new handler is added to the handlers chain by invoking the handlers() method on the ClientConfig instance as it appears in line 2. Finally, a new instance of a RestClient is created with this configuration as it appears in line 3.

org.apache.wink.client.handlers.BasicAuthSecurityHandler

You can configure a simple BasicAuthSecurityHandler to use basic authentication when accessing resources from the client.

Code Block
xml
xml

 ClientConfig config = new ClientConfig();<br/>
 BasicAuthSecurityHandler basicAuthHandler = new BasicAuthSecurityHandler();
 basicAuthHandler.setUserName("foo");
 basicAuthHandler.setPassword("bar");
 config.handlers(basicAuthHandler);<br/>
 // create the rest client instance<br/>
 RestClient client = new RestClient(config);<br/>
 // create the resource instance to interact with Resource<br/>
 resource = client.resource("http://localhost:8080/path/to/resource");<br/>

Custom Provider Configuration

Explanation

The BasicAuthSecurityHandler will attempt to access the resource without security information first. If the request is successful, then the BasicAuthSecurityHandler is like a no-op. However, if it receives a 401, then the username and password will be added to the request, and the request will be sent again.

Gzip Input Stream Adapter

The following code snippet is an example of an implementation of a Gzip input stream adapter.

Code Block
xml
xml
class GzipInputAdapter implements InputStreamAdapter{
        public InputStream adapt(InputStream is,
                                 ClientResponse response) {
      String header = response.getHeaders().getFirst("Content-Encoding");
      if (header != null && header.equalsIgnoreCase("gzip")) {
        return new GZIPInputStream(is);
      }
      return is;
   }}

Explanation

The Gzip input stream adapter is responsible for wrapping the input stream with the Gzip input stream.

Gzip Output Stream Adapter

The following code snippet is an example of an implementation of a Gzip output stream adapter.

Code Block
xml
xml
class GzipOutputAdapter implements OutputStreamAdapter {
    public OutputStream adapt(OutputStream os,
                              ClientRequest request) {
        request.getHeaders().add("Content-Encoding", "gzip");
        return new GZIPOutputStream(os);
    }}

Explanation

The Gzip output stream adapter is responsible for wrapping the output stream with the Gzip output stream.

Custom Provider Configuration

The following example demonstrates how to register a custom entity provider.TBD

Code Block
xml
xml
1  ClientConfig config = new ClientConfig();
   // Create new JAX-RS Application
2    Application app = new Application() {
     @Override
      public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>();
        set.add(FooProvider.class);
        return set;}};
3  conf.applications(app);
// create the rest client instance
4  RestClient client = new RestClient(config);
// create the resource instance to interact with
5  Resource resource = client.resource("http://services.com/HelloWorld");
// perform a GET on the resource. the resource will be returned as plain text
6  String response = resource.accept("text/plain").get(String.class);

Explanation

This example demonstrates how to register a custom entity provider. First, a new instance of ClientConfig is created as it appears in line 1. Then a new anonymous Application is instantiated and set on the ClientConfig as it appears in line 2 and 3. Finally, a new instance of a RestClient is created with this configuration as it appears in line 4.