Search in sources :

Example 81 with StatusLine

use of org.apache.http.StatusLine in project indy by Commonjava.

the class IndyClientHttp method exists.

public boolean exists(final String path, Supplier<Map<String, String>> querySupplier, final int... responseCodes) throws IndyClientException {
    connect();
    HttpHead request = null;
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        request = newJsonHead(buildUrl(baseUrl, querySupplier, path));
        response = client.execute(request, newContext());
        final StatusLine sl = response.getStatusLine();
        if (validResponseCode(sl.getStatusCode(), responseCodes)) {
            return true;
        } else if (sl.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            return false;
        }
        throw new IndyClientException(sl.getStatusCode(), "Error checking existence of: %s.\n%s", path, new IndyResponseErrorDetails(response));
    } catch (final IOException e) {
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        cleanupResources(request, response, client);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead)

Example 82 with StatusLine

use of org.apache.http.StatusLine in project indy by Commonjava.

the class IndyClientHttp method head.

public Map<String, String> head(final String path, final int... responseCodes) throws IndyClientException {
    connect();
    HttpHead request = null;
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        request = newJsonHead(buildUrl(baseUrl, path));
        client = newClient();
        response = client.execute(request, newContext());
        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            if (sl.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                return null;
            }
            throw new IndyClientException(sl.getStatusCode(), "Error executing HEAD: %s. Status was: %d %s (%s)", path, sl.getStatusCode(), sl.getReasonPhrase(), sl.getProtocolVersion());
        }
        final Map<String, String> headers = new HashMap<>();
        for (final Header header : response.getAllHeaders()) {
            final String name = header.getName().toLowerCase();
            if (!headers.containsKey(name)) {
                headers.put(name, header.getValue());
            }
        }
        return headers;
    } catch (final IOException e) {
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        cleanupResources(request, response, client);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Header(org.apache.http.Header) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResources.entityToString(org.commonjava.indy.client.core.helper.HttpResources.entityToString) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead)

Example 83 with StatusLine

use of org.apache.http.StatusLine in project indy by Commonjava.

the class IndyClientHttp method postWithResponse.

public <T> T postWithResponse(final String path, final Object value, final TypeReference<T> typeRef, final int... responseCodes) throws IndyClientException {
    checkRequestValue(value);
    connect();
    HttpPost post = null;
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        post = newJsonPost(buildUrl(baseUrl, path));
        post.setEntity(new StringEntity(objectMapper.writeValueAsString(value)));
        response = client.execute(post, newContext());
        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            throw new IndyClientException(sl.getStatusCode(), "Error retrieving %s from: %s.\n%s", typeRef.getType(), path, new IndyResponseErrorDetails(response));
        }
        final String json = entityToString(response);
        return objectMapper.readValue(json, typeRef);
    } catch (final IOException e) {
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        cleanupResources(post, response, client);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResources.entityToString(org.commonjava.indy.client.core.helper.HttpResources.entityToString) IOException(java.io.IOException)

Example 84 with StatusLine

use of org.apache.http.StatusLine in project indy by Commonjava.

the class IndyClientHttp method get.

public <T> T get(final String path, final TypeReference<T> typeRef) throws IndyClientException {
    connect();
    HttpGet request = null;
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        request = newJsonGet(buildUrl(baseUrl, path));
        response = client.execute(request, newContext());
        final StatusLine sl = response.getStatusLine();
        if (sl.getStatusCode() != 200) {
            if (sl.getStatusCode() == 404) {
                return null;
            }
            throw new IndyClientException(sl.getStatusCode(), "Error retrieving %s from: %s.\n%s", typeRef.getType(), path, new IndyResponseErrorDetails(response));
        }
        final String json = entityToString(response);
        final T value = objectMapper.readValue(json, typeRef);
        return value;
    } catch (final IOException e) {
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        cleanupResources(request, response, client);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResources.entityToString(org.commonjava.indy.client.core.helper.HttpResources.entityToString) IOException(java.io.IOException)

Example 85 with StatusLine

use of org.apache.http.StatusLine in project indy by Commonjava.

the class IndyFoloAdminClientModule method sealTrackingRecord.

public boolean sealTrackingRecord(String trackingId) throws IndyClientException {
    http.connect();
    HttpPost request = http.newRawPost(UrlUtils.buildUrl(http.getBaseUrl(), "/folo/admin", trackingId, "record"));
    HttpResources resources = null;
    try {
        resources = http.execute(request);
        HttpResponse response = resources.getResponse();
        StatusLine sl = response.getStatusLine();
        if (sl.getStatusCode() != 200) {
            if (sl.getStatusCode() == 404) {
                return false;
            }
            throw new IndyClientException(sl.getStatusCode(), "Error sealing tracking record %s.\n%s", trackingId, new IndyResponseErrorDetails(response));
        }
        return true;
    } finally {
        IOUtils.closeQuietly(resources);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpResponse(org.apache.http.HttpResponse) IndyClientException(org.commonjava.indy.client.core.IndyClientException) IndyResponseErrorDetails(org.commonjava.indy.client.core.IndyResponseErrorDetails)

Aggregations

StatusLine (org.apache.http.StatusLine)193 IOException (java.io.IOException)83 HttpResponse (org.apache.http.HttpResponse)76 HttpEntity (org.apache.http.HttpEntity)61 BasicStatusLine (org.apache.http.message.BasicStatusLine)46 ProtocolVersion (org.apache.http.ProtocolVersion)42 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)40 HttpGet (org.apache.http.client.methods.HttpGet)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)37 Header (org.apache.http.Header)36 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)34 Test (org.junit.Test)31 HttpPost (org.apache.http.client.methods.HttpPost)26 HashMap (java.util.HashMap)23 HttpResponseException (org.apache.http.client.HttpResponseException)23 StringEntity (org.apache.http.entity.StringEntity)23 URL (java.net.URL)20 BasicHeader (org.apache.http.message.BasicHeader)16 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15