private SingularityClientException fail(String type, HttpResponse response) { String body = ""; try { body = response.getAsString(); } catch (Exception e) { LOG.warn("Unable to read body", e); } String uri = ""; try { uri = response.getRequest().getUrl().toString(); } catch (Exception e) { LOG.warn("Unable to read uri", e); } throw new SingularityClientException(String.format("Failed '%s' action on Singularity (%s) - code: %s, %s", type, uri, response.getStatusCode(), body), response.getStatusCode()); }
private HttpResponse executeRequest(Function<String, String> hostToUri, Method method, Optional<?> body, Map<String, ?> queryParams) { HttpRequest.Builder request = HttpRequest.newBuilder().setMethod(method); if (body.isPresent()) { request.setBody(body.get()); } addQueryParams(request, queryParams); addCredentials(request); List<String> hosts = new ArrayList<>(hostsProvider.get()); request .setRetryStrategy(RetryStrategy.NEVER_RETRY) .setMaxRetries(1); try { return httpResponseRetryer.call(() -> { if (hosts.isEmpty()) { // We've tried everything we started with. Look again. hosts.addAll(hostsProvider.get()); } int selection = random.nextInt(hosts.size()); String host = hosts.get(selection); String url = hostToUri.apply(host); hosts.remove(selection); LOG.info("Making {} request to {}", method, url); request.setUrl(url); return httpClient.execute(request.build()); }); } catch (ExecutionException | RetryException exn) { throw new SingularityClientException("Failed request to Singularity", exn); } }
switch (request.getMethod()) { case GET: apacheRequest = new HttpGet(request.getUrl()); break; case POST: apacheRequest = new HttpPost(request.getUrl()); break; case PUT: apacheRequest = new HttpPut(request.getUrl()); break; case DELETE: apacheRequest = new HttpDeleteWithBody(request.getUrl()); break; case PATCH: apacheRequest = new HttpPatch(request.getUrl()); break; case HEAD: apacheRequest = new HttpHead(request.getUrl()); break; default: throw new IllegalArgumentException("Unrecognized request method: " + request.getMethod()); byte[] body = request.getBody(mapper); if (body != null && apacheRequest instanceof HttpEntityEnclosingRequest) { ((HttpEntityEnclosingRequest) apacheRequest).setEntity(new ByteArrayEntity(body)); for (Header header : request.getHeaders()) { apacheRequest.addHeader(header.getName(), header.getValue());
public HttpRequest build() { URI url = buildUrl(); Headers headers = buildHeaders(); validateBodyState(); return new HttpRequest(method, url, headers, compression, body, jsonBody, options); }
static String formatRequest(HttpRequest request) { StringBuilder builder = new StringBuilder(); String url = request.getUrl().toString(); if (urlContainsRawAuthToken(url)) { url = urlWithRedactedToken(url); } builder.append(request.getMethod()).append(" ").append(url).append("\n"); builder.append("------------------------------------------\n"); request.getHeaders().forEach(header -> builder.append(safeHeaderString(header.getName(), header.getValue()))); builder.append("------------------------------------------\n"); byte[] body = request.getBody(ObjectMapperUtils.mapper()); if (body != null) { try { builder.append( ObjectMapperUtils.mapper().writerWithDefaultPrettyPrinter() .writeValueAsString( ObjectMapperUtils.mapper().readValue(body, Object.class))); } catch (IOException ex) { builder.append(new String(body, Charsets.UTF_8)); } } return builder.toString(); }
public Request convert(HttpRequest request) { RequestBuilder ningRequest = new RequestBuilder(request.getMethod().name()); ningRequest.setUrl(request.getUrl().toString()); byte[] body = request.getBody(mapper); if (body != null) { ningRequest.setBody(body); } for (Header header : request.getHeaders()) { String name = header.getName(); if ("Host".equalsIgnoreCase(name)) { ningRequest.setVirtualHost(header.getValue()); } else { ningRequest.addHeader(name, header.getValue()); } } return ningRequest.build(); } }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); singularityClient = buildClient(); when(response.getRequest()) .thenReturn(request); when(request.getUrl()) .thenReturn(new URI("test-url")); }
private HttpResponse getFromMesos(String uri, boolean useShortTimeout) { HttpClient currentHttpClient = useShortTimeout ? shortTimeoutHttpClient : httpClient; HttpResponse response = null; final long start = System.currentTimeMillis(); LOG.debug("Fetching {} from mesos", uri); try { response = currentHttpClient.execute(HttpRequest.newBuilder().setUrl(uri).build(), new Options()); LOG.debug("Response {} - {} after {}", response.getStatusCode(), uri, JavaUtils.duration(start)); } catch (Exception e) { throw new MesosClientException(String.format("Exception fetching %s after %s", uri, JavaUtils.duration(start)), e); } if (!response.isSuccess()) { throw new MesosClientException(String.format("Invalid response code from %s : %s", uri, response.getStatusCode())); } return response; }
@Test public void itRetriesRequestsThatErrorDueToDeadHost() { when(httpClient.execute(any())) .thenReturn(response); when(response.getStatusCode()) .thenReturn(503) .thenReturn(200); when(response.isServerError()) .thenReturn(true) .thenReturn(false); singularityClient.pauseSingularityRequest("requestId", Optional.absent()); verify(httpClient, times(2)) .execute(requestCaptor.capture()); HttpRequest sentRequest = requestCaptor.getValue(); assertThat(sentRequest.getUrl().toString()) .matches("http://host(1|2)/singularity/v2/api/requests/request/requestId/pause"); }
private HttpRequest.Builder buildRequest(String uri, Map<String, String> queryParams) { final HttpRequest.Builder builder = HttpRequest.newBuilder().setUrl(uri); final Optional<String> maybeAuthkey = authkeyProvider.get(); if (maybeAuthkey.isPresent()) { builder.setQueryParam("authkey").to(maybeAuthkey.get()); } if ((queryParams != null) && (!queryParams.isEmpty())) { for (Map.Entry<String, String> entry : queryParams.entrySet()) { builder.setQueryParam(entry.getKey()).to(entry.getValue()); } } return builder; }
private BaragonClientException fail(String type, HttpResponse response) { String body = ""; try { body = response.getAsString(); } catch (Exception e) { LOG.warn("Unable to read body", e); } String uri = ""; try { uri = response.getRequest().getUrl().toString(); } catch (Exception e) { LOG.warn("Unable to read uri", e); } throw new BaragonClientException(String.format("Failed '%s' action on Baragon (%s) - code: %s, %s", type, uri, response.getStatusCode(), body), response.getStatusCode()); }
private HttpRequest.Builder buildBaseSlackPost(SlackMethod method) { return HttpRequest.newBuilder() .setMethod(Method.POST) .setUrl(config.getSlackApiBasePath().get() + "/" + method.getMethod()); }
LOG.warn(String.format("Going to retry failed request to '%s' (Status: %d)", request.getUrl(), response.getStatusCode())); } else { return response; if (timedOut.get()) { close(apacheResponse); e = new IOException(new TimeoutException(String.format("Request to '%s' timed out", request.getUrl()))); LOG.warn(String.format("Going to retry failed request to '%s'", request.getUrl()), e); } else { throw e;
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); singularityClient = buildClient(); when(response.getRequest()) .thenReturn(request); when(request.getUrl()) .thenReturn(new URI("test-url")); }
@Test public void itRetriesRequestsThatErrorDueToDeadHost() { when(httpClient.execute(any())) .thenReturn(response); when(response.getStatusCode()) .thenReturn(503) .thenReturn(200); when(response.isServerError()) .thenReturn(true) .thenReturn(false); singularityClient.pauseSingularityRequest("requestId", Optional.absent()); verify(httpClient, times(2)) .execute(requestCaptor.capture()); HttpRequest sentRequest = requestCaptor.getValue(); assertThat(sentRequest.getUrl().toString()) .matches("http://host(1|2)/singularity/v2/api/requests/request/requestId/pause"); }