Saturday, October 29, 2011

RESTful Web Services: Jerseylicious... a Jersey based client

I thought that I would provide a simpler example than the RESTful Web Services book. In this example I am using Jersey (JAX-RS Reference Implementation) client to do the same thing as Apache HTTP Client.

You can download the Apache Maven NetBeans project here: jerseylicious.zip

JerseyliciousApp.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * Copyright 2011 Blue Lotus Software, LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * $Id:$
 */
package com.bluelotussoftware.jerseylicious;
 
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
 
/**
 * Example Jersey Application to read delicious bookmarks.
 * @author John Yeary
 * @version 1.0
 */
public class JerseyliciousApp {
 
    public static void main(String[] args) {
 
        if (args.length < 2) {
            System.out.println("Usage: JerseyliciousApp [username] [password]");
            return;
        }
 
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        client.addFilter(new HTTPBasicAuthFilter(args[0], args[1]));
        WebResource service = client.resource(getBaseURI());
 
        System.out.println(service.path("v1").path("posts").path("recent").accept(
                MediaType.TEXT_XML).get(String.class));
 
    }
 
    private static URI getBaseURI() {
        return UriBuilder.fromUri("https://api.del.icio.us").build();
    }
}

0 comments :

Popular Posts