MyClient.java 3.52 KB
Newer Older
Michele Fiori's avatar
Michele Fiori committed
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
package client;

import beans.Word;
import com.google.gson.Gson;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class MyClient {
    public static void main(String[] argv){
        Client client = Client.create();
        String serverAddress = "http://localhost:1337";
        ClientResponse clientResponse = null;

        //POST
        String postPath = "/dictionary/add";
        Word word = new Word("computer","an electronic machine that can store and arrange large amounts of information");
        clientResponse = postRequest(client,serverAddress+postPath,word);
        System.out.println(clientResponse.toString());

        //GET #1
        String getPath = "/dictionary/get/computer";
        clientResponse = getRequest(client,serverAddress+getPath);
        System.out.println(clientResponse.toString());
        String response = clientResponse.getEntity(String.class);
        System.out.println(response);

        //PUT
        String putPath = "/dictionary/modify";
        word = new Word("computer","little box with many wires and a huge number of bright lights");
        clientResponse = putRequest(client, serverAddress+putPath, word);
        System.out.println(clientResponse.toString());

        //GET #2
        clientResponse = getRequest(client,serverAddress+getPath);
        System.out.println(clientResponse.toString());
        response = clientResponse.getEntity(String.class);
        System.out.println(response);

        //DELETE #1
        String deletePath = "/dictionary/delete/computer";
        clientResponse = deleteRequest(client, serverAddress+deletePath);
        System.out.println(clientResponse);

        //GET #3
        clientResponse = getRequest(client,serverAddress+getPath);
        System.out.println(clientResponse.toString());
        response = clientResponse.getEntity(String.class);
        System.out.println(response);

    }

    public static ClientResponse postRequest(Client client, String url, Word w){
        WebResource webResource = client.resource(url);
        String input = new Gson().toJson(w);
        try {
            return webResource.type("application/json").post(ClientResponse.class, input);
        } catch (ClientHandlerException e) {
            System.out.println("Server not available");
            return null;
        }
    }

    public static ClientResponse getRequest(Client client, String url){
        WebResource webResource = client.resource(url);
        try {
            return webResource.get(ClientResponse.class);
        } catch (ClientHandlerException e) {
            System.out.println("Server not available");
            return null;
        }
    }

    public static ClientResponse putRequest(Client client, String url, Word word){
        WebResource webResource = client.resource(url);
        String input = new Gson().toJson(word);
        try {
            return webResource.type("application/json").put(ClientResponse.class, input);
        } catch (ClientHandlerException e) {
            System.out.println("Server not available");
            return null;
        }
    }

    public static ClientResponse deleteRequest(Client client, String url){
        WebResource webResource = client.resource(url);
        try{
            return webResource.delete(ClientResponse.class);
        }catch(ClientHandlerException e){
            System.out.println("Server not available");
            return null;
        }
    }
}