Search in sources :

Example 11 with WebClientRequestBuilder

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

the class HelidonConnector method applyInternal.

private CompletionStage<ClientResponse> applyInternal(ClientRequest request) {
    final WebClientRequestBuilder webClientRequestBuilder = webClient.method(request.getMethod());
    webClientRequestBuilder.uri(request.getUri());
    webClientRequestBuilder.headers(HelidonStructures.createHeaders(request.getRequestHeaders()));
    for (String propertyName : request.getConfiguration().getPropertyNames()) {
        Object property = request.getConfiguration().getProperty(propertyName);
        if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
            webClientRequestBuilder.property(propertyName, (String) property);
        }
    }
    for (String propertyName : request.getPropertyNames()) {
        Object property = request.resolveProperty(propertyName, Object.class);
        if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
            webClientRequestBuilder.property(propertyName, (String) property);
        }
    }
    // TODO
    // HelidonStructures.createProxy(request).ifPresent(webClientRequestBuilder::proxy);
    webClientRequestBuilder.followRedirects(request.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
    webClientRequestBuilder.readTimeout(request.resolveProperty(ClientProperties.READ_TIMEOUT, 10000), TimeUnit.MILLISECONDS);
    CompletionStage<WebClientResponse> responseStage = null;
    if (request.hasEntity()) {
        responseStage = HelidonEntity.submit(entityType, request, webClientRequestBuilder, executorServiceKeeper.getExecutorService(request));
    } else {
        responseStage = webClientRequestBuilder.submit();
    }
    return responseStage.thenCompose((a) -> convertResponse(request, a));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Example 12 with WebClientRequestBuilder

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

the class IdcsMtRoleMapperRxProvider method getGrantsFromServer.

/**
 * Get grants from IDCS server. The result is cached.
 *
 * @param idcsTenantId ID of the IDCS tenant
 * @param idcsAppName  Name of IDCS application
 * @param subject      subject to get grants for
 * @return optional list of grants from server
 */
protected Single<List<? extends Grant>> getGrantsFromServer(String idcsTenantId, String idcsAppName, Subject subject) {
    String subjectName = subject.principal().getName();
    String subjectType = (String) subject.principal().abacAttribute("sub_type").orElse(defaultIdcsSubjectType());
    RoleMapTracing tracing = SecurityTracing.get().roleMapTracing("idcs");
    return Single.create(getAppToken(idcsTenantId, tracing)).flatMapSingle(maybeAppToken -> {
        if (maybeAppToken.isEmpty()) {
            return Single.error(new SecurityException("Application token not available"));
        }
        return Single.just(maybeAppToken.get());
    }).flatMapSingle(appToken -> {
        JsonObjectBuilder requestBuilder = JSON.createObjectBuilder().add("mappingAttributeValue", subjectName).add("subjectType", subjectType).add("appName", idcsAppName).add("includeMemberships", true);
        JsonArrayBuilder arrayBuilder = JSON.createArrayBuilder();
        arrayBuilder.add("urn:ietf:params:scim:schemas:oracle:idcs:Asserter");
        requestBuilder.add("schemas", arrayBuilder);
        Context parentContext = Contexts.context().orElseGet(Contexts::globalContext);
        Context childContext = Context.builder().parent(parentContext).build();
        tracing.findParent().ifPresent(childContext::register);
        WebClientRequestBuilder post = oidcConfig().generalWebClient().post().context(childContext).uri(multitenantEndpoints.assertEndpoint(idcsTenantId)).headers(it -> {
            it.add(Http.Header.AUTHORIZATION, "Bearer " + appToken);
            return it;
        });
        return processRoleRequest(post, requestBuilder.build(), subjectName);
    });
}
Also used : ProviderRequest(io.helidon.security.ProviderRequest) WebClient(io.helidon.webclient.WebClient) Context(io.helidon.common.context.Context) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) SecurityException(io.helidon.security.SecurityException) OidcConfig(io.helidon.security.providers.oidc.common.OidcConfig) EvictableCache(io.helidon.security.providers.common.EvictableCache) Single(io.helidon.common.reactive.Single) Grant(io.helidon.security.Grant) Subject(io.helidon.security.Subject) URI(java.net.URI) LinkedList(java.util.LinkedList) Http(io.helidon.common.http.Http) SecurityTracing(io.helidon.security.integration.common.SecurityTracing) Config(io.helidon.config.Config) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SecurityProvider(io.helidon.security.spi.SecurityProvider) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) TokenHandler(io.helidon.security.util.TokenHandler) Logger(java.util.logging.Logger) AuthenticationResponse(io.helidon.security.AuthenticationResponse) Contexts(io.helidon.common.context.Contexts) Json(jakarta.json.Json) Objects(java.util.Objects) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) List(java.util.List) Optional(java.util.Optional) RoleMapTracing(io.helidon.security.integration.common.RoleMapTracing) Collections(java.util.Collections) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Context(io.helidon.common.context.Context) RoleMapTracing(io.helidon.security.integration.common.RoleMapTracing) SecurityException(io.helidon.security.SecurityException) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) Contexts(io.helidon.common.context.Contexts) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Example 13 with WebClientRequestBuilder

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

the class TestClient method callServiceAndGetString.

/**
 * Call remote service method and return its raw data as JSON object.
 * No response content check is done. No query parameters are passed.
 *
 * @param service remote service name
 * @param method remote test method name
 * @return data returned by remote service
 */
public String callServiceAndGetString(final String service, final String method) {
    WebClientRequestBuilder rb = clientGetBuilderWithPath(service, method);
    final MessageBodyReadableContent content = rb.submit().await(1, TimeUnit.MINUTES).content();
    return content.as(String.class).await(1, TimeUnit.MINUTES);
}
Also used : WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent)

Example 14 with WebClientRequestBuilder

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

the class RequestTest method reuseRequestBuilder.

@Test
public void reuseRequestBuilder() {
    WebClientRequestBuilder requestBuilder = webClient.get();
    JsonObject response = requestBuilder.request(JsonObject.class).await();
    assertThat(response, notNullValue());
    assertThat(response.getString("message"), is("Hello World!"));
    response = requestBuilder.request(JsonObject.class).await();
    assertThat(response, notNullValue());
    assertThat(response.getString("message"), is("Hello World!"));
}
Also used : JsonObject(jakarta.json.JsonObject) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 15 with WebClientRequestBuilder

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

the class TestDefaultCorsSupport method testOptionsWithCors.

@Test
void testOptionsWithCors() throws ExecutionException, InterruptedException {
    WebServer server = null;
    WebClient client;
    try {
        server = WebServer.create(prepRouting(true)).start().toCompletableFuture().get();
        client = WebClient.builder().baseUri("http://localhost:" + server.port()).get();
        WebClientRequestBuilder reqBuilder = client.options().path("/greet");
        Headers h = reqBuilder.headers();
        h.add("Origin", "http://foo.com");
        h.add("Host", "bar.com");
        WebClientResponse response = reqBuilder.submit().toCompletableFuture().get();
        WebClientResponseHeaders headers = response.headers();
        List<String> allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
        assertThat(allowOrigins, contains("*"));
    } finally {
        if (server != null) {
            server.shutdown();
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) WebClientResponseHeaders(io.helidon.webclient.WebClientResponseHeaders) Headers(io.helidon.common.http.Headers) WebClient(io.helidon.webclient.WebClient) WebClientResponseHeaders(io.helidon.webclient.WebClientResponseHeaders) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) 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