Search in sources :

Example 11 with ClientMetrics

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

the class IndyClientHttp method postRaw.

public HttpResources postRaw(final String path, Object value, final Map<String, String> headers) throws IndyClientException {
    final HttpPost req = newRawPost(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(req);
    checkRequestValue(value);
    connect();
    CloseableHttpResponse response = null;
    try {
        addLoggingMDCToHeaders(req);
        if (headers != null) {
            for (String key : headers.keySet()) {
                req.setHeader(key, headers.get(key));
            }
        }
        req.setEntity(new StringEntity(objectMapper.writeValueAsString(value)));
        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);
    // DO NOT CLOSE!!!! We're handing off control of the response to the caller!
    // closeQuietly( response );
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) 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 12 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 Class<T> type, 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 POSTING with %s result from: %s.\n%s", type.getSimpleName(), path, new IndyResponseErrorDetails(response));
        }
        final String json = entityToString(response);
        return objectMapper.readValue(json, type);
    } 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 13 with ClientMetrics

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

the class IndyClientHttp method put.

public boolean put(final String path, final Object value, final int... responseCodes) throws IndyClientException {
    HttpPut put = newJsonPut(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(put);
    checkRequestValue(value);
    connect();
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        addLoggingMDCToHeaders(put);
        put.setEntity(new StringEntity(objectMapper.writeValueAsString(value)));
        response = client.execute(put, newContext());
        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            metrics.registerErr(sl);
            throw new IndyClientException(sl.getStatusCode(), "Error in response from: %s.\n%s", path, new IndyResponseErrorDetails(response));
        }
    } catch (final IOException e) {
        metrics.registerErr(e);
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        metrics.registerEnd(response);
        cleanupResources(put, response, client, metrics);
    }
    return true;
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics) HttpPut(org.apache.http.client.methods.HttpPut)

Example 14 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 Class<T> type) 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", type.getSimpleName(), path, new IndyResponseErrorDetails(response));
        }
        final String json = entityToString(response);
        logger.debug("Got JSON:\n\n{}\n\n", json);
        final T value = objectMapper.readValue(json, type);
        logger.debug("Got result object: {}", value);
        return value;
    } 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)

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