[demo] / insanejava / trunk / rest-demo / src / main / java / no / brodwall / insanejava / rest / RESTHttpClientImpl.java Repository:
ViewVC logotype

View of /insanejava/trunk/rest-demo/src/main/java/no/brodwall/insanejava/rest/RESTHttpClientImpl.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 161 - (download) (as text) (annotate)
Sun Mar 4 03:11:44 2007 UTC (3 years, 6 months ago) by johannes
File size: 3343 byte(s)
Updated for article
package no.brodwall.insanejava.rest;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;


public class RESTHttpClientImpl implements RESTHttpClient {

    public void doPut(URL url, WriterTemplate writerTemplate) {
        doHttpRequest(url, "PUT", writerTemplate, responseCodes());
    }

    public Reader doGet(URL url) {
        HttpURLConnection connection = doHttpRequest(url, "GET", null, responseCodes(HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_NOT_FOUND));
        if (connection == null) {
            return null;
        }
        try {
            return new InputStreamReader(connection.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public URL doPostAndReturnLocation(URL url, WriterTemplate writerTemplate) {
        HttpURLConnection connection = doHttpRequest(url, "POST", writerTemplate, responseCodes(HttpURLConnection.HTTP_CREATED));
        try {
            return new URL(connection.getHeaderField("Location"));
        } catch (MalformedURLException e) {
            throw new RuntimeException("Error from server problem with Location header", e);
        }
    }

    public void doDelete(URL url) {
        doHttpRequest(url, "DELETE", null, responseCodes(HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_NOT_FOUND));
    }

    private List<Integer> responseCodes(Integer... codes) {
        if (codes.length == 0) {
            codes = new Integer[] { HttpURLConnection.HTTP_OK };
        }
        return Arrays.asList(codes);
    }

    private HttpURLConnection doHttpRequest(URL requestUrl, String method, WriterTemplate content, Collection<Integer> responseCodes) {
        try {
            HttpURLConnection connection = (HttpURLConnection)requestUrl.openConnection();
            connection.setRequestMethod(method);
            if (content != null) {
                connection.setDoOutput(true);
                content.doWithWriter(new OutputStreamWriter(connection.getOutputStream()));
            }
            connection.connect();
            checkResponseCode(connection, responseCodes);
            if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                return null;
            }
            return connection;
        } catch (FileNotFoundException e) {
            if (responseCodes.contains(HttpURLConnection.HTTP_NOT_FOUND)) {
                return null;
            } else {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    private void checkResponseCode(HttpURLConnection connection, Collection<Integer> responseCodes) throws IOException {
        if (!responseCodes.contains(connection.getResponseCode())) {
            throw new RuntimeException("Error from server " + connection.getResponseCode() + " "
                                       + connection.getResponseMessage());
        }
    }

}

Johannes Brodwall
ViewVC Help
Powered by ViewVC 1.0.0