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);
}
}
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);
}
}
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 );
}
}
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);
}
}
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 );
}
}
Aggregations