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()); }
@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"); }
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:
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()); }
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(); }
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"); }
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(); } }