socket = client.prepareGet(url).execute(handler).get(); LOG.info("Successfully connected to {}.", url); } catch (ExecutionException e) {
response = futureStatus.get(DEFAULT_TIMEOUT_SECS, TimeUnit.SECONDS); } catch (final TimeoutException toe) { log.warn("Timeout while connecting to Okta");
private Response doGetRequest(final String path) { final BoundRequestBuilder builder = httpClient.prepareGet(securityConfig.getShiroOktaUrl() + path); builder.addHeader("Authorization", "SSWS " + securityConfig.getShiroOktaAPIToken()); builder.addHeader("Content-Type", "application/json; charset=UTF-8"); final Response response; try { final ListenableFuture<Response> futureStatus = builder.execute(new AsyncCompletionHandler<Response>() { @Override public Response onCompleted(final Response response) throws Exception { return response; } }); response = futureStatus.get(DEFAULT_TIMEOUT_SECS, TimeUnit.SECONDS); } catch (final TimeoutException toe) { log.warn("Timeout while connecting to Okta"); throw new AuthorizationException(toe); } catch (final Exception e) { log.warn("Error while connecting to Okta"); throw new AuthorizationException(e); } return response; }
return sb.toString(); }).get();
public Response getReponse() throws InterruptedException { try { return future.get(); } catch (ExecutionException e) { throw Throwables.propagate(e); } }
@Override public void write(OutputStream output) throws WebApplicationException, IOException { if (wrappedOutputStream == null) { wrappedOutputStream = output; try { requestBuilder.execute(this).get(); } catch (ExecutionException | InterruptedException e) { LOG.error("Failed or interrupted while proxying a download from Mesos", e); } } } }
public static void main(String[] args) { SecureRandom secureRandom = new SecureRandom(); SSLContext sslContext = null; try { sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, null, secureRandom); } catch (Exception e) { e.printStackTrace(); } AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder() .setConnectTimeout(5000) .setSSLContext(sslContext).build(); AsyncHttpClient client = new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config); long start = System.currentTimeMillis(); try { client.executeRequest(client.prepareGet("http://www.google.com").build()).get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } LOGGER.debug("COMPLETE: " + (System.currentTimeMillis() - start) + "ms"); } }
final Response response = builder.execute().get();
try { LOG.trace("Sending request to leader: {}", httpRequest); response = httpClient.executeRequest(httpRequest).get(); } catch (IOException|ExecutionException|InterruptedException e) { LOG.error("Could not proxy request {} to leader", e);
private LoadBalancerUpdateHolder sendRequest(LoadBalancerRequestId loadBalancerRequestId, Request request, BaragonRequestState onFailure) { try { LOG.trace("Sending LB {} request for {} to {}", request.getMethod(), loadBalancerRequestId, request.getUrl()); ListenableFuture<Response> future = httpClient.executeRequest(request); Response response = future.get(loadBalancerTimeoutMillis, TimeUnit.MILLISECONDS); LOG.trace("LB {} request {} returned with code {}", request.getMethod(), loadBalancerRequestId, response.getStatusCode()); if (response.getStatusCode() == 504) { return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("LB %s request %s timed out", request.getMethod(), loadBalancerRequestId))); } else if (!JavaUtils.isHttpSuccess(response.getStatusCode())) { return new LoadBalancerUpdateHolder(onFailure, Optional.of(String.format("Response status code %s", response.getStatusCode()))); } BaragonResponse lbResponse = readResponse(response); return new LoadBalancerUpdateHolder(lbResponse.getLoadBalancerState(), lbResponse.getMessage()); } catch (TimeoutException te) { LOG.trace("LB {} request {} timed out after waiting {}", request.getMethod(), loadBalancerRequestId, JavaUtils.durationFromMillis(loadBalancerTimeoutMillis)); return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("Timed out after %s", JavaUtils.durationFromMillis(loadBalancerTimeoutMillis)))); } catch (Throwable t) { LOG.error("LB {} request {} to {} threw error", request.getMethod(), loadBalancerRequestId, request.getUrl(), t); return new LoadBalancerUpdateHolder(BaragonRequestState.UNKNOWN, Optional.of(String.format("Exception %s - %s", t.getClass().getSimpleName(), t.getMessage()))); } }
public Collection<MesosFileObject> browse(String slaveHostname, String fullPath) throws SlaveNotFoundException { try { PerRequestConfig timeoutConfig = new PerRequestConfig(); timeoutConfig.setRequestTimeoutInMs((int) configuration.getSandboxHttpTimeoutMillis()); Response response = asyncHttpClient .prepareGet(String.format("http://%s:5051/files/browse", slaveHostname)) .setPerRequestConfig(timeoutConfig) .addQueryParameter("path", fullPath) .execute() .get(); if (response.getStatusCode() == 404) { return Collections.emptyList(); } if (response.getStatusCode() != 200) { throw new RuntimeException(String.format("Got HTTP %s from Mesos slave", response.getStatusCode())); } return objectMapper.readValue(response.getResponseBodyAsStream(), MESOS_FILE_OBJECTS); } catch (ConnectException ce) { throw new SlaveNotFoundException(ce); } catch (Exception e) { if (e.getCause().getClass() == ConnectException.class) { throw new SlaveNotFoundException(e); } else { throw Throwables.propagate(e); } } }
private SingularityUserPermissionsResponse verifyUncached(String authHeaderValue) { try { Response response = asyncHttpClient.prepareGet(webhookAuthConfiguration.getAuthVerificationUrl()) .addHeader("Authorization", authHeaderValue) .execute() .get(); if (response.getStatusCode() > 299) { throw WebExceptions.unauthorized(String.format("Got status code %d when verifying jwt", response.getStatusCode())); } else { String responseBody = response.getResponseBody(); SingularityUserPermissionsResponse permissionsResponse = objectMapper.readValue(responseBody, SingularityUserPermissionsResponse.class); if (!permissionsResponse.getUser().isPresent()) { throw WebExceptions.unauthorized(String.format("No user present in response %s", permissionsResponse)); } if (!permissionsResponse.getUser().get().isAuthenticated()) { throw WebExceptions.unauthorized(String.format("User not authenticated (response: %s)", permissionsResponse)); } permissionsCache.put(authHeaderValue, permissionsResponse); return permissionsResponse; } } catch (Throwable t) { throw new RuntimeException(t); } } }
Response response = future.get();
@Override public void write(OutputStream output) throws WebApplicationException, IOException { if (wrappedOutputStream == null) { wrappedOutputStream = output; try { requestBuilder.execute(this).get(); } catch (ExecutionException | InterruptedException e) { LOG.error("Failed or interrupted while proxying a download from Mesos", e); } } } }
private SimpleResponse executeSimpleRequest(Request request) throws IOException, InterruptedException, ExecutionException, TimeoutException { ListenableFuture<SimpleResponse> fResponse = this.client.executeRequest(request, SIMPLE_RESPONSE_HANDLER); return getRequestTimeout() >= 0L ? fResponse.get(getRequestTimeout(), TimeUnit.MILLISECONDS) : fResponse.get(); }
@Override public void run() { try { future.set(new WebResponseImpl(injector, responseFuture.get())); } catch (InterruptedException e) { throw new TransportException(e); } catch (ExecutionException e) { throw new TransportException(e); } } }, executor);
/** Execute a PUT request. */ @Override public HttpResponse put() { this.type = "PUT"; try { return new HttpAsyncResponse(prepare(preparePut()).execute().get()); } catch (Exception e) { throw new RuntimeException(e); } }
/** Execute a OPTIONS request. */ @Override public HttpResponse options() { this.type = "OPTIONS"; try { return new HttpAsyncResponse(prepare(prepareOptions()).execute().get()); } catch (Exception e) { throw new RuntimeException(e); } }
/** Execute a DELETE request. */ @Override public HttpResponse delete() { this.type = "DELETE"; try { return new HttpAsyncResponse(prepare(prepareDelete()).execute().get()); } catch (Exception e) { throw new RuntimeException(e); } }
public static int post(@Nonnull final String url, @Nonnull final JSONObject data) throws IOException, ExecutionException, InterruptedException { AsyncHttpClient httpClient = AHC.instance(); ListenableFuture<Response> execute = httpClient.preparePost(url) .setBodyEncoding("UTF-8") .setHeader("Content-Type", "application/json; charset=UTF-8") .setBody(data.toString()) .execute(); return execute.get().getStatusCode(); } }