private static void addHttpHeaders(Request request, HttpHeaders headers) { headers.forEach((key, values) -> { for (String value : values) { request.header(key, value); } }); }
@Override public void decorateProxyRequest( HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest ) { Object cookieToken = clientRequest.getAttribute(SIGNED_TOKEN_ATTRIBUTE); if (cookieToken != null && cookieToken instanceof String) { log.debug("Found cookie token will attache it to proxyRequest as cookie"); String authResult = (String) cookieToken; String existingCookies = proxyRequest.getCookies() .stream() .map(HttpCookie::toString) .collect(Collectors.joining(";")); proxyRequest.header(HttpHeader.COOKIE, Joiner.on(";").join(authResult, existingCookies)); } }
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 verifyRegistrationInConsul() { String registryServer = serviceProps.getRegistryServer(); if (StringUtils.isBlank(registryServer)) { return false; } String url = "http://" + registryServer + "/v1/catalog/service/" + serviceProps.getServiceName(); try { ContentResponse httpResponse = httpClient.newRequest(url). method(HttpMethod.GET).header(HttpHeader.CONTENT_TYPE, "application/json").send(); if (httpResponse.getStatus() != 200) { return false; } JsonArray pods = new JsonParser().parse(httpResponse.getContentAsString()).getAsJsonArray(); Iterator<JsonElement> iter = pods.iterator(); while (iter.hasNext()) { JsonElement pod = iter.next(); String serviceId = pod.getAsJsonObject().get("ServiceID").getAsString(); if (serviceProps.getServiceInstanceId().equals(serviceId)) { return true; } } } catch (Exception ex) { logger.warn("Caught exception verifying registration", ex); } return false; }
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; }
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(); }
@Override public void put(Request carrier, String key, String value) { carrier.header(key, value); } };
@Override public void apply(Request request) { request.header(header, value); }
@Override public void apply(Request request) { request.header(header, value); } }
invocation.getHeaders().asMap().forEach((h, vs) -> vs.forEach(v -> request.header(h, v))); configureTimeout(request); if (request.getAttributes().get(FOLLOW_REDIRECTS) == Boolean.FALSE) {
protected void addXForwardedHeaders(HttpServletRequest clientRequest, Request proxyRequest) { proxyRequest.header(HttpHeader.X_FORWARDED_FOR, clientRequest.getRemoteAddr()); proxyRequest.header(HttpHeader.X_FORWARDED_PROTO, clientRequest.getScheme()); proxyRequest.header(HttpHeader.X_FORWARDED_HOST, clientRequest.getHeader(HttpHeader.HOST.asString())); proxyRequest.header(HttpHeader.X_FORWARDED_SERVER, clientRequest.getLocalName()); }
@Override public void rewrite(List<IdentityToken> credentials, Request output) { if (credentials != null) { for (IdentityToken token : credentials) { try { String tokenName = SimpleIdentityTokenName.valueOf(token.getName()).name(); output.header(TOKEN_PREFIX + tokenName, token.getValue()); } catch (IllegalArgumentException e) { /*ignore*/ } } } }
@Test public void shouldNotAcceptUnknownContentType() throws Exception { //given putStringValueInCache("default", "key1", "test1"); //when ContentResponse response = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "default", "key1")) .header(HttpHeader.ACCEPT, "application/wrong-content-type") .send(); //then ResponseAssertion.assertThat(response).isNotAcceptable(); }
public void testRestAccessLog() throws Exception { client.newRequest(String.format("http://localhost:%d/rest/default/key", restServer.getPort())) .content(new StringContentProvider("value")) .header("Content-type", "text/plain; charset=utf-8") .method(HttpMethod.PUT) .send(); restServer.stop(); String logline = logAppender.getLog(0); assertTrue(logline, logline.matches("^127\\.0\\.0\\.1 - \\[\\d+/\\w+/\\d+:\\d+:\\d+:\\d+ [+-]?\\w+\\] \"PUT /rest/default/key HTTP/1\\.1\" 404 \\d+ \\d+ \\d+ Jetty/\\p{Graph}+$")); } }
protected void write(int id, String contents, HttpMethod method, MediaType contentType) throws Exception { ContentResponse response = client .newRequest(String.format("http://localhost:%d/rest/%s/%d", pickServer().getPort(), CACHE_NAME, id)) .method(method) .content(new StringContentProvider(contents)) .header(HttpHeader.CONTENT_TYPE, contentType.toString()) .send(); assertEquals(response.getStatus(), HttpStatus.OK_200); }
@Test public void shouldReturnJsonWithDefaultConfig() throws Exception { putStringValueInCache("textCache", "test", "Hey!"); ContentResponse getResponse = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "textCache", "test")) .method(HttpMethod.GET) .header("Accept", "application/json") .send(); ResponseAssertion.assertThat(getResponse).isOk(); ResponseAssertion.assertThat(getResponse).hasReturnedText("\"Hey!\""); }
@Test public void shouldGetAllEntriesFromEmptyCache() throws Exception { //when ContentResponse response = client .newRequest("http://localhost:" + restServer().getPort() + "/rest/default") .method(HttpMethod.GET) .header("Content-Type", "text/plain; charset=utf-8") .send(); //then ResponseAssertion.assertThat(response).isOk(); ResponseAssertion.assertThat(response).hasReturnedText(""); }