use of org.apache.http.impl.client.AbstractResponseHandler in project selenium_java by sergueik.
the class GithubApi method getRelease.
/**
* Returns a specific release from the specified repository.
*
* @param owner the repository's owner
* @param repo the repository's name
* @param id the id of the release to retrieve
* @return An {@link Optional} that contains the specified release if it could be found.
* @throws IOException if an IO error occurs while communicating with GitHub.
* @throws URISyntaxException if the crafted URI is incorrect.
*/
public Optional<GithubRelease> getRelease(String owner, String repo, String id) throws IOException, URISyntaxException {
URIBuilder requestUrl = new URIBuilder().setScheme(URL_PROTOCOL).setHost(GITHUB_API_HOSTNAME).setPath("/repos/" + owner + "/" + repo + "/releases/" + id);
logger.debug(() -> "requestUrl = " + requestUrl);
HttpGet httpget = new HttpGet(requestUrl.build());
return Optional.ofNullable(httpClient.execute(httpget, new AbstractResponseHandler<GithubRelease>() {
@Override
public GithubRelease handleEntity(HttpEntity entity) throws IOException {
return gson.fromJson(EntityUtils.toString(entity), GithubRelease.class);
}
}));
}
use of org.apache.http.impl.client.AbstractResponseHandler in project camunda-bpm-platform by camunda.
the class RequestExecutor method handleResponse.
protected <T> ResponseHandler<T> handleResponse(final Class<T> responseClass) {
return new AbstractResponseHandler<T>() {
@SuppressWarnings("unchecked")
public T handleEntity(HttpEntity responseEntity) throws IOException {
T response = null;
if (responseClass.isAssignableFrom(byte[].class)) {
InputStream inputStream = null;
try {
inputStream = responseEntity.getContent();
response = (T) IoUtil.inputStreamAsByteArray(inputStream);
} finally {
IoUtil.closeSilently(inputStream);
}
} else if (!responseClass.isAssignableFrom(Void.class)) {
try {
response = deserializeResponse(responseEntity, responseClass);
} catch (EngineClientException e) {
throw new RuntimeException(e);
}
}
try {
EntityUtils.consume(responseEntity);
} catch (IOException e) {
LOG.exceptionWhileClosingResourceStream(response, e);
}
return response;
}
@Override
public T handleResponse(HttpResponse response) throws HttpResponseException, IOException {
final StatusLine statusLine = response.getStatusLine();
final HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
String error = IoUtil.inputStreamAsString(inputStream);
throw new HttpResponseException(statusLine.getStatusCode(), error);
} finally {
EntityUtils.consume(entity);
IoUtil.closeSilently(inputStream);
}
}
return entity == null ? null : handleEntity(entity);
}
};
}
Aggregations