Search in sources :

Example 1 with LRA_HTTP_PARENT_CONTEXT_HEADER

use of org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_PARENT_CONTEXT_HEADER in project helidon by oracle.

the class NonJaxRsResource method createNonJaxRsParticipantResource.

Service createNonJaxRsParticipantResource() {
    return rules -> rules.any("/{type}/{fqdn}/{methodName}", (req, res) -> {
        LOGGER.log(Level.FINE, () -> "Non JAX-RS LRA resource " + req.method().name() + " " + req.absoluteUri());
        RequestHeaders headers = req.headers();
        HttpRequest.Path path = req.path();
        URI lraId = headers.first(LRA_HTTP_CONTEXT_HEADER).or(() -> headers.first(LRA_HTTP_ENDED_CONTEXT_HEADER)).map(URI::create).orElse(null);
        URI parentId = headers.first(LRA_HTTP_PARENT_CONTEXT_HEADER).map(URI::create).orElse(null);
        PropagatedHeaders propagatedHeaders = participantService.prepareCustomHeaderPropagation(headers.toMap());
        String fqdn = path.param("fqdn");
        String method = path.param("methodName");
        String type = path.param("type");
        switch(type) {
            case "compensate":
            case "complete":
                Single.<Optional<?>>empty().observeOn(exec).onCompleteResumeWithSingle(o -> participantService.invoke(fqdn, method, lraId, parentId, propagatedHeaders)).forSingle(result -> result.ifPresentOrElse(r -> sendResult(res, r), res::send)).exceptionallyAccept(t -> sendError(lraId, req, res, t));
                break;
            case "afterlra":
                req.content().as(String.class).map(LRAStatus::valueOf).observeOn(exec).flatMapSingle(s -> Single.defer(() -> participantService.invoke(fqdn, method, lraId, s, propagatedHeaders))).onComplete(res::send).onError(t -> sendError(lraId, req, res, t)).ignoreElement();
                break;
            case "status":
                Single.<Optional<?>>empty().observeOn(exec).onCompleteResumeWithSingle(o -> participantService.invoke(fqdn, method, lraId, null, propagatedHeaders)).forSingle(result -> result.ifPresentOrElse(r -> sendResult(res, r), // or in the case of non-JAX-RS method returning ParticipantStatus null.
                () -> res.status(Response.Status.GONE.getStatusCode()).send())).exceptionallyAccept(t -> sendError(lraId, req, res, t));
                break;
            case "forget":
                Single.<Optional<?>>empty().observeOn(exec).onCompleteResumeWithSingle(o -> participantService.invoke(fqdn, method, lraId, parentId, propagatedHeaders)).onComplete(res::send).onError(t -> sendError(lraId, req, res, t)).ignoreElement();
                break;
            default:
                LOGGER.severe(() -> "Unexpected non Jax-Rs LRA compensation type " + type + ": " + req.absoluteUri());
                res.status(404).send();
                break;
        }
    });
}
Also used : LRAResponse(org.eclipse.microprofile.lra.LRAResponse) LRA_HTTP_CONTEXT_HEADER(org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_CONTEXT_HEADER) PropagatedHeaders(io.helidon.lra.coordinator.client.PropagatedHeaders) LRA_HTTP_ENDED_CONTEXT_HEADER(org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_ENDED_CONTEXT_HEADER) Supplier(java.util.function.Supplier) Level(java.util.logging.Level) Response(jakarta.ws.rs.core.Response) Map(java.util.Map) ServerResponse(io.helidon.webserver.ServerResponse) Single(io.helidon.common.reactive.Single) URI(java.net.URI) Service(io.helidon.webserver.Service) Reflected(io.helidon.common.Reflected) ExecutorService(java.util.concurrent.ExecutorService) LRA_HTTP_PARENT_CONTEXT_HEADER(org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_PARENT_CONTEXT_HEADER) ParticipantStatus(org.eclipse.microprofile.lra.annotation.ParticipantStatus) Config(io.helidon.config.Config) PreDestroy(jakarta.annotation.PreDestroy) Logger(java.util.logging.Logger) RequestHeaders(io.helidon.webserver.RequestHeaders) Collectors(java.util.stream.Collectors) ServerRequest(io.helidon.webserver.ServerRequest) TimeUnit(java.util.concurrent.TimeUnit) LRAStatus(org.eclipse.microprofile.lra.annotation.LRAStatus) Optional(java.util.Optional) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Inject(jakarta.inject.Inject) ThreadPoolSupplier(io.helidon.common.configurable.ThreadPoolSupplier) HttpRequest(io.helidon.common.http.HttpRequest) HttpRequest(io.helidon.common.http.HttpRequest) PropagatedHeaders(io.helidon.lra.coordinator.client.PropagatedHeaders) Optional(java.util.Optional) RequestHeaders(io.helidon.webserver.RequestHeaders) URI(java.net.URI)

