Search in sources :

Example 1 with Parameters

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

the class Main method parametersAndHeaders.

/**
 * {@link io.helidon.webserver.ServerRequest ServerRequest} provides access to three types of "parameters":
 * <ul>
 *     <li>Headers</li>
 *     <li>Query parameters</li>
 *     <li>Path parameters - <i>Evaluated from provided {@code path pattern}</i></li>
 * </ul>
 * <p>
 * {@link java.util.Optional Optional} API is heavily used to represent parameters optionality.
 * <p>
 * WebServer {@link Parameters Parameters} API is used to represent fact, that <i>headers</i> and
 * <i>query parameters</i> can contain multiple values.
 */
public void parametersAndHeaders() {
    Routing routing = Routing.builder().get("/context/{id}", (req, res) -> {
        StringBuilder sb = new StringBuilder();
        // Request headers
        req.headers().first("foo").ifPresent(v -> sb.append("foo: ").append(v).append("\n"));
        // Request parameters
        req.queryParams().first("bar").ifPresent(v -> sb.append("bar: ").append(v).append("\n"));
        // Path parameters
        sb.append("id: ").append(req.path().param("id"));
        // Response headers
        res.headers().contentType(MediaType.TEXT_PLAIN);
        // Response entity (payload)
        res.send(sb.toString());
    }).build();
    startServer(routing);
}
Also used : JerseySupport(io.helidon.webserver.jersey.JerseySupport) DataChunk(io.helidon.common.http.DataChunk) RequestPredicate(io.helidon.webserver.RequestPredicate) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) MediaContext(io.helidon.media.common.MediaContext) MessageBodyReader(io.helidon.media.common.MessageBodyReader) InvocationTargetException(java.lang.reflect.InvocationTargetException) MediaType(io.helidon.common.http.MediaType) Json(jakarta.json.Json) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Handler(io.helidon.webserver.Handler) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Modifier(java.lang.reflect.Modifier) Parameters(io.helidon.common.http.Parameters) HttpException(io.helidon.webserver.HttpException) WebServer(io.helidon.webserver.WebServer) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) Method(java.lang.reflect.Method) Collections(java.util.Collections) Routing(io.helidon.webserver.Routing)

Example 2 with Parameters

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

the class GraphQlSupport method graphQlGet.

// handle GET request for GraphQL endpoint
private void graphQlGet(ServerRequest req, ServerResponse res) {
    Parameters queryParams = req.queryParams();
    String query = queryParams.first("query").orElseThrow(() -> new IllegalStateException("Query must be defined"));
    String operationName = queryParams.first("operationName").orElse(null);
    Map<String, Object> variables = queryParams.first("variables").map(this::toVariableMap).orElseGet(Map::of);
    processRequest(res, query, operationName, variables);
}
Also used : Parameters(io.helidon.common.http.Parameters) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with Parameters

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

the class WebSecurityQueryParamTest method testQueryParams.

@Test
public void testQueryParams() {
    SecurityHandler securityHandler = SecurityHandler.create().queryParam("jwt", TokenHandler.builder().tokenHeader("BEARER_TOKEN").tokenPattern(Pattern.compile("bearer (.*)")).build()).queryParam("name", TokenHandler.builder().tokenHeader("NAME_FROM_REQUEST").build());
    ServerRequest req = Mockito.mock(ServerRequest.class);
    Parameters params = Mockito.mock(Parameters.class);
    when(params.all("jwt")).thenReturn(List.of("bearer jwt_content"));
    when(params.all("name")).thenReturn(List.of("name_content"));
    when(req.queryParams()).thenReturn(params);
    SecurityContext context = Mockito.mock(SecurityContext.class);
    SecurityEnvironment env = SecurityEnvironment.create();
    when(context.env()).thenReturn(env);
    // context is a stub
    securityHandler.extractQueryParams(context, req);
    // captor captures the argument
    ArgumentCaptor<SecurityEnvironment> newHeaders = ArgumentCaptor.forClass(SecurityEnvironment.class);
    verify(context).env(newHeaders.capture());
    // now validate the value we were called with
    env = newHeaders.getValue();
    assertThat(env.headers().get("BEARER_TOKEN"), is(List.of("jwt_content")));
    assertThat(env.headers().get("NAME_FROM_REQUEST"), is(List.of("name_content")));
}
Also used : Parameters(io.helidon.common.http.Parameters) SecurityEnvironment(io.helidon.security.SecurityEnvironment) SecurityContext(io.helidon.security.SecurityContext) ServerRequest(io.helidon.webserver.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 4 with Parameters

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

the class HashRequestHeaders method cookies.

@Override
public Parameters cookies() {
    Parameters lCookies = this.cookies;
    if (lCookies == null) {
        synchronized (internalLock) {
            lCookies = this.cookies;
            if (lCookies == null) {
                List<Parameters> list = all(Http.Header.COOKIE).stream().map(CookieParser::parse).collect(Collectors.toList());
                lCookies = Parameters.toUnmodifiableParameters(HashParameters.concat(list));
                this.cookies = lCookies;
            }
        }
    }
    return lCookies;
}
Also used : HashParameters(io.helidon.common.http.HashParameters) Parameters(io.helidon.common.http.Parameters) ReadOnlyParameters(io.helidon.common.http.ReadOnlyParameters)

Example 5 with Parameters

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

the class UriComponentTest method noDecode.

@Test
public void noDecode() throws Exception {
    Parameters parameters = UriComponent.decodeQuery("a=" + URLEncoder.encode("1&b=2", US_ASCII.name()), false);
    assertThat(parameters.first("a").get(), is(URLEncoder.encode("1&b=2", US_ASCII.name())));
}
Also used : Parameters(io.helidon.common.http.Parameters) Test(org.junit.jupiter.api.Test)

Aggregations

Parameters (io.helidon.common.http.Parameters)16 Test (org.junit.jupiter.api.Test)11 HashParameters (io.helidon.common.http.HashParameters)3 HaInfo (com.sun.xml.ws.api.ha.HaInfo)1 DataChunk (io.helidon.common.http.DataChunk)1 Http (io.helidon.common.http.Http)1 MediaType (io.helidon.common.http.MediaType)1 ReadOnlyParameters (io.helidon.common.http.ReadOnlyParameters)1 MediaContext (io.helidon.media.common.MediaContext)1 MessageBodyReader (io.helidon.media.common.MessageBodyReader)1 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)1 SecurityContext (io.helidon.security.SecurityContext)1 SecurityEnvironment (io.helidon.security.SecurityEnvironment)1 Handler (io.helidon.webserver.Handler)1 HttpException (io.helidon.webserver.HttpException)1 RequestPredicate (io.helidon.webserver.RequestPredicate)1 Routing (io.helidon.webserver.Routing)1 ServerRequest (io.helidon.webserver.ServerRequest)1 WebServer (io.helidon.webserver.WebServer)1 JerseySupport (io.helidon.webserver.jersey.JerseySupport)1