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

uk.co.mruoc.rest.client.response.ResponseConverter Maven / Gradle / Ivy

package uk.co.mruoc.rest.client.response;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import uk.co.mruoc.rest.client.header.DefaultHeaders;
import uk.co.mruoc.rest.client.header.Headers;

import java.io.IOException;

import static uk.co.mruoc.rest.client.response.Response.*;

public class ResponseConverter {

    public Response toResponse(HttpResponse apacheResponse) throws IOException {
        return new ResponseBuilder()
                .setStatusCode(extractStatusCode(apacheResponse))
                .setBody(extractBody(apacheResponse))
                .setHeaders(extractHeaders(apacheResponse))
                .build();
    }

    private static int extractStatusCode(HttpResponse response) {
        return response.getStatusLine().getStatusCode();
    }

    private static String extractBody(HttpResponse response) throws IOException {
        if (response.getEntity() == null)
            return "";
        return EntityUtils.toString(response.getEntity());
    }

    private static Headers extractHeaders(HttpResponse response) {
        return new DefaultHeaders(response);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy