/** * @return The status code of the response */ public int getStatusCode() { return this.httpResponse.statusCode(); }
/** * @return The response content */ public String getContent() { return this.httpResponse.body(); }
/** * Build http request, and send out sync. */ public AsyncResponseContext sendAsync() { Request request = build(); // interceptors var interceptors = Lists.convert(client.interceptors(), InterceptorSupplier::get); for (var interceptor : interceptors) { request = interceptor.onRequest(request); } var finalRequest = request; var httpRequest = RequestUtils.toHttpRequest(finalRequest); var responseFuture = client.httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofPublisher()) .thenApply(r -> { var headers = Headers.ofHttpHeaders(r.headers()); URL url; try { url = r.uri().toURL(); } catch (MalformedURLException e) { throw new RequestsException(e); } return new SimpleAsyncResponseInfo(url, r.statusCode(), headers, r.body()); }); return new AsyncResponseContext(method, responseFuture, interceptors); }
/** * @return The content type of the response */ public String getContentType() { return this.httpResponse.headers().firstValue(CONTENT_TYPE).orElse(""); }
@Override public Mono<ReactiveHttpResponse> executeRequest(ReactiveHttpRequest request) { HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(request.uri()) .method(request.method().toUpperCase(), provideBody(request)); setUpHeaders(request, requestBuilder); if(requestTimeout > 0){ requestBuilder = requestBuilder.timeout(Duration.ofMillis(requestTimeout)); } if(tryUseCompression){ requestBuilder = requestBuilder.setHeader(ACCEPT_ENCODING_HEADER, GZIP); } Java11ReactiveHttpResponse.ReactiveBodySubscriber bodySubscriber = new Java11ReactiveHttpResponse.ReactiveBodySubscriber(); CompletableFuture<HttpResponse<Void>> response = httpClient.sendAsync( requestBuilder.build(), fromSubscriber(bodySubscriber)); return Mono.fromFuture(response) .<ReactiveHttpResponse>map(resp -> { if(!resp.version().equals(httpClient.version())){ throw new IllegalArgumentException("Incorrect response version:"+resp.version()); } return new Java11ReactiveHttpResponse(resp, bodySubscriber.content(), returnPublisherClass, returnActualClass, jsonFactory, responseReader); }) .onErrorMap(ex -> ex instanceof CompletionException && ex.getCause() instanceof java.net.http.HttpTimeoutException, ReadTimeoutException::new); }
int status = response.statusCode(); var httpHeaders = response.headers(); Headers headers = Headers.ofHttpHeaders(httpHeaders); URL url; try { url = response.uri().toURL(); } catch (MalformedURLException e) { throw new RequestsException(e); var responseInfo = new SimpleResponseInfo(status, headers, response.body()); return new ResponseContext(method, url, responseInfo, interceptors);
/** * Retrieves a specific header with the given name * * @param name The name of the header * @return The value of the header or an empty string if not found */ public String getHeader(String name) { return this.httpResponse.headers().firstValue(name).orElse(""); } }
@Override public Map<String, List<String>> headers() { return clientResponse.headers().map(); }
@Override public int status() { return clientResponse.statusCode(); }