Search in sources :

Example 16 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class RestApiBase method errorResponse.

/**
 * Create an error response.
 * This method attempts to read the response entity as a string, parse it into a JsonObject and
 * depending on result, calls methods to create a proper exception.
 *
 * @param path requested path
 * @param request original request
 * @param method HTTP method
 * @param requestId ID of the request
 * @param response actual response where we do not expect an entity
 * @param <T> type of the response
 *
 * @return future with error
 */
protected <T extends ApiResponse> Single<T> errorResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response) {
    if (response.headers().contentLength().orElse(-1L) == 0) {
        // explicitly no content
        return Single.error(readError(path, request, method, requestId, response));
    } else {
        AtomicBoolean processedError = new AtomicBoolean();
        return response.content().as(String.class).flatMapSingle(string -> {
            try {
                JsonObject json = jsonReaderFactory.createReader(new StringReader(string)).readObject();
                Single<T> error = Single.error(readError(path, request, method, requestId, response, json));
                processedError.set(true);
                return error;
            } catch (Exception e) {
                Single<T> error = Single.error(readError(path, request, method, requestId, response, string));
                processedError.set(true);
                return error;
            }
        }).onErrorResumeWithSingle(it -> {
            if (processedError.get()) {
                return Single.error(it);
            }
            return Single.error(readErrorFailedEntity(path, request, method, requestId, response, it));
        });
    }
}
Also used : JsonWriterFactory(jakarta.json.JsonWriterFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) JsonReaderFactory(jakarta.json.JsonReaderFactory) DataChunk(io.helidon.common.http.DataChunk) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Supplier(java.util.function.Supplier) MediaType(io.helidon.common.http.MediaType) Flow(java.util.concurrent.Flow) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) Http(io.helidon.common.http.Http) Multi(io.helidon.common.reactive.Multi) FtHandler(io.helidon.faulttolerance.FtHandler) Collector(io.helidon.common.reactive.Collector) UUID(java.util.UUID) Logger(java.util.logging.Logger) WebClientRequestHeaders(io.helidon.webclient.WebClientRequestHeaders) Contexts(io.helidon.common.context.Contexts) SpanContext(io.opentracing.SpanContext) StringReader(java.io.StringReader) Optional(java.util.Optional) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Single(io.helidon.common.reactive.Single) StringReader(java.io.StringReader) JsonObject(jakarta.json.JsonObject)

Example 17 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class MicrometerSimplePrometheusTest method checkNoMatch.

@Test
public void checkNoMatch() throws ExecutionException, InterruptedException {
    WebClientResponse response = webClient.get().accept(MediaType.builder().type(MediaType.TEXT_PLAIN.type()).subtype("special").build()).path("/micrometer").request().get();
    assertThat("Expected failed HTTP status", response.status(), is(Http.Status.NOT_ACCEPTABLE_406));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Test(org.junit.jupiter.api.Test)

Example 18 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class MicrometerSimplePrometheusTest method checkViaMediaType.

@Test
public void checkViaMediaType() throws ExecutionException, InterruptedException {
    timer1.record(2L, TimeUnit.SECONDS);
    counter1.increment(3);
    gauge1.set(4);
    WebClientResponse response = webClient.get().accept(MediaType.TEXT_PLAIN).path("/micrometer").request().get();
    assertThat("Unexpected HTTP status", response.status(), is(Http.Status.OK_200));
    String promOutput = response.content().as(String.class).get();
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Test(org.junit.jupiter.api.Test)

Example 19 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class MicrometerSimplePrometheusTest method checkViaQueryParam.

@Test
public void checkViaQueryParam() throws ExecutionException, InterruptedException {
    timer1.record(2L, TimeUnit.SECONDS);
    counter1.increment(3);
    gauge1.set(4);
    WebClientResponse response = webClient.get().accept(MediaType.builder().type(MediaType.TEXT_PLAIN.type()).subtype("special").build()).path("/micrometer").queryParam("type", "prometheus").request().get();
    assertThat("Unexpected HTTP status", response.status(), is(Http.Status.OK_200));
    String promOutput = response.content().as(String.class).get();
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Test(org.junit.jupiter.api.Test)

Example 20 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class OciRestApi method requestJsonPayload.

@Override
protected Supplier<Single<WebClientResponse>> requestJsonPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, JsonObject jsonObject) {
    requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
    requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
    // common stuff
    updateRequestBuilder(requestBuilder, request, requestId);
    // signature requirement
    // this requires a content length and hash
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    jsonWriterFactory().createWriter(baos).write(jsonObject);
    byte[] requestBytes = baos.toByteArray();
    String sha256 = HASH_DIGEST.digest(Base64Value.create(requestBytes)).toBase64();
    requestBuilder.headers(headers -> {
        headers.contentLength(requestBytes.length);
        headers.add("x-content-sha256", sha256);
        return headers;
    }).contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
    return () -> requestBuilder.submit(requestBytes);
}
Also used : ApiRequest(io.helidon.integrations.common.rest.ApiRequest) Security(io.helidon.security.Security) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebClientResponse(io.helidon.webclient.WebClientResponse) BiFunction(java.util.function.BiFunction) DataChunk(io.helidon.common.http.DataChunk) Supplier(java.util.function.Supplier) Level(java.util.logging.Level) MediaType(io.helidon.common.http.MediaType) Base64Value(io.helidon.common.Base64Value) Flow(java.util.concurrent.Flow) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) URI(java.net.URI) Http(io.helidon.common.http.Http) HashDigest(io.helidon.common.crypto.HashDigest) Config(io.helidon.config.Config) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) Logger(java.util.logging.Logger) StandardCharsets(java.nio.charset.StandardCharsets) RestApiBase(io.helidon.integrations.common.rest.RestApiBase) URLEncoder(java.net.URLEncoder) RestApi(io.helidon.integrations.common.rest.RestApi) Version(io.helidon.common.Version) Optional(java.util.Optional) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

WebClientResponse (io.helidon.webclient.WebClientResponse)120 Test (org.junit.jupiter.api.Test)85 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)38 Headers (io.helidon.common.http.Headers)24 WebClient (io.helidon.webclient.WebClient)24 JsonObject (jakarta.json.JsonObject)22 Order (org.junit.jupiter.api.Order)16 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)16 Http (io.helidon.common.http.Http)15 DataChunk (io.helidon.common.http.DataChunk)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Single (io.helidon.common.reactive.Single)7 WebServer (io.helidon.webserver.WebServer)7 Logger (java.util.logging.Logger)7 Context (io.helidon.common.context.Context)6 MediaType (io.helidon.common.http.MediaType)6 URI (java.net.URI)6 Optional (java.util.Optional)6 Contexts (io.helidon.common.context.Contexts)5 Multi (io.helidon.common.reactive.Multi)5