All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.co.mruoc.rest.client.AbstractSimpleRestClient Maven / Gradle / Ivy

package uk.co.mruoc.rest.client;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import uk.co.mruoc.rest.client.header.Headers;
import uk.co.mruoc.rest.client.response.Response;
import uk.co.mruoc.rest.client.response.ResponseConverter;

import java.io.IOException;
import java.io.UncheckedIOException;


public abstract class AbstractSimpleRestClient extends BaseRestClient {

    private final HttpClient client;
    private final ResponseConverter converter = new ResponseConverter();

    public AbstractSimpleRestClient(HttpClient client) {
        this.client = client;
    }

    @Override
    public Response post(String endpoint, String entity, Headers headers) {
        HttpPost post = createPost(endpoint, entity, headers);
        return execute(post);
    }

    @Override
    public Response put(String endpoint, String entity, Headers headers) {
        HttpPut put = createPut(endpoint, entity, headers);
        return execute(put);
    }

    @Override
    public Response patch(String endpoint, String entity, Headers headers) {
        HttpPatch patch = createPatch(endpoint, entity, headers);
        return execute(patch);
    }

    @Override
    public Response get(String endpoint, Headers headers) {
        HttpGet get = createGet(endpoint, headers);
        return execute(get);
    }

    @Override
    public Response delete(String endpoint, Headers headers) {
        HttpDelete delete = createDelete(endpoint, headers);
        return execute(delete);
    }

    protected Response execute(HttpRequestBase request) {
        try {
            log(request);
            HttpResponse rawResponse = client.execute(request);
            Response response = converter.toResponse(rawResponse);
            log(response);
            return response;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } finally {
            request.releaseConnection();
        }
    }

    protected abstract void log(HttpRequestBase request);

    protected abstract void log(Response response);

    private HttpPost createPost(String endpoint, String entity, Headers headers) {
        HttpPost post = new HttpPost(endpoint);
        setEntity(post, entity);
        addHeaders(post, headers);
        return post;
    }

    private HttpPut createPut(String endpoint, String entity, Headers headers) {
        HttpPut put = new HttpPut(endpoint);
        setEntity(put, entity);
        addHeaders(put, headers);
        return put;
    }

    private HttpPatch createPatch(String endpoint, String entity, Headers headers) {
        HttpPatch patch = new HttpPatch(endpoint);
        setEntity(patch, entity);
        addHeaders(patch, headers);
        return patch;
    }

    private HttpGet createGet(String endpoint, Headers headers) {
        HttpGet get = new HttpGet(endpoint);
        addHeaders(get, headers);
        return get;
    }

    private HttpDelete createDelete(String endpoint, Headers headers) {
        HttpDelete delete = new HttpDelete(endpoint);
        addHeaders(delete, headers);
        return delete;
    }

    private void setEntity(HttpEntityEnclosingRequestBase request, String entity) {
        request.setEntity(toEntity(request, entity));
    }

    private void addHeaders(HttpRequest request, Headers headers) {
        for (String name : headers.getNames())
            request.setHeader(name, headers.get(name));
    }

    private HttpEntity toEntity(HttpEntityEnclosingRequestBase request, String entity) {
        return new StringEntity(entity, ContentTypeExtractor.extract(request));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy