/** * @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(); }
private void upgradeToH2c(Target target){ try { httpClient.send(HttpRequest.newBuilder() .method("options", HttpRequest.BodyPublishers.noBody()) .uri(URI.create(target.url())) .build(), HttpResponse.BodyHandlers.discarding()); } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } } }
private final void init() { this.httpRequest.timeout(Duration.of(TWO_SECONDS, ChronoUnit.SECONDS)); this.httpClient.followRedirects(HttpClient.Redirect.ALWAYS); this.httpClient.cookieHandler(this.cookieManager); }
/** * @return The content type of the response */ public String getContentType() { return this.httpResponse.headers().firstValue(CONTENT_TYPE).orElse(""); }
/** * Sets the ContentType of the request * * @param contentType The content type to use * * @return TestResponse instance */ public TestResponse withContentType(String contentType) { Objects.requireNonNull(contentType, "contentType can not be null"); this.httpRequest.header(CONTENT_TYPE, contentType); return this; }
/** * Disables redirects when the request is executed by setting * followReditects to HttpClient.Redirect.NEVER * * Default is HttpClient.Redirect.ALWAYS * * @return TestResponse instance */ public TestResponse withDisabledRedirects() { this.httpClient.followRedirects(HttpClient.Redirect.NEVER); return this; }
@Override public BodyPublisher asBodyPublisher() { return BodyPublishers.ofByteArray(body()); } }
/** * Construct a headers, from immutable jdk http client headers. * * @param httpHeaders the http headers * @return Headers */ public static Headers ofHttpHeaders(HttpHeaders httpHeaders) { return new Headers(httpHeaders.map()); }
@Override public BodyPublisher asBodyPublisher() { return BodyPublishers.ofInputStream(body()); } }
private static long accumulateLen(List<BodyPublisher> publishers) { int len = 0; for (var publisher : publishers) { if (publisher.contentLength() < 0) { return -1; } len += publisher.contentLength(); } return len; }
/** * Discard all response body. * * @return the response Future */ public CompletableFuture<Response<Void>> discard() { return handle(BodySubscribers.discarding()); }
private AggregateSubscription(BodyPublisher[] publishers, Subscriber<? super ByteBuffer> subscriber) { this.publishers = publishers; this.subscriber = subscriber; this.subscriberAdapter = this.new SubscriberAdapter(); publishers[0].subscribe(subscriberAdapter); }
@Override public void target(Target target) { if(httpClient.version() == HttpClient.Version.HTTP_2){ //preliminary upgrade to h2s and setup TCP connection upgradeToH2c(target); } }
/** * Write response body to file. * * @param path the file path * @return the response future */ public CompletableFuture<Response<Path>> writeTo(Path path) { return handle(BodySubscribers.ofFile(path)); }
/** * Collect the response body to byte array. * * @return the response Future */ public CompletableFuture<Response<byte[]>> toBinaryResponse() { return handle(BodySubscribers.ofByteArray()); }
/** * 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 int status() { return clientResponse.statusCode(); }
@Override protected BodyPublisher asPublisher() { return BodyPublishers.ofInputStream(content()); }