Java Code Examples for java.net.http.HttpResponse#BodyHandler
The following examples show how to use
java.net.http.HttpResponse#BodyHandler .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java From Java-Coding-Problems with MIT License | 6 votes |
private static HttpResponse.PushPromiseHandler<String> pushPromiseHandler() { return (HttpRequest initiatingRequest, HttpRequest pushPromiseRequest, Function<HttpResponse.BodyHandler<String>, CompletableFuture<HttpResponse<String>>> acceptor) -> { CompletableFuture<Void> pushcf = acceptor.apply(HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body) .thenAccept((b) -> System.out.println("\nPushed resource body:\n " + b)); asyncPushRequests.add(pushcf); System.out.println("\nJust got promise push number: " + asyncPushRequests.size()); System.out.println("\nInitial push request: " + initiatingRequest.uri()); System.out.println("Initial push headers: " + initiatingRequest.headers()); System.out.println("Promise push request: " + pushPromiseRequest.uri()); System.out.println("Promise push headers: " + pushPromiseRequest.headers()); }; }
Example 2
Source File: JdkHttpClientServiceShould.java From mutual-tls-ssl with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings({"unchecked", "rawtypes"}) public void executeRequest() throws Exception { HttpResponse httpResponse = mock(HttpResponse.class); when(httpClient.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(httpResponse); when(httpResponse.statusCode()).thenReturn(200); when(httpResponse.body()).thenReturn("Hello"); ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class); ArgumentCaptor<HttpResponse.BodyHandler<String>> bodyHandlerArgumentCaptor = ArgumentCaptor.forClass(HttpResponse.BodyHandler.class); ClientResponse clientResponse = victim.executeRequest(HTTP_URL); assertThat(clientResponse.getStatusCode()).isEqualTo(200); assertThat(clientResponse.getResponseBody()).isEqualTo("Hello"); verify(httpClient, times(1)).send(httpRequestArgumentCaptor.capture(), bodyHandlerArgumentCaptor.capture()); assertThat(httpRequestArgumentCaptor.getValue().uri().toString()).isEqualTo(HTTP_URL); assertThat(httpRequestArgumentCaptor.getValue().method()).isEqualTo(GET_METHOD); assertThat(httpRequestArgumentCaptor.getValue().headers().map()).containsExactly(Assertions.entry(HEADER_KEY_CLIENT_TYPE, Collections.singletonList(JDK_HTTP_CLIENT.getValue()))); assertThat(bodyHandlerArgumentCaptor.getValue()).isEqualTo(HttpResponse.BodyHandlers.ofString()); }
Example 3
Source File: Main.java From keycloak-extension-playground with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { String serverUrl = "http://127.0.0.1:8081/auth"; String realm = "session-propagation"; String username = "tester"; String password = "test"; String clientId = "app-backend"; String clientSecret = "0b69f10f-ba95-4674-a2d0-62a7d6ae60f7"; Keycloak keycloak = Keycloak.getInstance(serverUrl, realm, username, password, clientId, clientSecret); AccessTokenResponse accessToken = keycloak.tokenManager().getAccessToken(); String sessionState = accessToken.getSessionState(); System.out.println(sessionState); while (true) { Thread.sleep(2500); Map<Object, Object> data = new HashMap<>(); data.put(OAuth2Constants.CLIENT_ID, clientId); data.put(OAuth2Constants.CLIENT_SECRET, clientSecret); data.put("token", keycloak.tokenManager().getAccessTokenString()); HttpRequest postRequest = HttpRequest.newBuilder() .uri(URI.create(String.format("%s/realms/%s/protocol/openid-connect/token/introspect", serverUrl, realm))) .header("Content-Type", "application/x-www-form-urlencoded") .POST(ofFormData(data)) .build(); HttpClient client = HttpClient.newHttpClient(); HttpResponse.BodyHandler<String> asString = HttpResponse.BodyHandlers.ofString(); HttpResponse<String> response = client.send(postRequest, asString); System.out.printf("%s: %s %n", Instant.now(), response.body()); } }
Example 4
Source File: HttpEndpoint.java From vespa with Apache License 2.0 | 5 votes |
@Override public <T> HttpResponse<T> send(HttpRequest.Builder request, HttpResponse.BodyHandler<T> handler) { try { return client.send(authenticator.authenticated(request).build(), handler); } catch (IOException | InterruptedException e) { throw new RuntimeException(request.build() + " failed: " + e.getMessage(), e); } }
Example 5
Source File: HttpClientExample.java From tutorials with MIT License | 5 votes |
private static PushPromiseHandler<String> pushPromiseHandler() { return (HttpRequest initiatingRequest, HttpRequest pushPromiseRequest, Function<HttpResponse.BodyHandler<String>, CompletableFuture<HttpResponse<String>>> acceptor) -> { acceptor.apply(BodyHandlers.ofString()) .thenAccept(resp -> { System.out.println(" Pushed response: " + resp.uri() + ", headers: " + resp.headers()); }); System.out.println("Promise request: " + pushPromiseRequest.uri()); System.out.println("Promise request: " + pushPromiseRequest.headers()); }; }
Example 6
Source File: Endpoint.java From vespa with Apache License 2.0 | 4 votes |
/** Sends the given request with required authentication. */ <T> HttpResponse<T> send(HttpRequest.Builder request, HttpResponse.BodyHandler<T> handler);
Example 7
Source File: DefaultTimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param builder http请求内容,之所以使用builder而不是构建完成的request是为了统一设置超时时间。 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @return 响应的内容 * @throws IOException if an I/O error occurs when sending or receiving * @throws InterruptedException if the operation is interrupted */ @Override public <T> HttpResponse<T> send(HttpRequest.Builder builder, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException, InterruptedException { return httpClient.send(setTimeoutAndBuild(builder), responseBodyHandler); }
Example 8
Source File: DefaultTimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param builder http请求内容,之所以使用builder而不是构建完成的request是为了统一设置超时时间。 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @return Future - 注意:该future回调的执行环境为{@link #appEventLoop} */ @Override public <T> FluentFuture<HttpResponse<T>> sendAsync(HttpRequest.Builder builder, HttpResponse.BodyHandler<T> responseBodyHandler) { return FutureUtils.fromCompletableFuture(httpClient.sendAsync(setTimeoutAndBuild(builder), responseBodyHandler)); }
Example 9
Source File: DefaultTimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param request http请求内容 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @return 响应的内容 * @throws IOException if an I/O error occurs when sending or receiving * @throws InterruptedException if the operation is interrupted * @throws IllegalArgumentException if timeout is empty */ @Override public <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException, InterruptedException { ensureTimeoutPresent(request); return httpClient.send(request, responseBodyHandler); }
Example 10
Source File: DefaultTimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param request http请求内容 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @return Future - 注意:该future回调的执行环境为{@link #appEventLoop} * @throws IllegalArgumentException if timeout is empty */ @Override public <T> FluentFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) { ensureTimeoutPresent(request); return FutureUtils.fromCompletableFuture(httpClient.sendAsync(request, responseBodyHandler)); }
Example 11
Source File: TimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param builder http请求内容,之所以使用builder而不是构建完成的request是为了统一设置超时时间。 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @return future * @throws IOException if an I/O error occurs when sending or receiving * @throws InterruptedException if the operation is interrupted */ <T> HttpResponse<T> send(HttpRequest.Builder builder, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException, InterruptedException;
Example 12
Source File: TimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param builder http请求内容,之所以使用builder而不是构建完成的request是为了统一设置超时时间。 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @return future */ <T> FluentFuture<HttpResponse<T>> sendAsync(HttpRequest.Builder builder, HttpResponse.BodyHandler<T> responseBodyHandler);
Example 13
Source File: TimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param request http请求内容 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @return 响应的内容 * @throws IOException if an I/O error occurs when sending or receiving * @throws InterruptedException if the operation is interrupted * @throws IllegalArgumentException if timeout does not set */ <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException, InterruptedException;
Example 14
Source File: TimeoutHttpClient.java From fastjgame with Apache License 2.0 | 2 votes |
/** * @param request http请求内容 * @param responseBodyHandler 响应解析器 * @param <T> 响应内容的类型 * @throws IllegalArgumentException if timeout does not set */ <T> FluentFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler);