protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, @Nullable String body) { Request httpRequest = this.httpClient.newRequest(url).method(method); addHttpHeaders(httpRequest, headers); if (body != null) { httpRequest.content(new StringContentProvider(body)); } ContentResponse response; try { response = httpRequest.send(); } catch (Exception ex) { throw new SockJsTransportFailureException("Failed to execute request to " + url, ex); } HttpStatus status = HttpStatus.valueOf(response.getStatus()); HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders()); return (response.getContent() != null ? new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) : new ResponseEntity<>(responseHeaders, status)); }
@Override public ClientResponse apply(final ClientRequest jerseyRequest) throws ProcessingException { final Request jettyRequest = translateRequest(jerseyRequest); final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest); final ContentProvider entity = getBytesProvider(jerseyRequest); if (entity != null) { jettyRequest.content(entity); } try { final ContentResponse jettyResponse = jettyRequest.send(); HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(), JettyConnector.this.getClass().getName()); final javax.ws.rs.core.Response.StatusType status = jettyResponse.getReason() == null ? Statuses.from(jettyResponse.getStatus()) : Statuses.from(jettyResponse.getStatus(), jettyResponse.getReason()); final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest); processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse); try { jerseyResponse.setEntityStream(new HttpClientResponseInputStream(jettyResponse)); } catch (final IOException e) { LOGGER.log(Level.SEVERE, null, e); } return jerseyResponse; } catch (final Exception e) { throw new ProcessingException(e); } }
if (response.getStatus() != HttpStatus.OK_200) { XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatus()); return FAIL; String responseMsg = response.getContentAsString(); XxlJobLogger.log(responseMsg); return SUCCESS;
public ResponseAssertion isUnauthorized() { Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED_401); Assertions.assertThat(response.getHeaders().get(HttpHeader.WWW_AUTHENTICATE)).isNotNull().isNotEmpty(); return this; }
public void updateHealthStatus(HealthCheck.Status status) throws Exception { logger.trace("Updating health of {}", serviceProps.getServiceName()); ContentResponse httpResponse = httpClient.newRequest(getHealthCheckUri(status)).method(HttpMethod.PUT).send(); if (httpResponse.getStatus() != 200) { logger.warn("Received {} trying to update health", httpResponse.getStatus()); } }
public void setResponseException(RpcCallException responseException) { this.responseException = responseException; if (featureFlag.equals("true")) { when(httpResponse.getStatus()).thenReturn(responseException.getCategory().getHttpStatus()); } String response = "{\"error\":" + responseException.toJson() + ",\"result\":{}}"; when(httpResponse.getContentAsString()).thenReturn(response); RpcEnvelope.Response pbResponse = RpcEnvelope.Response.newBuilder(). setError(responseException.toJson().toString()).build(); byte[] responseArray = pbResponse.toByteArray(); byte[] headerLength = Ints.toByteArray(responseArray.length); byte[] bodyLength = Ints.toByteArray(0); byte[] overallPayload = concatAll(headerLength, responseArray, bodyLength); when(httpResponse.getContent()).thenReturn(overallPayload); }
@Override public RpcCallException decodeException(ContentResponse response) throws RpcCallException { try { if (response != null) { JsonObject json = (JsonObject) new JsonParser().parse(response.getContentAsString()); JsonElement error = json.get("error"); if (error != null) { return RpcCallException.fromJson(error.toString()); } } } catch (Exception ex) { logger.warn("Caught exception decoding protobuf response exception", ex); throw new RpcCallException(RpcCallException.Category.InternalServerError, RpcCallExceptionDecoder.exceptionToString(ex)); } return null; }
private static InputStream getInputStream(final ContentResponse response) { return new ByteArrayInputStream(response.getContent()); } }
@Before public void setup() throws Exception { HttpClient httpClient = mock(HttpClient.class); response = mock(ContentResponse.class); when(response.getStatus()).thenReturn(200); when(response.getContentAsString()).thenReturn(healthInfo); HttpFields headers = new HttpFields(); headers.add(CONSUL_INDEX, "42"); when(response.getHeaders()).thenReturn(headers); Request request = mock(Request.class); when(httpClient.newRequest(anyString())).thenReturn(request); when(request.send()).thenReturn(response); props = new ServiceProperties(); props.addProperty(ServiceProperties.REGISTRY_SERVER_KEY, "localhost:1234"); worker = new RegistrationMonitorWorker(httpClient, props, mock(ServiceDependencyHealthCheck.class)); worker.setServiceName("foobar"); }
private void initialize() { httpResponse = mock(ContentResponse.class); when(httpResponse.getHeaders()).thenReturn(new HttpFields()); when(httpResponse.getStatus()).thenReturn(200); try { RpcEnvelope.Response.Builder responseBuilder = RpcEnvelope.Response.newBuilder(); responseBuilder.setServiceMethod("Test.test"); if (requestsFail) { RpcCallException callException = new RpcCallException( RpcCallException.Category.InternalServerError, "requests fail!"); responseBuilder.setError(callException.toJson().toString()); } RpcEnvelope.Response rpcResponse = responseBuilder.build(); byte[] responseHeader = rpcResponse.toByteArray(); byte[] payload = FrameworkTest.Foobar.newBuilder().build().toByteArray(); ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(Ints.toByteArray(responseHeader.length)); out.write(responseHeader); out.write(Ints.toByteArray(payload.length)); out.write(payload); out.flush(); when(httpResponse.getContent()).thenReturn(out.toByteArray()); } catch (Exception e) { e.printStackTrace(); } }
private void readConsulIndexHeader(ContentResponse httpResponse) { String value = httpResponse.getHeaders().get(CONSUL_INDEX); if (value != null) { consulIndex = value; } }
String encoding = response.getEncoding() != null ? response.getEncoding().replaceAll("\"", "").trim() : "UTF-8"; String responseBody; try { responseBody = new String(response.getContent(), encoding); } catch (UnsupportedEncodingException e) { responseBody = null;
@Override public String getHttpCodeMessage() { return response.getReason(); }
if (response.getStatus() != HttpStatus.OK_200) { XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatus()); return FAIL; String responseMsg = response.getContentAsString(); XxlJobLogger.log(responseMsg); return SUCCESS;
assertEquals(HttpServletResponse.SC_OK,response.getStatus()); String sessionCookie = response.getHeaders().get("Set-Cookie"); assertTrue(sessionCookie != null); response = request.send(); assertEquals(HttpServletResponse.SC_OK,response.getStatus()); request.header("Cookie", sessionCookie); response = request.send(); assertEquals(HttpServletResponse.SC_OK,response.getStatus());
@Test public void problemGettingHealthInfo() { when(response.getStatus()).thenReturn(404); List<ConsulHealthEntry> entries = worker.loadCurrentHealthList(); assertThat(entries).isEmpty(); when(response.getStatus()).thenReturn(0); entries = worker.loadCurrentHealthList(); assertThat(entries).isEmpty(); }
@Test public void requestHealthListSingleInstance() { when(response.getContentAsString()).thenReturn(healthInfo); List<ConsulHealthEntry> entries = worker.loadCurrentHealthList(); assertThat(entries.size()).isEqualTo(1); }
private boolean responseWasSuccessful(RpcCallExceptionDecoder decoder, ContentResponse response, int lastStatusCode) throws RpcCallException { if (shouldExposeErrorsToHttp(serviceProps)) { return lastStatusCode == 200 && response != null && response.getContent().length > 0; } else if (lastStatusCode != 0 && lastStatusCode != 200) { return false; } else if (response == null || response.getContent() == null) { return false; } RpcCallException exception = decoder.decodeException(response); return (exception == null); }
/** * Parse out information from LemonGraph response * @param res ContentResponse */ public void parseContentResponse(ContentResponse res) { JSONObject ret = new JSONObject(); try { ret = new JSONObject(res.getContentAsString()); } catch (Exception e) {} parseHeadersAndSetVariables(res.getHeaders()); parseJobId(ret); setContent(ret); setResponseCode(res.getStatus()); setSuccess(true); }