Search in sources :

Example 6 with WebClientRequestBuilder

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

the class RestApiBase method invokeBytesRequest.

@Override
public <T extends ApiResponse> Single<T> invokeBytesRequest(Http.RequestMethod method, String path, ApiRequest<?> request, Flow.Publisher<DataChunk> byteRequest, ApiResponse.Builder<?, T> responseBuilder) {
    String requestId = requestId(request);
    LOGGER.finest(() -> requestId + ": Invoking " + method + " on path " + path + " with bytes request");
    WebClientRequestBuilder requestBuilder = webClient.method(method).path(path);
    addHeaders(requestBuilder, path, request, method, requestId);
    addQueryParams(requestBuilder, path, request, method, requestId);
    Supplier<Single<WebClientResponse>> responseSupplier = requestBytesPayload(path, request, method, requestId, requestBuilder, byteRequest);
    return ftHandler.invoke(responseSupplier).flatMapSingle(response -> handleResponse(path, request, method, requestId, response, responseBuilder));
}
Also used : Single(io.helidon.common.reactive.Single) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Example 7 with WebClientRequestBuilder

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

the class RestApiBase method requestBytesPayload.

/**
 * Create a supplier for a response with publisher request.
 * Defaults to "{@code () -> clientRequest.submit(publisher)}".
 * Also configures content type and accept headers.
 *
 * @param path path requested
 * @param request API request
 * @param method HTTP method
 * @param requestId ID of this request
 * @param requestBuilder {@link io.helidon.webclient.WebClient} request builder
 * @param publisher publisher to be used as request entity
 *
 * @return supplier of a web client response
 */
protected Supplier<Single<WebClientResponse>> requestBytesPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, Flow.Publisher<DataChunk> publisher) {
    requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
    requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_OCTET_STREAM));
    AtomicBoolean updated = new AtomicBoolean();
    return () -> {
        if (updated.compareAndSet(false, true)) {
            return updateRequestBuilderBytesPayload(requestBuilder, path, request, method, requestId).flatMapSingle(it -> it.submit(publisher));
        } else {
            return requestBuilder.submit(publisher);
        }
    };
}
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)

Example 8 with WebClientRequestBuilder

use of io.helidon.webclient.WebClientRequestBuilder 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)

Example 9 with WebClientRequestBuilder

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

the class MainTest method testNamedGreetWithCors.

// Run after CORS test changes greeting to Cheers.
@Order(12)
@Test
void testNamedGreetWithCors() {
    WebClientRequestBuilder builder = webClient.get();
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    WebClientResponse r = getResponse("/greet/Maria", builder);
    assertEquals(200, r.status().code(), "HTTP response");
    assertTrue(fromPayload(r).getMessage().contains("Cheers Maria"));
    headers = r.headers();
    Optional<String> allowOrigin = headers.value(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertTrue(allowOrigin.isPresent(), "Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " is absent");
    assertEquals(allowOrigin.get(), "*");
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 10 with WebClientRequestBuilder

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

the class MainTest method testGreetingChangeWithCorsAndOtherOrigin.

// After all other tests so we can rely on deterministic greetings.
@Order(100)
@Test
void testGreetingChangeWithCorsAndOtherOrigin() {
    WebClientRequestBuilder builder = webClient.put();
    Headers headers = builder.headers();
    headers.add("Origin", "http://other.com");
    headers.add("Host", "here.com");
    WebClientResponse r = putResponse("/greet/greeting", new GreetingMessage("Ahoy"), builder);
    // Result depends on whether we are using overrides or not.
    boolean isOverriding = Config.create().get("cors").exists();
    assertEquals(isOverriding ? 204 : 403, r.status().code(), "HTTP response3");
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)50 WebClientResponse (io.helidon.webclient.WebClientResponse)37 Test (org.junit.jupiter.api.Test)34 Headers (io.helidon.common.http.Headers)24 Http (io.helidon.common.http.Http)9 WebClient (io.helidon.webclient.WebClient)9 JsonObject (jakarta.json.JsonObject)8 Order (org.junit.jupiter.api.Order)8 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)8 Single (io.helidon.common.reactive.Single)7 Logger (java.util.logging.Logger)7 Optional (java.util.Optional)6 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)5 Contexts (io.helidon.common.context.Contexts)4 Config (io.helidon.config.Config)4 URI (java.net.URI)4 DataChunk (io.helidon.common.http.DataChunk)3 MediaType (io.helidon.common.http.MediaType)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Flow (java.util.concurrent.Flow)3