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

import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class ClientManual {
    private static void testGetOne(String urlprefix, int f1, String acctyp) throws IOException {
        HttpGet request = new HttpGet(urlprefix + "/t1/" + f1);
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        System.out.println(response.getStatusLine().getStatusCode() / 100 == 2);
        String contyp = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        System.out.println(Util.String2Object(T1.class, respbody, contyp));
    }
    private static void testGetAll(String urlprefix, String acctyp) throws IOException {
        HttpGet request = new HttpGet(urlprefix + "/t1");
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        System.out.println(response.getStatusLine().getStatusCode() / 100 == 2);
        String contyp = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        T1[] a;
        if(contyp.equals("application/xml")) {
             a = Util.String2Object(T1Array.class, respbody, contyp).getArray();
        } else {
            a = Util.String2Object(T1[].class, respbody, contyp);
        }
        for(T1 o : a) System.out.print(o);
        System.out.println();
    }
    private static void testGetSome(String urlprefix, int start, int finish, String acctyp) throws IOException {
        HttpGet request = new HttpGet(urlprefix + "/t1?start=" + start + "&finish=" + finish);
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        System.out.println(response.getStatusLine().getStatusCode() / 100 == 2);
        String contyp = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        T1[] a;
        if(contyp.equals("application/xml")) {
             a = Util.String2Object(T1Array.class, respbody, contyp).getArray();
        } else {
            a = Util.String2Object(T1[].class, respbody, contyp);
        }
        for(T1 o : a) System.out.print(o);
        System.out.println();
    }
    private static void testPost(String urlprefix, String contyp, T1 o, String acctyp) throws IOException {
        String reqbody = Util.Object2String(o, contyp);
        HttpPost request = new HttpPost(urlprefix + "/t1");
        request.setHeader("Content-Type", contyp);
        request.setEntity(new StringEntity(reqbody));
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        System.out.println(response.getStatusLine().getStatusCode() / 100 == 2);
        String contyp2 = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        System.out.println(Util.String2Object(T1.class, respbody, contyp2));
    }
    private static void testPut(String urlprefix, String contyp, T1 o) throws IOException {
        String reqbody = Util.Object2String(o, contyp);
        HttpPut request = new HttpPut(urlprefix + "/t1/" + o.getF1());
        request.setHeader("Content-Type", contyp);
        request.setEntity(new StringEntity(reqbody));
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        System.out.println(response.getStatusLine().getStatusCode() / 100 == 2);
    }
    private static void testDelete(String urlprefix, int f1) throws IOException {
        HttpDelete request = new HttpDelete(urlprefix + "/t1/" + f1);   
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        System.out.println(response.getStatusLine().getStatusCode() / 100 == 2);
    }
    private static void test(String urlprefix, String typ) throws IOException {
        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) throws IOException {
        test(urlprefix, "application/xml");
        test(urlprefix, "application/json");
    }
    public static void main(String[] args) throws IOException {
        Logger.getRootLogger().setLevel(Level.OFF);
        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");
    }
}
