Search in sources :

Example 16 with ServerResponse

use of io.helidon.webserver.ServerResponse in project helidon by oracle.

the class TodosHandler method getAll.

/**
 * Handler for {@code GET /todo}.
 *
 * @param req the server request
 * @param res the server response
 */
private void getAll(final ServerRequest req, final ServerResponse res) {
    AtomicReference<Span> createdSpan = new AtomicReference<>();
    SpanContext spanContext = req.spanContext().orElseGet(() -> {
        Span mySpan = req.tracer().buildSpan("getAll").start();
        createdSpan.set(mySpan);
        return mySpan.context();
    });
    secure(req, res, sc -> {
        bsc.getAll(spanContext).thenAccept(res::send).exceptionally(t -> sendError(t, res)).whenComplete((noting, throwable) -> {
            Span mySpan = createdSpan.get();
            if (null != mySpan) {
                mySpan.finish();
            }
        });
    });
}
Also used : Metadata(org.eclipse.microprofile.metrics.Metadata) MetricType(org.eclipse.microprofile.metrics.MetricType) MetricUnits(org.eclipse.microprofile.metrics.MetricUnits) SecurityContext(io.helidon.security.SecurityContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerRequest(io.helidon.webserver.ServerRequest) SpanContext(io.opentracing.SpanContext) Consumer(java.util.function.Consumer) Counter(org.eclipse.microprofile.metrics.Counter) ServerResponse(io.helidon.webserver.ServerResponse) JsonObject(jakarta.json.JsonObject) Optional(java.util.Optional) Span(io.opentracing.Span) Service(io.helidon.webserver.Service) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) RegistryFactory(io.helidon.metrics.RegistryFactory) SpanContext(io.opentracing.SpanContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Span(io.opentracing.Span)

Example 17 with ServerResponse

use of io.helidon.webserver.ServerResponse in project helidon by oracle.

the class OidcSupport method processJsonResponse.

private String processJsonResponse(ServerRequest req, ServerResponse res, JsonObject json) {
    String tokenValue = json.getString("access_token");
    String idToken = json.getString("id_token", null);
    // redirect to "state"
    String state = req.queryParams().first(STATE_PARAM_NAME).orElse(DEFAULT_REDIRECT);
    res.status(Http.Status.TEMPORARY_REDIRECT_307);
    if (oidcConfig.useParam()) {
        state = (state.contains("?") ? "&" : "?") + oidcConfig.paramName() + "=" + tokenValue;
    }
    state = increaseRedirectCounter(state);
    res.headers().add(Http.Header.LOCATION, state);
    if (oidcConfig.useCookie()) {
        ResponseHeaders headers = res.headers();
        tokenCookieHandler.createCookie(tokenValue).forSingle(builder -> {
            headers.addCookie(builder.build());
            if (idToken != null && oidcConfig.logoutEnabled()) {
                idTokenCookieHandler.createCookie(idToken).forSingle(it -> {
                    headers.addCookie(it.build());
                    res.send();
                }).exceptionallyAccept(t -> sendError(res, t));
            } else {
                res.send();
            }
        }).exceptionallyAccept(t -> sendError(res, t));
    } else {
        res.send();
    }
    return "done";
}
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) ResponseHeaders(io.helidon.webserver.ResponseHeaders)

Example 18 with ServerResponse

use of io.helidon.webserver.ServerResponse 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 19 with ServerResponse

use of io.helidon.webserver.ServerResponse in project helidon by oracle.

the class StaticContentHandlerTest method handleValid.

@Test
void handleValid() {
    ServerRequest request = mockRequestWithPath("/foo/some.txt");
    ServerResponse response = mock(ServerResponse.class);
    TestContentHandler handler = TestContentHandler.create(true);
    handler.handle(Http.Method.GET, request, response);
    verify(request, never()).next();
    assertThat(handler.path, is(Paths.get("foo/some.txt").toAbsolutePath().normalize()));
}
Also used : ServerResponse(io.helidon.webserver.ServerResponse) ServerRequest(io.helidon.webserver.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 20 with ServerResponse

use of io.helidon.webserver.ServerResponse in project helidon by oracle.

the class StaticContentHandlerTest method handleNextOnFalse.

@Test
void handleNextOnFalse() {
    ServerRequest request = mockRequestWithPath("/");
    ServerResponse response = mock(ServerResponse.class);
    TestContentHandler handler = TestContentHandler.create(false);
    handler.handle(Http.Method.GET, request, response);
    verify(request).next();
    assertThat(handler.counter.get(), is(1));
}
Also used : ServerResponse(io.helidon.webserver.ServerResponse) ServerRequest(io.helidon.webserver.ServerRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ServerResponse (io.helidon.webserver.ServerResponse)37 ServerRequest (io.helidon.webserver.ServerRequest)36 Routing (io.helidon.webserver.Routing)23 Logger (java.util.logging.Logger)18 JsonObject (jakarta.json.JsonObject)13 Config (io.helidon.config.Config)12 Map (java.util.Map)12 Service (io.helidon.webserver.Service)11 Json (jakarta.json.Json)11 Optional (java.util.Optional)11 Single (io.helidon.common.reactive.Single)10 DbClient (io.helidon.dbclient.DbClient)10 Test (org.junit.jupiter.api.Test)10 Http (io.helidon.common.http.Http)9 Pokemon (io.helidon.tests.integration.dbclient.appl.model.Pokemon)9 AppResponse (io.helidon.tests.integration.tools.service.AppResponse)9 RemoteTestException (io.helidon.tests.integration.tools.service.RemoteTestException)9 List (java.util.List)9 SecurityContext (io.helidon.security.SecurityContext)8 AbstractService (io.helidon.tests.integration.dbclient.appl.AbstractService)8