Search in sources :

Example 1 with ClientMetrics

use of org.commonjava.indy.client.core.metric.ClientMetrics 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 {
    HttpHead request = newJsonHead(buildUrl(baseUrl, querySupplier, 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 (validResponseCode(sl.getStatusCode(), responseCodes)) {
            return true;
        } else if (sl.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            return false;
        }
        metrics.registerErr(sl);
        throw new IndyClientException(sl.getStatusCode(), "Error checking existence of: %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(request, response, client, metrics);
    }
}
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) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics) HttpHead(org.apache.http.client.methods.HttpHead)

Example 2 with ClientMetrics

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

the class IndyClientHttp method putWithStream.

public void putWithStream(final String path, final InputStream stream, final int... responseCodes) throws IndyClientException {
    final HttpPut put = newRawPut(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(put);
    connect();
    addLoggingMDCToHeaders(put);
    final CloseableHttpClient client = newClient();
    CloseableHttpResponse response = null;
    try {
        put.setEntity(new InputStreamEntity(stream));
        response = client.execute(put, newContext());
        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            metrics.registerErr(sl);
            throw new ClientProtocolException(new IndyClientException(sl.getStatusCode(), "Error in response from: %s.\n%s", path, new IndyResponseErrorDetails(response)));
        }
    } catch (final ClientProtocolException e) {
        final Throwable cause = e.getCause();
        metrics.registerErr(cause);
        if (cause instanceof IndyClientException) {
            throw (IndyClientException) cause;
        }
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } 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);
    }
}
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) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 3 with ClientMetrics

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

the class IndyClientHttp method deleteWithChangelog.

public void deleteWithChangelog(final String path, final String changelog, final int... responseCodes) throws IndyClientException {
    HttpDelete delete = newDelete(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(delete);
    connect();
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        addLoggingMDCToHeaders(delete);
        delete.setHeader(ArtifactStore.METADATA_CHANGELOG, changelog);
        response = client.execute(delete, newContext());
        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            metrics.registerErr(sl);
            throw new IndyClientException(sl.getStatusCode(), "Error deleting: %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(delete, response, client, metrics);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpDelete(org.apache.http.client.methods.HttpDelete) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ClientMetrics(org.commonjava.indy.client.core.metric.ClientMetrics)

Example 4 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 HttpGet req) throws IndyClientException {
    ClientMetrics metrics = metricManager.register(req);
    connect();
    addLoggingMDCToHeaders(req);
    CloseableHttpResponse response = null;
    try {
        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 : 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)

Example 5 with ClientMetrics

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

the class IndyClientHttp method delete.

public void delete(final String path, final int... responseCodes) throws IndyClientException {
    HttpDelete delete = newDelete(buildUrl(baseUrl, path));
    ClientMetrics metrics = metricManager.register(delete);
    connect();
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = newClient();
        addLoggingMDCToHeaders(delete);
        response = client.execute(delete, newContext());
        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            metrics.registerErr(sl);
            throw new IndyClientException(sl.getStatusCode(), "Error deleting: %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(delete, response, client, metrics);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpDelete(org.apache.http.client.methods.HttpDelete) 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