Example 2 with LRA_HTTP_PARENT_CONTEXT_HEADER

use of org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_PARENT_CONTEXT_HEADER in project helidon by oracle.

the class LraAnnotationHandler method handleJaxRsAfter.

@Override
public void handleJaxRsAfter(ContainerRequestContext reqCtx, ContainerResponseContext resCtx, ResourceInfo resourceInfo) {
    Optional<URI> lraId = Optional.ofNullable((URI) reqCtx.getProperty(LRA_HTTP_CONTEXT_HEADER)).or(() -> Contexts.context().flatMap(c -> c.get(LRA_HTTP_CONTEXT_HEADER, URI.class)));
    PropagatedHeaders propagatedHeaders = participantService.prepareCustomHeaderPropagation(reqCtx.getHeaders());
    Response.Status resStatus = resCtx.getStatusInfo().toEnum();
    Response.Status.Family resFamily = resCtx.getStatusInfo().getFamily();
    boolean end = annotation.end();
    boolean cancel = annotation.cancelOnFamily().contains(resFamily) || annotation.cancelOn().contains(resStatus);
    lraId.ifPresent(id -> {
        if (cancel) {
            cancel(id, propagatedHeaders);
        } else if (end) {
            close(id, propagatedHeaders);
        }
        resCtx.getHeaders().putSingle(LRA_HTTP_CONTEXT_HEADER, id);
    });
    Optional.ofNullable(reqCtx.getProperty(LRA_HTTP_PARENT_CONTEXT_HEADER)).map(URI.class::cast).ifPresent(suppressedLra -> resCtx.getHeaders().putSingle(LRA_HTTP_CONTEXT_HEADER, suppressedLra));
}
Also used : LRA_HTTP_CONTEXT_HEADER(org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_CONTEXT_HEADER) PropagatedHeaders(io.helidon.lra.coordinator.client.PropagatedHeaders) LRA_HTTP_RECOVERY_HEADER(org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_RECOVERY_HEADER) Participant(io.helidon.lra.coordinator.client.Participant) CompletionException(java.util.concurrent.CompletionException) Logger(java.util.logging.Logger) WebApplicationException(jakarta.ws.rs.WebApplicationException) Contexts(io.helidon.common.context.Contexts) Response(jakarta.ws.rs.core.Response) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) CoordinatorConnectionException(io.helidon.lra.coordinator.client.CoordinatorConnectionException) AnnotationInstance(org.jboss.jandex.AnnotationInstance) Duration(java.time.Duration) ResourceInfo(jakarta.ws.rs.container.ResourceInfo) Optional(java.util.Optional) Single(io.helidon.common.reactive.Single) CoordinatorClient(io.helidon.lra.coordinator.client.CoordinatorClient) ContainerResponseContext(jakarta.ws.rs.container.ContainerResponseContext) URI(java.net.URI) Method(java.lang.reflect.Method) LRA_HTTP_PARENT_CONTEXT_HEADER(org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_PARENT_CONTEXT_HEADER) Response(jakarta.ws.rs.core.Response) PropagatedHeaders(io.helidon.lra.coordinator.client.PropagatedHeaders) URI(java.net.URI)

Aggregations

Single (io.helidon.common.reactive.Single)2 PropagatedHeaders (io.helidon.lra.coordinator.client.PropagatedHeaders)2 Response (jakarta.ws.rs.core.Response)2 URI (java.net.URI)2 Optional (java.util.Optional)2 Logger (java.util.logging.Logger)2 LRA_HTTP_CONTEXT_HEADER (org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_CONTEXT_HEADER)2 LRA_HTTP_PARENT_CONTEXT_HEADER (org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_PARENT_CONTEXT_HEADER)2 Reflected (io.helidon.common.Reflected)1 ThreadPoolSupplier (io.helidon.common.configurable.ThreadPoolSupplier)1 Contexts (io.helidon.common.context.Contexts)1 HttpRequest (io.helidon.common.http.HttpRequest)1 Config (io.helidon.config.Config)1 CoordinatorClient (io.helidon.lra.coordinator.client.CoordinatorClient)1 CoordinatorConnectionException (io.helidon.lra.coordinator.client.CoordinatorConnectionException)1 Participant (io.helidon.lra.coordinator.client.Participant)1 RequestHeaders (io.helidon.webserver.RequestHeaders)1 ServerRequest (io.helidon.webserver.ServerRequest)1 ServerResponse (io.helidon.webserver.ServerResponse)1 Service (io.helidon.webserver.Service)1