List of usage examples for org.apache.http.client ResponseHandler handleResponse
T handleResponse(HttpResponse response) throws ClientProtocolException, IOException;
From source file:org.apache.http.impl.client.cache.CachingHttpClient.java
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException { HttpResponse resp = execute(target, request, context); return responseHandler.handleResponse(resp); }
From source file:org.apache.http.impl.client.cache.CachingHttpClient.java
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException { HttpResponse resp = execute(request, context); return responseHandler.handleResponse(resp); }
From source file:org.apache.http.impl.client.CloseableHttpClient.java
/** * Executes a request using the default context and processes the * response using the given response handler. The content entity associated * with the response is fully consumed and the underlying connection is * released back to the connection manager automatically in all cases * relieving individual {@link ResponseHandler}s from having to manage * resource deallocation internally.//from w w w . ja v a 2 s . c om * * @param target the target host for the request. * Implementations may accept <code>null</code> * if they can still determine a route, for example * to a default target or by inspecting the request. * @param request the request to execute * @param responseHandler the response handler * @param context the context to use for the execution, or * <code>null</code> to use the default context * * @return the response object as generated by the response handler. * @throws IOException in case of a problem or the connection was aborted * @throws ClientProtocolException in case of an http protocol error */ public <T> T execute(final HttpHost target, final HttpRequest request, final ResponseHandler<? extends T> responseHandler, final HttpContext context) throws IOException, ClientProtocolException { Args.notNull(responseHandler, "Response handler"); final HttpResponse response = execute(target, request, context); final T result; try { result = responseHandler.handleResponse(response); } catch (final Exception t) { final HttpEntity entity = response.getEntity(); try { EntityUtils.consume(entity); } catch (final Exception t2) { // Log this exception. The original exception is more // important and will be thrown to the caller. this.log.warn("Error consuming content after an exception.", t2); } if (t instanceof RuntimeException) { throw (RuntimeException) t; } if (t instanceof IOException) { throw (IOException) t; } throw new UndeclaredThrowableException(t); } // Handling the response was successful. Ensure that the content has // been fully consumed. final HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); return result; }
From source file:org.labkey.filetransfer.globus.GlobusFileTransferProvider.java
public TransferResult transfer(@NotNull TransferEndpoint source, @NotNull TransferEndpoint destination, @NotNull List<String> fileNames, @Nullable String label) throws Exception { if (fileNames.isEmpty()) return null; URI submissionIdUri = new URI(settings.getTransferApiUrlPrefix() + "/submission_id"); SubmissionId submissionId = (SubmissionId) makeApiGetRequest(submissionIdUri, SubmissionId.class); if (submissionId == null || submissionId.getValue() == null) throw new Exception("Could not retrieve submission id from uri " + submissionIdUri + ". The provider's service may be down or there could be a configuration problem. Please contact an administrator."); JSONObject transferObject = new JSONObject(); transferObject.put("DATA_TYPE", "transfer"); transferObject.put("submission_id", submissionId.getValue()); transferObject.put("source_endpoint", source.getId()); transferObject.put("destination_endpoint", destination.getId()); if (label != null) transferObject.put("label", label); List<JSONObject> items = new ArrayList<>(); for (String fileName : fileNames) { JSONObject item = new JSONObject(); item.put("DATA_TYPE", "transfer_item"); item.put("source_path", source.getPath() + "/" + fileName); item.put("destination_path", destination.getPath() + fileName); items.add(item);//from w ww .ja v a2s . c o m } transferObject.put("DATA", items); URI transferUri = new URI(getUrlPrefix() + "/transfer"); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(transferUri); httpPost.setHeader("Authorization", "Bearer " + credential.getAccessToken()); httpPost.setEntity( new StringEntity(JSONObject.valueToString(transferObject), ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { ResponseHandler<String> handler = new BasicResponseHandler(); // StatusLine status = response.getStatusLine(); // TODO check what happens in the error cases String contents = handler.handleResponse(response); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(contents, TransferResult.class); } } }
From source file:org.labkey.filetransfer.globus.GlobusFileTransferProvider.java
private Object makeApiGetRequest(URI uri, Class clazz) throws IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); httpGet.setHeader("Authorization", "Bearer " + credential.getAccessToken()); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { ResponseHandler<String> handler = new BasicResponseHandler(); StatusLine status = response.getStatusLine(); if (status.getStatusCode() == HttpStatus.SC_OK) { String contents = handler.handleResponse(response); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(contents, clazz); }//from w w w . j a v a 2 s .c o m } catch (UnknownHostException e) { logger.error("Could not make request using uri " + uri, e); } } return null; }
From source file:org.opencastproject.kernel.security.TrustedHttpClientImpl.java
@Override public <T> T execute(HttpUriRequest httpUriRequest, ResponseHandler<T> responseHandler, int connectionTimeout, int socketTimeout) throws TrustedHttpClientException { try {//from www . j a va 2 s . com return responseHandler.handleResponse(execute(httpUriRequest, connectionTimeout, socketTimeout)); } catch (IOException e) { throw new TrustedHttpClientException(e); } }