Search in sources :

Example 6 with ClientMetrics

use of org.commonjava.indy.client.core.metric.ClientMetrics in project indy by Commonjava.

the class IndyClientHttp method get.

public <T> T get(final String path, final TypeReference<T> typeRef) throws IndyClientException {
    HttpGet request = newJsonGet(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(request);
    connect();
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        addLoggingMDCToHeaders(request);
        response = client.execute(request, newContext());
        final StatusLine sl = response.getStatusLine();
        if (sl.getStatusCode() != 200) {
            if (sl.getStatusCode() == 404) {
                return null;
            }
            metrics.registerErr(sl);
            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) {
        metrics.registerErr(e);
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        metrics.registerEnd(response);
        cleanupResources(request, response, client, metrics);
    }
}
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) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics)

Example 7 with ClientMetrics

use of org.commonjava.indy.client.core.metric.ClientMetrics 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 {
    HttpPost post = newJsonPost(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(post);
    checkRequestValue(value);
    connect();
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        addLoggingMDCToHeaders(post);
        post.setEntity(new StringEntity(objectMapper.writeValueAsString(value)));
        response = client.execute(post, newContext());
        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            metrics.registerErr(sl);
            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) {
        metrics.registerErr(e);
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        metrics.registerEnd(response);
        cleanupResources(post, response, client, metrics);
    }
}
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) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics)

Example 8 with ClientMetrics

use of org.commonjava.indy.client.core.metric.ClientMetrics in project indy by Commonjava.

the class IndyClientHttp method getRaw.

public HttpResources getRaw(final String path, final Map<String, String> headers) throws IndyClientException {
    final HttpGet req = newRawGet(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(req);
    connect();
    CloseableHttpResponse response = null;
    try {
        addLoggingMDCToHeaders(req);
        if (headers != null) {
            headers.forEach(req::setHeader);
        }
        final CloseableHttpClient client = newClient();
        response = client.execute(req, newContext());
        return new HttpResources(req, response, client, metrics);
    } catch (final IOException e) {
        metrics.registerErr(e);
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        metrics.registerEnd(response);
    // metrics.close();
    // DO NOT CLOSE!!!! We're handing off control of the response to the caller!
    // closeQuietly( response );
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics)

Example 9 with ClientMetrics

use of org.commonjava.indy.client.core.metric.ClientMetrics in project indy by Commonjava.

the class IndyClientHttp method head.

public Map<String, String> head(final String path, final int... responseCodes) throws IndyClientException {
    HttpHead request = newJsonHead(buildUrl(baseUrl, path));
    connect();
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    ClientMetrics metrics = metricManager.register(request);
    try {
        addLoggingMDCToHeaders(request);
        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;
            }
            metrics.registerErr(sl);
            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) {
        metrics.registerErr(e);
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        metrics.registerEnd(response);
        cleanupResources(request, response, client, metrics);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) 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) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics) HttpHead(org.apache.http.client.methods.HttpHead)

Example 10 with ClientMetrics

use of org.commonjava.indy.client.core.metric.ClientMetrics in project indy by Commonjava.

the class IndyClientHttp method execute.

public HttpResources execute(HttpRequestBase request) throws IndyClientException {
    ClientMetrics metrics = metricManager.register(request);
    connect();
    addLoggingMDCToHeaders(request);
    CloseableHttpResponse response = null;
    try {
        final CloseableHttpClient client = newClient();
        response = client.execute(request, newContext());
        return new HttpResources(request, response, client, metrics);
    } catch (final IOException e) {
        metrics.registerErr(e);
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        metrics.registerEnd(response);
    // DO NOT CLOSE!!!! We're handing off control of the response to the caller!
    // closeQuietly( response );
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics)

Aggregations

IOException (java.io.IOException)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)14 ClientMetrics (org.commonjava.indy.client.core.metric.ClientMetrics)14 StatusLine (org.apache.http.StatusLine)10 HttpResources.entityToString (org.commonjava.indy.client.core.helper.HttpResources.entityToString)6 StringEntity (org.apache.http.entity.StringEntity)4 HttpResources (org.commonjava.indy.client.core.helper.HttpResources)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 HttpDelete (org.apache.http.client.methods.HttpDelete)2 HttpHead (org.apache.http.client.methods.HttpHead)2 HttpPut (org.apache.http.client.methods.HttpPut)2 HashMap (java.util.HashMap)1 Header (org.apache.http.Header)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1 BasicHeader (org.apache.http.message.BasicHeader)1