Search in sources :

Example 1 with FormParams

use of io.helidon.common.http.FormParams in project helidon by oracle.

the class FormParamsBodyReader method create.

private FormParams create(String paramAssignments, MediaType mediaType, Function<String, String> decoder) {
    FormParams.Builder builder = FormParams.builder();
    Matcher m = PATTERNS.get(mediaType).matcher(paramAssignments);
    while (m.find()) {
        final String key = m.group(1);
        final String value = m.group(2);
        if (value == null) {
            builder.add(decoder.apply(key));
        } else {
            builder.add(decoder.apply(key), decoder.apply(value));
        }
    }
    return builder.build();
}
Also used : FormParams(io.helidon.common.http.FormParams) Matcher(java.util.regex.Matcher)

Example 2 with FormParams

use of io.helidon.common.http.FormParams in project helidon by oracle.

the class FormParamsBodyReader method read.

@Override
@SuppressWarnings("unchecked")
public <U extends FormParams> Single<U> read(Flow.Publisher<DataChunk> publisher, GenericType<U> type, MessageBodyReaderContext context) {
    MediaType mediaType = context.contentType().orElseThrow();
    Charset charset = mediaType.charset().map(Charset::forName).orElse(StandardCharsets.UTF_8);
    Function<String, String> decoder = decoder(mediaType, charset);
    return (Single<U>) ContentReaders.readString(publisher, charset).map(formStr -> create(formStr, mediaType, decoder));
}
Also used : URLDecoder(java.net.URLDecoder) DataChunk(io.helidon.common.http.DataChunk) GenericType(io.helidon.common.GenericType) Function(java.util.function.Function) StandardCharsets(java.nio.charset.StandardCharsets) MediaType(io.helidon.common.http.MediaType) Matcher(java.util.regex.Matcher) Charset(java.nio.charset.Charset) Flow(java.util.concurrent.Flow) Map(java.util.Map) FormParams(io.helidon.common.http.FormParams) Single(io.helidon.common.reactive.Single) Pattern(java.util.regex.Pattern) Single(io.helidon.common.reactive.Single) MediaType(io.helidon.common.http.MediaType) Charset(java.nio.charset.Charset)

Example 3 with FormParams

use of io.helidon.common.http.FormParams in project helidon by oracle.

the class IdcsSupport method signJwk.

// load signature jwk with a token, blocking operation
static JwkKeys signJwk(WebClient appWebClient, WebClient generalClient, URI tokenEndpointUri, URI signJwkUri, Duration clientTimeout) {
    // need to get token to be able to request this endpoint
    FormParams form = FormParams.builder().add("grant_type", "client_credentials").add("scope", "urn:opc:idm:__myscopes__").build();
    try {
        WebClientResponse response = appWebClient.post().uri(tokenEndpointUri).accept(MediaType.APPLICATION_JSON).submit(form).await(clientTimeout.toMillis(), TimeUnit.MILLISECONDS);
        if (response.status().family() == Http.ResponseStatus.Family.SUCCESSFUL) {
            JsonObject json = response.content().as(JsonObject.class).await(clientTimeout.toMillis(), TimeUnit.MILLISECONDS);
            String accessToken = json.getString("access_token");
            // get the jwk from server
            JsonObject jwkJson = generalClient.get().uri(signJwkUri).headers(it -> {
                it.add(Http.Header.AUTHORIZATION, "Bearer " + accessToken);
                return it;
            }).request(JsonObject.class).await(clientTimeout.toMillis(), TimeUnit.MILLISECONDS);
            return JwkKeys.create(jwkJson);
        } else {
            String errorEntity = response.content().as(String.class).await(clientTimeout.toMillis(), TimeUnit.MILLISECONDS);
            throw new SecurityException("Failed to read JWK from IDCS. Status: " + response.status() + ", entity: " + errorEntity);
        }
    } catch (SecurityException e) {
        throw e;
    } catch (Exception e) {
        throw new SecurityException("Failed to read JWK from IDCS", e);
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) FormParams(io.helidon.common.http.FormParams) JsonObject(jakarta.json.JsonObject) SecurityException(io.helidon.security.SecurityException) SecurityException(io.helidon.security.SecurityException)

Example 4 with FormParams

use of io.helidon.common.http.FormParams in project helidon by oracle.

the class FormTest method testFormContent.

@Test
public void testFormContent() {
    FormParams received = webClient.post().path("/form/content").submit(ADVANCED_TEST_FORM, FormParams.class).await();
    assertThat(received.all(SPECIAL), is(ADVANCED_TEST_FORM.all(SPECIAL)));
    assertThat(received.all(MULTIPLE), is(ADVANCED_TEST_FORM.all(MULTIPLE)));
    assertThat(received.all(NO_VALUE).size(), is(0));
}
Also used : FormParams(io.helidon.common.http.FormParams) Test(org.junit.jupiter.api.Test)

Aggregations

FormParams (io.helidon.common.http.FormParams)4 Matcher (java.util.regex.Matcher)2 GenericType (io.helidon.common.GenericType)1 DataChunk (io.helidon.common.http.DataChunk)1 MediaType (io.helidon.common.http.MediaType)1 Single (io.helidon.common.reactive.Single)1 SecurityException (io.helidon.security.SecurityException)1 WebClientResponse (io.helidon.webclient.WebClientResponse)1 JsonObject (jakarta.json.JsonObject)1 URLDecoder (java.net.URLDecoder)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Map (java.util.Map)1 Flow (java.util.concurrent.Flow)1 Function (java.util.function.Function)1 Pattern (java.util.regex.Pattern)1 Test (org.junit.jupiter.api.Test)1