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);
}
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);
}
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")));
}
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;
}
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())));
}
Aggregations