Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Apache Wink Client is an easy-to-use, providing a high level Java API for writing clients that consume HTTP-based RESTful Web Services. It The Apache Wink Client utilizes JAX-RS concepts, encapsulates Rest standards and protocols and maps Rest principles concepts to Java classes, which facilitates the development of clients for any HTTP-based Rest Web Services.

...

GET Request

Code Block
xml
xml

// create the rest client instance
1  RestClient client = new RestClient();

// create the resource instance to interact with
2  Resource resource = client.resource("http://services.com/HelloWorld");
// perform a GET on the resource. The resource will be returned as plain text
3  String response = resource.accept("text/plain").get(String.class);

...

POST Request

Code Block
xml
xml

// create the rest client instance
1  RestClient client = new RestClient();

// create the resource instance to interact with
2  Resource resource = client.resource("http://services.co");

// issue the request
3  String response = resource.contentType("text/plain").accept("text/plain").post(String.class, "foo");

...

POST Atom Request

Code Block
xml
xml

// create the rest client instance
1  RestClient client = new RestClient();

// create the resource instance to interact with
2  Resource resource = client.resource("http://services.co");

3  AtomEntry request = getAtomEntry();

// issue the request
4  AtomEntry response = resource.contentType("application/atom+xml").accept("application/atom+xml").post(AtomEntry.class, request);

...

Using ClientResponse

Code Block
xml
xml

// create the rest client instance
1  RestClient client = new RestClient();

// create the resource instance to interact with
2  Resource resource = client.resource("http://services.co");

// issue the request
3  ClientResponse response = resource.accept("text/plain").get();

// deserialize response
4  String responseAsString = response.getEntity(String.class);

...

Handler Configuration

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);

...

Custom Provider Configuration

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);

...

A typical implementation of the "handle" method appears as follows:

Code Block
xml
xml

public class MyHandler implements ClientHandler {

 public ClientResponse handle(ClientRequest rqs, HandlerContext ctx) {

   // Do something before request is issued to the web resource
1    rqs.getHeaders().add("CUSTOM-REQUEST-HEADER", "Foo-Request");

2    ClientResponse resp = ctx.doChain(request);

   // Do something before response is returned to the client
3    resp.getHeaders().add("CUSTOM-RESPONSE-HEADER", "Foo-Response");

4    return resp;

 }
}

...

Gzip Handler

Code Block
xml
xml

public class GzipHandler implements ClientHandler {
   public ClientResponse handle(ClientRequest request,
                                HandlerContext context) {
        request.getHeaders().add("Accept-Encoding", "gzip");
        context.addInputStreamAdapter(new GzipInputAdapter());
        context.addOutputStreamAdapter(new GzipOutputAdapter());
        return context.doChain(request);
  }}

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;
   }}

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);
    }}

...