use of org.apache.http.StatusLine 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 {
connect();
HttpHead request = null;
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
try {
client = newClient();
request = newJsonHead(buildUrl(baseUrl, querySupplier, path));
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;
}
throw new IndyClientException(sl.getStatusCode(), "Error checking existence of: %s.\n%s", path, new IndyResponseErrorDetails(response));
} catch (final IOException e) {
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
cleanupResources(request, response, client);
}
}
use of org.apache.http.StatusLine in project indy by Commonjava.
the class IndyClientHttp method head.
public Map<String, String> head(final String path, final int... responseCodes) throws IndyClientException {
connect();
HttpHead request = null;
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
try {
request = newJsonHead(buildUrl(baseUrl, path));
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;
}
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) {
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
cleanupResources(request, response, client);
}
}
use of org.apache.http.StatusLine 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 {
checkRequestValue(value);
connect();
HttpPost post = null;
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
try {
client = newClient();
post = newJsonPost(buildUrl(baseUrl, path));
post.setEntity(new StringEntity(objectMapper.writeValueAsString(value)));
response = client.execute(post, newContext());
final StatusLine sl = response.getStatusLine();
if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
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) {
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
cleanupResources(post, response, client);
}
}
use of org.apache.http.StatusLine in project indy by Commonjava.
the class IndyClientHttp method get.
public <T> T get(final String path, final TypeReference<T> typeRef) throws IndyClientException {
connect();
HttpGet request = null;
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
try {
client = newClient();
request = newJsonGet(buildUrl(baseUrl, path));
response = client.execute(request, newContext());
final StatusLine sl = response.getStatusLine();
if (sl.getStatusCode() != 200) {
if (sl.getStatusCode() == 404) {
return null;
}
throw new IndyClientException(sl.getStatusCode(), "Error retrieving %s from: %s.\n%s", typeRef.getType(), path, new IndyResponseErrorDetails(response));
}
final String json = entityToString(response);
final T value = objectMapper.readValue(json, typeRef);
return value;
} catch (final IOException e) {
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
cleanupResources(request, response, client);
}
}
use of org.apache.http.StatusLine in project indy by Commonjava.
the class IndyFoloAdminClientModule method sealTrackingRecord.
public boolean sealTrackingRecord(String trackingId) throws IndyClientException {
http.connect();
HttpPost request = http.newRawPost(UrlUtils.buildUrl(http.getBaseUrl(), "/folo/admin", trackingId, "record"));
HttpResources resources = null;
try {
resources = http.execute(request);
HttpResponse response = resources.getResponse();
StatusLine sl = response.getStatusLine();
if (sl.getStatusCode() != 200) {
if (sl.getStatusCode() == 404) {
return false;
}
throw new IndyClientException(sl.getStatusCode(), "Error sealing tracking record %s.\n%s", trackingId, new IndyResponseErrorDetails(response));
}
return true;
} finally {
IOUtils.closeQuietly(resources);
}
}
Aggregations