Search in sources :

Example 1 with ServerResponse

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

the class TranslatorFrontendService method getText.

private void getText(ServerRequest request, ServerResponse response) {
    try {
        String query = request.queryParams().first("q").orElseThrow(() -> new BadRequestException("missing query parameter 'q'"));
        String language = request.queryParams().first("lang").orElseThrow(() -> new BadRequestException("missing query parameter 'lang'"));
        try (Response backendResponse = backendTarget.property(ClientTracingFilter.TRACER_PROPERTY_NAME, request.tracer()).property(ClientTracingFilter.CURRENT_SPAN_CONTEXT_PROPERTY_NAME, request.spanContext().orElse(null)).queryParam("q", query).queryParam("lang", language).request().get()) {
            final String result;
            if (backendResponse.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
                result = backendResponse.readEntity(String.class);
            } else {
                result = "Error: " + backendResponse.readEntity(String.class);
            }
            response.send(result + "\n");
        }
    } catch (ProcessingException pe) {
        LOGGER.log(Level.WARNING, "Problem to call translator frontend.", pe);
        response.status(503).send("Translator backend service isn't available.");
    }
}
Also used : Response(jakarta.ws.rs.core.Response) ServerResponse(io.helidon.webserver.ServerResponse) BadRequestException(io.helidon.webserver.BadRequestException) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 2 with ServerResponse

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

the class OutboundOverrideExample method propagate.

private static void propagate(ServerRequest req, ServerResponse res) {
    SecurityContext context = getSecurityContext(req);
    webTarget(servingPort).request(String.class).thenAccept(result -> res.send("You are: " + context.userName() + ", backend service returned: " + result + "\n")).exceptionally(throwable -> sendError(throwable, res));
}
Also used : OutboundOverrideUtil.startServer(io.helidon.security.examples.outbound.OutboundOverrideUtil.startServer) Config(io.helidon.config.Config) OutboundOverrideUtil.sendError(io.helidon.security.examples.outbound.OutboundOverrideUtil.sendError) SecurityContext(io.helidon.security.SecurityContext) Principal(io.helidon.security.Principal) ServerRequest(io.helidon.webserver.ServerRequest) OutboundOverrideUtil.getSecurityContext(io.helidon.security.examples.outbound.OutboundOverrideUtil.getSecurityContext) CompletionStage(java.util.concurrent.CompletionStage) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Subject(io.helidon.security.Subject) Routing(io.helidon.webserver.Routing) OutboundOverrideUtil.createConfig(io.helidon.security.examples.outbound.OutboundOverrideUtil.createConfig) OutboundOverrideUtil.webTarget(io.helidon.security.examples.outbound.OutboundOverrideUtil.webTarget) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) SecurityContext(io.helidon.security.SecurityContext) OutboundOverrideUtil.getSecurityContext(io.helidon.security.examples.outbound.OutboundOverrideUtil.getSecurityContext)

Example 3 with ServerResponse

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

the class OutboundOverrideJwtExample method propagate.

private static void propagate(ServerRequest req, ServerResponse res) {
    SecurityContext context = getSecurityContext(req);
    webTarget(servingPort).request(String.class).thenAccept(result -> res.send("You are: " + context.userName() + ", backend service returned: " + result)).exceptionally(throwable -> sendError(throwable, res));
}
Also used : OutboundOverrideUtil.startServer(io.helidon.security.examples.outbound.OutboundOverrideUtil.startServer) Config(io.helidon.config.Config) OutboundOverrideUtil.sendError(io.helidon.security.examples.outbound.OutboundOverrideUtil.sendError) SecurityContext(io.helidon.security.SecurityContext) Principal(io.helidon.security.Principal) ServerRequest(io.helidon.webserver.ServerRequest) OutboundOverrideUtil.getSecurityContext(io.helidon.security.examples.outbound.OutboundOverrideUtil.getSecurityContext) CompletionStage(java.util.concurrent.CompletionStage) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Subject(io.helidon.security.Subject) Routing(io.helidon.webserver.Routing) OutboundOverrideUtil.createConfig(io.helidon.security.examples.outbound.OutboundOverrideUtil.createConfig) OutboundOverrideUtil.webTarget(io.helidon.security.examples.outbound.OutboundOverrideUtil.webTarget) JwtProvider(io.helidon.security.providers.jwt.JwtProvider) SecurityContext(io.helidon.security.SecurityContext) OutboundOverrideUtil.getSecurityContext(io.helidon.security.examples.outbound.OutboundOverrideUtil.getSecurityContext)

