// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

public class Test {
    public static void testGet(String url, String accpt) throws HttpException, IOException {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(url);
        if(accpt != null) {
            get.addRequestHeader("Accept", accpt);
        }
        client.executeMethod(get);
        String res = get.getResponseBodyAsString();
        System.out.println(res);
    }
    public static void testGetDefault() throws HttpException, IOException {
        testGet("http://localhost:8080/demo/rest/person", null);
        testGet("http://localhost:8080/demo/rest/person/1", null);
        testGet("http://localhost:8080/demo/rest/person/10", null);
    }
    public static void testGetXml() throws HttpException, IOException {
        testGet("http://localhost:8080/demo/rest/person", "application/xml");
        testGet("http://localhost:8080/demo/rest/person/1", "application/xml");
        testGet("http://localhost:8080/demo/rest/person/10", "application/xml");
    }
    public static void testGetJson() throws HttpException, IOException {
        testGet("http://localhost:8080/demo/rest/person", "application/json");
        testGet("http://localhost:8080/demo/rest/person/1", "application/json");
        testGet("http://localhost:8080/demo/rest/person/10", "application/json");
    }
    public static void testGet() throws HttpException, IOException {
        testGetDefault();
        testGetXml();
        testGetJson();
    }
    public static void testPost(String url, String body, String conttyp) throws HttpException, IOException {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.setRequestEntity(new StringRequestEntity(body, conttyp, "UTF-8"));
        client.executeMethod(post);
    }
    public static void testPostXml() throws HttpException, IOException {
        testPost("http://localhost:8080/demo/rest/person", "<person><id>3</id><name>C C</name><address>C vej 3</address></person>", "application/xml");
    }
    public static void testPostJson() throws HttpException, IOException {
        testPost("http://localhost:8080/demo/rest/person", "{ \"id\": \"4\", \"name\": \"D D\", \"address\": \"D vej 4\" }", "application/json");
    }
    public static void testPost() throws HttpException, IOException {
        testPostXml();
        testPostJson();
    }
    public static void main(String[] args) throws Exception {
        testGet();
        testPost();
        testGet();
    }
}
