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