Example 4 with ServerResponse

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

the class OutboundOverrideJwtExample method override.

private static void override(ServerRequest req, ServerResponse res) {
    SecurityContext context = getSecurityContext(req);
    webTarget(servingPort).property(JwtProvider.EP_PROPERTY_OUTBOUND_USER, "jill").request(String.class).thenAccept(result -> res.send("You are: " + context.userName() + ", backend service returned: " + result)).exceptionally(throwable -> sendError(throwable, res));
}
Also used : OutboundOverrideUtil.startServer(io.helidon.security.examples.outbound.OutboundOverrideUtil.startServer) Config(io.helidon.config.Config) OutboundOverrideUtil.sendError(io.helidon.security.examples.outbound.OutboundOverrideUtil.sendError) SecurityContext(io.helidon.security.SecurityContext) Principal(io.helidon.security.Principal) ServerRequest(io.helidon.webserver.ServerRequest) OutboundOverrideUtil.getSecurityContext(io.helidon.security.examples.outbound.OutboundOverrideUtil.getSecurityContext) CompletionStage(java.util.concurrent.CompletionStage) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Subject(io.helidon.security.Subject) Routing(io.helidon.webserver.Routing) OutboundOverrideUtil.createConfig(io.helidon.security.examples.outbound.OutboundOverrideUtil.createConfig) OutboundOverrideUtil.webTarget(io.helidon.security.examples.outbound.OutboundOverrideUtil.webTarget) JwtProvider(io.helidon.security.providers.jwt.JwtProvider) SecurityContext(io.helidon.security.SecurityContext) OutboundOverrideUtil.getSecurityContext(io.helidon.security.examples.outbound.OutboundOverrideUtil.getSecurityContext)

Example 5 with ServerResponse

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

the class ObjectStorageService method upload.

private void upload(ServerRequest req, ServerResponse res) {
    OptionalLong contentLength = req.headers().contentLength();
    if (contentLength.isEmpty()) {
        req.content().forEach(DataChunk::release);
        res.status(Http.Status.BAD_REQUEST_400).send("Content length must be defined");
        return;
    }
    String objectName = req.path().param("file-name");
    PutObject.Request request = PutObject.Request.builder().objectName(objectName).bucket(bucketName).contentLength(contentLength.getAsLong());
    req.headers().contentType().ifPresent(request::requestMediaType);
    objectStorage.putObject(request, req.content()).forSingle(response -> res.send(response.requestId())).exceptionally(res::send);
}
Also used : DeleteObject(io.helidon.integrations.oci.objectstorage.DeleteObject) DataChunk(io.helidon.common.http.DataChunk) RenameObject(io.helidon.integrations.oci.objectstorage.RenameObject) ServerRequest(io.helidon.webserver.ServerRequest) GetObject(io.helidon.integrations.oci.objectstorage.GetObject) OptionalLong(java.util.OptionalLong) PutObject(io.helidon.integrations.oci.objectstorage.PutObject) GetObjectRx(io.helidon.integrations.oci.objectstorage.GetObjectRx) OciObjectStorageRx(io.helidon.integrations.oci.objectstorage.OciObjectStorageRx) ServerResponse(io.helidon.webserver.ServerResponse) Optional(java.util.Optional) Service(io.helidon.webserver.Service) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) OptionalLong(java.util.OptionalLong) DataChunk(io.helidon.common.http.DataChunk) PutObject(io.helidon.integrations.oci.objectstorage.PutObject)

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