Search in sources :

Example 26 with WebClientRequestBuilder

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

the class SslTest method multipleSslRequestsKeepAlive.

@Test
public void multipleSslRequestsKeepAlive() throws Exception {
    WebClientRequestBuilder requestBuilder = client.get().uri("https://localhost:" + webServer.port());
    // send an entity that won't be consumed, as such a new connection will be created by the server
    requestBuilder.request(String.class).thenAccept(it -> assertThat(it, is("It works!"))).thenCompose(it -> requestBuilder.request(String.class)).thenAccept(it -> assertThat(it, is("It works!"))).toCompletableFuture().get();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) WebClientTls(io.helidon.webclient.WebClientTls) WebClient(io.helidon.webclient.WebClient) BeforeAll(org.junit.jupiter.api.BeforeAll) KeyConfig(io.helidon.common.pki.KeyConfig) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Logger(java.util.logging.Logger) Resource(io.helidon.common.configurable.Resource) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 27 with WebClientRequestBuilder

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

the class TestHttpParseFineTuning method testHeader.

private void testHeader(WebClient client, int size, boolean success) {
    String headerValue = longString(size);
    WebClientRequestBuilder builder = client.get();
    builder.headers().add("X_HEADER", headerValue);
    WebClientResponse response = builder.path("/static/static-content.txt").request().await(10, TimeUnit.SECONDS);
    if (success) {
        assertThat("Header of size " + size + " should have passed", response.status(), is(Http.Status.OK_200));
        assertThat("This request should return content of static-content.txt", response.content().as(String.class).await(10, TimeUnit.SECONDS), is("Hi"));
    } else {
        assertThat("Header of size " + size + " should have failed", response.status(), is(Http.Status.BAD_REQUEST_400));
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Example 28 with WebClientRequestBuilder

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

the class OidcSupport method processCode.

private void processCode(String code, ServerRequest req, ServerResponse res) {
    WebClient webClient = oidcConfig.appWebClient();
    FormParams.Builder form = FormParams.builder().add("grant_type", "authorization_code").add("code", code).add("redirect_uri", redirectUri(req));
    WebClientRequestBuilder post = webClient.post().uri(oidcConfig.tokenEndpointUri()).accept(io.helidon.common.http.MediaType.APPLICATION_JSON);
    oidcConfig.updateRequest(OidcConfig.RequestType.CODE_TO_TOKEN, post, form);
    OidcConfig.postJsonResponse(post, form.build(), json -> processJsonResponse(req, res, json), (status, errorEntity) -> processError(res, status, errorEntity), (t, message) -> processError(res, t, message)).ignoreElement();
}
Also used : Security(io.helidon.security.Security) WebClient(io.helidon.webclient.WebClient) HashMap(java.util.HashMap) Level(java.util.logging.Level) OidcCookieHandler(io.helidon.security.providers.oidc.common.OidcCookieHandler) Matcher(java.util.regex.Matcher) OidcConfig(io.helidon.security.providers.oidc.common.OidcConfig) Map(java.util.Map) FormParams(io.helidon.common.http.FormParams) ServerResponse(io.helidon.webserver.ServerResponse) JsonObject(jakarta.json.JsonObject) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) URI(java.net.URI) Service(io.helidon.webserver.Service) Http(io.helidon.common.http.Http) Config(io.helidon.config.Config) CrossOriginConfig(io.helidon.webserver.cors.CrossOriginConfig) Logger(java.util.logging.Logger) ServerRequest(io.helidon.webserver.ServerRequest) List(java.util.List) ResponseHeaders(io.helidon.webserver.ResponseHeaders) CorsSupport(io.helidon.webserver.cors.CorsSupport) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Routing(io.helidon.webserver.Routing) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) FormParams(io.helidon.common.http.FormParams) WebClient(io.helidon.webclient.WebClient) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Example 29 with WebClientRequestBuilder

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

the class IdcsRoleMapperRxProvider method getGrantsFromServer.

/**
 * Retrieves grants from IDCS server.
 *
 * @param subject to get grants for
 * @return optional list of grants to be added
 */
protected Single<List<? extends Grant>> getGrantsFromServer(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(appToken.getToken(tracing)).flatMapSingle(maybeAppToken -> {
        if (maybeAppToken.isEmpty()) {
            return Single.error(new SecurityException("Application token not available"));
        }
        String appToken = maybeAppToken.get();
        JsonObjectBuilder requestBuilder = JSON.createObjectBuilder().add("mappingAttributeValue", subjectName).add("subjectType", subjectType).add("includeMemberships", true);
        JsonArrayBuilder arrayBuilder = JSON.createArrayBuilder();
        arrayBuilder.add("urn:ietf:params:scim:schemas:oracle:idcs:Asserter");
        requestBuilder.add("schemas", arrayBuilder);
        // use current span context as a parent for client outbound
        // using a custom child context, so we do not replace the parent in the current context
        Context parentContext = Contexts.context().orElseGet(Contexts::globalContext);
        Context childContext = Context.builder().parent(parentContext).build();
        tracing.findParent().ifPresent(childContext::register);
        WebClientRequestBuilder request = oidcConfig().generalWebClient().post().uri(asserterUri).context(childContext).headers(it -> {
            it.add(Http.Header.AUTHORIZATION, "Bearer " + appToken);
            return it;
        });
        return processRoleRequest(request, requestBuilder.build(), subjectName);
    }).peek(ignored -> tracing.finish()).onError(tracing::error);
}
Also used : Context(io.helidon.common.context.Context) ProviderRequest(io.helidon.security.ProviderRequest) 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) SubjectMappingProvider(io.helidon.security.spi.SubjectMappingProvider) SecurityProvider(io.helidon.security.spi.SecurityProvider) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) AuthenticationResponse(io.helidon.security.AuthenticationResponse) Contexts(io.helidon.common.context.Contexts) Json(jakarta.json.Json) 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) 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 30 with WebClientRequestBuilder

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

the class TestHandlerRegistration method test4PreFlightAllowedHeaders2.

@Test
void test4PreFlightAllowedHeaders2() throws ExecutionException, InterruptedException {
    WebClientRequestBuilder reqBuilder = client.options().path(CORS4_CONTEXT_ROOT);
    Headers headers = reqBuilder.headers();
    headers.add(ORIGIN, "http://foo.bar");
    headers.add(ACCESS_CONTROL_REQUEST_METHOD, "PUT");
    headers.add(ACCESS_CONTROL_REQUEST_HEADERS, "X-foo, X-bar");
    WebClientResponse res = reqBuilder.request().toCompletableFuture().get();
    assertThat(res.status(), is(Http.Status.OK_200));
    assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_ORIGIN), present(is("http://foo.bar")));
    assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_METHODS), present(is("PUT")));
    assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_HEADERS), present(containsString("X-foo")));
    assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_HEADERS), present(containsString("X-bar")));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) 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