// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class ClientJAXRS {
    private static void testGetOne(String urlprefix, int f1, String acctyp) {
        Client client = ClientBuilder.newClient();
        Response response = client.target(urlprefix).path("/t1/" + f1).request(acctyp).get();
        System.out.println(response.getStatus() / 100 == 2);
        System.out.println(response.readEntity(T1.class));
    }
    private static void testGetAll(String urlprefix, String acctyp) {
        Client client = ClientBuilder.newClient();
        Response response = client.target(urlprefix).path("/t1").request(acctyp).get();
        System.out.println(response.getStatus() / 100 == 2);
        T1[] a;
        if(acctyp.equals(MediaType.APPLICATION_XML)) {
            a = response.readEntity(T1Array.class).getArray();
        } else {
            a = response.readEntity(T1[].class);
        }
        for(T1 o : a) System.out.print(o);
        System.out.println();
    }
    private static void testGetSome(String urlprefix, int start, int finish, String acctyp) {
        Client client = ClientBuilder.newClient();
        Response response = client.target(urlprefix).path("/t1").queryParam("start", start).queryParam("finish", finish).request(acctyp).get();
        System.out.println(response.getStatus() / 100 == 2);
        T1[] a;
        if(acctyp.equals(MediaType.APPLICATION_XML)) {
            a = response.readEntity(T1Array.class).getArray();
        } else {
            a = response.readEntity(T1[].class);
        }
        for(T1 o : a) System.out.print(o);
        System.out.println();
    }
    private static void testPost(String urlprefix, String contyp, T1 o, String acctyp) {
        Client client = ClientBuilder.newClient();
        Response response = client.target(urlprefix).path("/t1").request(acctyp).post(Entity.entity(o, contyp));
        System.out.println(response.getStatus() / 100 == 2);
        System.out.println(response.readEntity(T1.class));
    }
    private static void testPut(String urlprefix, String contyp, T1 o) {
        Client client = ClientBuilder.newClient();
        Response response = client.target(urlprefix).path("/t1/" + o.getF1()).request().put(Entity.entity(o, contyp));
        System.out.println(response.getStatus() / 100 == 2);
    }
    private static void testDelete(String urlprefix, int f1) {
        Client client = ClientBuilder.newClient();
        Response response = client.target(urlprefix).path("/t1/" + f1).request().delete();
        System.out.println(response.getStatus() / 100 == 2);
    }
    private static void test(String urlprefix, String typ) {
        System.out.println("**** " + urlprefix + " (" + typ + ") ****");
        testGetOne(urlprefix, 123, typ);
        testGetAll(urlprefix, typ);
        testGetSome(urlprefix, 10, 12, typ);
        testPost(urlprefix, typ, new T1(123, "ABC"), typ);
        testPut(urlprefix, typ, new T1(123, "ABC"));
        testDelete(urlprefix, 123);
    }
    private static void test(String urlprefix) {
        test(urlprefix, MediaType.APPLICATION_XML);
        test(urlprefix, MediaType.APPLICATION_JSON);
    }
    public static void main(String[] args) {
        test("http://localhost:8080/wsrest/testapi1");
        test("http://localhost:8080/wsrest/testapi2");
        test("http://localhost:8080/wsrest/testapi3");
        test("http://localhost/testapi.ashx");
        test("http://localhost/testapi.svc");
        test("http://localhost/testapi");
        test("http://localhost:81/testapi.php");
    }
}
