Search in sources :

Example 1 with AbstractResponseHandler

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);
        }
    }));
}
Also used : HttpEntity(org.apache.http.HttpEntity) AbstractResponseHandler(org.apache.http.impl.client.AbstractResponseHandler) HttpGet(org.apache.http.client.methods.HttpGet) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 2 with AbstractResponseHandler

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);
        }
    };
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) AbstractResponseHandler(org.apache.http.impl.client.AbstractResponseHandler) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Aggregations

HttpEntity (org.apache.http.HttpEntity)2 AbstractResponseHandler (org.apache.http.impl.client.AbstractResponseHandler)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 HttpGet (org.apache.http.client.methods.HttpGet)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1