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)); }
copyRequestHeaders(req, proxyReq::header); proxyReq.content(new InputStreamContentProvider(req.getInputStream())); HeaderSender headerSender = new HeaderSender(resp);
final ContentProvider entity = getStreamProvider(jerseyRequest); if (entity != null) { jettyRequest.content(entity);
@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); } }
public Request newRequest(HttpClient httpClient) { Request request = httpClient.newRequest(uri); request.content(contentProvider).method(method); for (String key : headers.keySet()) { if (! "User-Agent".equals(key)) { request.header(key, headers.get(key)); } } return request; }
private boolean sendRegistration(JsonObject request) { try { ContentResponse httpResponse = httpClient.newRequest(getRegistrationUri()). content(new StringContentProvider(request.toString())). method(HttpMethod.PUT).header(HttpHeader.CONTENT_TYPE, "application/json").send(); if (httpResponse.getStatus() == 200) { return true; } } catch (Exception ex) { logger.warn("Caught exception sending registration {}", request.toString(), ex); } return false; }
@Override public Request newRequest(String uri) { Request retval = mock(Request.class); try { if (requestsTimeout || (isFirstRequestTimeout && requests.isEmpty())) { when(retval.send()).thenThrow(new TimeoutException()); } else { when(retval.send()).thenReturn(httpResponse); } if (requestsFail && featureFlag.equals("true")) { when(httpResponse.getStatus()).thenReturn(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } else if (responseException != null) { when(httpResponse.getStatus()).thenReturn(responseException.getCategory().getHttpStatus()); } else { when(httpResponse.getStatus()).thenReturn(HttpServletResponse.SC_OK); } } catch (Exception e) { e.printStackTrace(); } when(retval.method(anyString())).thenReturn(retval); when(retval.content(any(ContentProvider.class))).thenReturn(retval); when(retval.timeout(anyLong(), any(TimeUnit.class))).thenReturn(retval); requests.add(uri); return retval; }
@Before public void setup() throws InterruptedException, ExecutionException, TimeoutException { when(loadBalancer.getHealthyInstance()).thenReturn(createServiceEndpoint()); when(loadBalancer.getHealthyInstanceExclude(anyListOf(ServiceEndpoint.class))) .thenReturn(createServiceEndpoint()); when(rpcClient.getRetries()).thenReturn(NUMBER_OF_RETRIES); when(rpcClient.getTimeout()).thenReturn(0); httpClientWrapper.setLoadBalancer(loadBalancer); when(rpcClientMetrics.getMethodTimer(any(), any())).thenReturn(new GoTimer("timer")); when(tracer.buildSpan(any())).thenReturn(spanBuilder); when(spanBuilder.start()).thenReturn(span); when(httpClient.newRequest(any(URI.class))).thenReturn(request); when(httpClient.newRequest(any(String.class))).thenReturn(request); when(request.content(any(ContentProvider.class))).thenReturn(request); when(request.method(anyString())).thenReturn(request); when(request.timeout(anyLong(), any(TimeUnit.class))).thenReturn(request); when(request.send()).thenReturn(httpContentResponse); when(httpContentResponse.getStatus()).thenReturn(100); dependencyHealthCheck = mock(ServiceDependencyHealthCheck.class); }
protected String sendRequest(String path, String data, HttpMethod method) throws Exception { String url = getServiceUrl(path); Request request = httpClient.newRequest(url).method(method). header(HttpHeader.CONTENT_TYPE, "application/json"); if (data != null) { request.content(new StringContentProvider(data)); } ContentResponse response = request.send(); return response.getContentAsString(); }
request.content(new BytesContentProvider(os.toByteArray()));
/** * <p>Provides the request content via a Publisher.</p> * * @param content the request content * @return this instance */ public Builder content(Content content) { request.content(new PublisherContentProvider(content)); return this; }
/** * <p>Provides the request content via a Publisher.</p> * * @param content the request content * @return this instance */ public Builder content(Content content) { request.content(new PublisherContentProvider(content)); return this; }
protected Request getRequestWithoutSessionId(HttpServletRequest clientRequest, Request proxyRequest) throws IOException { String content = IOUtils.toString(clientRequest.getInputStream(), UTF_8); if (!content.isEmpty()) { String remoteHost = getRemoteHost(clientRequest); content = removeSessionIdSafe(content, remoteHost); } return proxyRequest.content( new StringContentProvider(clientRequest.getContentType(), content, UTF_8)); }
private Request createPostRequest(Session session, String path, ContentProvider content) { Request req = client.newRequest(config.uri(path)) .method("POST") .timeout(Config.POST_TIMEOUT, TimeUnit.MILLISECONDS) .content(content) .header("Content-Length", String.valueOf(content.getLength())); session.writeCookie(req); return req; }
private Request createPostRequest(Session session, String path, ContentProvider content) { Request req = client.newRequest(config.uri(path)) .method("POST") .timeout(Config.POST_TIMEOUT, TimeUnit.MILLISECONDS) .content(content) .header("Content-Length", String.valueOf(content.getLength())); session.writeCookie(req); return req; }
private ContentResponse writeJsonToCache(String key, String json, String cacheName) throws Exception { return client.newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), cacheName, key)) .content(new StringContentProvider(json)) .header(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_TYPE) .method(HttpMethod.PUT).send(); }
protected void registerProtobuf(String protoFileName, String protoFileContents) throws Exception { String protobufMetadataUrl = getProtobufMetadataUrl(protoFileName); ContentResponse response = client .newRequest(protobufMetadataUrl) .content(new StringContentProvider(protoFileContents)) .method(POST) .send(); assertEquals(response.getStatus(), HttpStatus.OK_200); String errorKey = protoFileName.concat(".error"); ContentResponse errorCheck = client.newRequest(getProtobufMetadataUrl(errorKey)).method(GET).send(); assertEquals(errorCheck.getStatus(), HttpStatus.NOT_FOUND_404); }
void putBinaryValueInCache(String cacheName, String key, byte[] value, MediaType mediaType) throws InterruptedException, ExecutionException, TimeoutException { ContentResponse response = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), cacheName, key)) .content(new BytesContentProvider(value)) .header(HttpHeader.CONTENT_TYPE, mediaType.toString()) .method(HttpMethod.PUT) .send(); ResponseAssertion.assertThat(response).isOk(); }
private void putInCache(String cacheName, Object key, String keyContentType, String value, String contentType) throws InterruptedException, ExecutionException, TimeoutException { Request request = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), cacheName, key)) .content(new StringContentProvider(value)) .header("Content-type", contentType) .method(HttpMethod.PUT); if (keyContentType != null) request.header("Key-Content-type", keyContentType); ContentResponse response = request.send(); ResponseAssertion.assertThat(response).isOk(); }