Search in sources :

Example 1 with RestfulieException

use of br.com.caelum.restfulie.RestfulieException in project restfulie-java by caelum.

the class ApacheDispatcher method process.

public Response process(Request details, String method, URI uri, final Object payload) {
    if (payload == null) {
        return access(details, method, uri);
    }
    final Map<String, String> headers = details.getHeaders();
    if (!headers.containsKey("Content-type")) {
        throw new RestfulieException("You should set a content type prior to sending some payload.");
    }
    StringWriter writer = new StringWriter();
    String type = headers.get("Content-type");
    try {
        type = type.split(";")[0];
        handlerFor(type).marshal(payload, writer, client);
        writer.flush();
        HttpEntityEnclosingRequestBase verb = (HttpEntityEnclosingRequestBase) verbFor(method, uri);
        add(verb, headers);
        String string = writer.getBuffer().toString();
        verb.setEntity(new StringEntity(string));
        return execute(details, verb);
    } catch (IOException e) {
        throw new RestfulieException("Unable to marshal entity.", e);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) StringWriter(java.io.StringWriter) RestfulieException(br.com.caelum.restfulie.RestfulieException) IOException(java.io.IOException)

Example 2 with RestfulieException

use of br.com.caelum.restfulie.RestfulieException in project restfulie-java by caelum.

the class JavaNetDispatcher method access.

private Response access(Request request, String verb, URI uri) {
    try {
        HttpURLConnection connection = prepareConnectionWith(request.getHeaders(), uri);
        connection.setDoOutput(false);
        connection.setRequestMethod(verb);
        JavaNetResponse response = responseFor(connection, new HttpURLConnectionContentProcessor(connection), request);
        return response;
    } catch (IOException e) {
        throw new RestfulieException("Unable to execute " + uri, e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RestfulieException(br.com.caelum.restfulie.RestfulieException) HttpURLConnectionContentProcessor(br.com.caelum.restfulie.http.HttpURLConnectionContentProcessor) IOException(java.io.IOException)

Example 3 with RestfulieException

use of br.com.caelum.restfulie.RestfulieException in project restfulie-java by caelum.

the class JavaNetDispatcher method process.

public Response process(Request details, String verb, URI uri, Object payload) {
    if (payload == null) {
        return access(details, verb, uri);
    }
    try {
        Map<String, String> headers = details.getHeaders();
        HttpURLConnection connection = prepareConnectionWith(headers, uri);
        if (!headers.containsKey("Content-type")) {
            throw new RestfulieException("You should set a content type prior to sending some payload.");
        }
        connection.setDoOutput(true);
        connection.setRequestMethod(verb);
        OutputStream output = connection.getOutputStream();
        Writer writer = new OutputStreamWriter(output);
        String type = headers.get("Content-type");
        handlerFor(type).marshal(payload, writer, client);
        writer.flush();
        return responseFor(connection, new IdentityContentProcessor(), details);
    } catch (IOException e) {
        throw new RestfulieException("Unable to execute " + uri, e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RestfulieException(br.com.caelum.restfulie.RestfulieException) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IdentityContentProcessor(br.com.caelum.restfulie.http.IdentityContentProcessor)

Aggregations

RestfulieException (br.com.caelum.restfulie.RestfulieException)3 IOException (java.io.IOException)3 HttpURLConnection (java.net.HttpURLConnection)2 HttpURLConnectionContentProcessor (br.com.caelum.restfulie.http.HttpURLConnectionContentProcessor)1 IdentityContentProcessor (br.com.caelum.restfulie.http.IdentityContentProcessor)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)1 StringEntity (org.apache.http.entity.StringEntity)1