Search in sources :

Example 1 with Current

use of io.narayana.lra.client.Current in project narayana by jbosstm.

the class ServerLRAFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    // a request is leaving the container so clear any context on the thread and fix up the LRA response header
    Object suspendedLRA = Current.getState("suspendedLRA");
    Object newLRA = Current.getState("newLRA");
    URL current = Current.peek();
    try {
        if (current != null) {
            int status = responseContext.getStatus();
            Response.Status.Family[] cancel0nFamily = (Response.Status.Family[]) requestContext.getProperty(CANCEL_ON_FAMILY_PROP);
            Response.Status[] cancel0n = (Response.Status[]) requestContext.getProperty(CANCEL_ON_PROP);
            Boolean closeCurrent = (Boolean) requestContext.getProperty(TERMINAL_LRA_PROP);
            if (closeCurrent == null)
                closeCurrent = false;
            if (cancel0nFamily != null)
                if (Arrays.stream(cancel0nFamily).anyMatch(f -> Response.Status.Family.familyOf(status) == f))
                    closeCurrent = true;
            if (cancel0n != null && !closeCurrent)
                if (Arrays.stream(cancel0n).anyMatch(f -> status == f.getStatusCode()))
                    closeCurrent = true;
            if (closeCurrent) {
                lraTrace(requestContext, (URL) newLRA, "ServerLRAFilter after: closing LRA becasue http status is " + status);
                lraClient.cancelLRA(current);
                if (current.equals(newLRA))
                    // don't try to cancle newKRA twice
                    newLRA = null;
            }
        }
        if (newLRA != null) {
            lraTrace(requestContext, (URL) newLRA, "ServerLRAFilter after: closing LRA");
            lraClient.closeLRA((URL) newLRA);
        }
        if (responseContext.getStatus() == Response.Status.OK.getStatusCode() && NarayanaLRAClient.isAsyncCompletion(resourceInfo.getResourceMethod())) {
            LRALogger.i18NLogger.warn_lraParticipantqForAsync(resourceInfo.getResourceMethod().getDeclaringClass().getName(), resourceInfo.getResourceMethod().getName(), Response.Status.ACCEPTED.getStatusCode(), Response.Status.OK.getStatusCode());
        }
    } finally {
        if (suspendedLRA != null)
            Current.push((URL) suspendedLRA);
        Current.updateLRAContext(responseContext.getHeaders());
        Current.popAll();
    }
}
Also used : Response(javax.ws.rs.core.Response) Status(io.narayana.lra.annotation.Status) LEAVE(io.narayana.lra.client.NarayanaLRAClient.LEAVE) Arrays(java.util.Arrays) COMPENSATE(io.narayana.lra.client.NarayanaLRAClient.COMPENSATE) Provider(javax.ws.rs.ext.Provider) URL(java.net.URL) IllegalLRAStateException(io.narayana.lra.client.IllegalLRAStateException) GenericLRAException(io.narayana.lra.client.GenericLRAException) Complete(io.narayana.lra.annotation.Complete) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) LRA_HTTP_HEADER(io.narayana.lra.client.NarayanaLRAClient.LRA_HTTP_HEADER) Compensate(io.narayana.lra.annotation.Compensate) Forget(io.narayana.lra.annotation.Forget) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inject(javax.inject.Inject) Leave(io.narayana.lra.annotation.Leave) ResourceInfo(javax.ws.rs.container.ResourceInfo) Current(io.narayana.lra.client.Current) Map(java.util.Map) FORGET(io.narayana.lra.client.NarayanaLRAClient.FORGET) Status(io.narayana.lra.annotation.Status) URI(java.net.URI) LRA(io.narayana.lra.annotation.LRA) Method(java.lang.reflect.Method) STATUS(io.narayana.lra.client.NarayanaLRAClient.STATUS) Context(javax.ws.rs.core.Context) TimeLimit(io.narayana.lra.annotation.TimeLimit) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) NestedLRA(io.narayana.lra.annotation.NestedLRA) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TimeUnit(java.util.concurrent.TimeUnit) NarayanaLRAClient(io.narayana.lra.client.NarayanaLRAClient) COMPLETE(io.narayana.lra.client.NarayanaLRAClient.COMPLETE) Response(javax.ws.rs.core.Response) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) LRALogger(io.narayana.lra.logging.LRALogger) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) LRA_HTTP_RECOVERY_HEADER(io.narayana.lra.client.NarayanaLRAClient.LRA_HTTP_RECOVERY_HEADER) URL(java.net.URL)

Aggregations

Compensate (io.narayana.lra.annotation.Compensate)1 Complete (io.narayana.lra.annotation.Complete)1 Forget (io.narayana.lra.annotation.Forget)1 LRA (io.narayana.lra.annotation.LRA)1 Leave (io.narayana.lra.annotation.Leave)1 NestedLRA (io.narayana.lra.annotation.NestedLRA)1 Status (io.narayana.lra.annotation.Status)1 TimeLimit (io.narayana.lra.annotation.TimeLimit)1 Current (io.narayana.lra.client.Current)1 GenericLRAException (io.narayana.lra.client.GenericLRAException)1 IllegalLRAStateException (io.narayana.lra.client.IllegalLRAStateException)1 NarayanaLRAClient (io.narayana.lra.client.NarayanaLRAClient)1 COMPENSATE (io.narayana.lra.client.NarayanaLRAClient.COMPENSATE)1 COMPLETE (io.narayana.lra.client.NarayanaLRAClient.COMPLETE)1 FORGET (io.narayana.lra.client.NarayanaLRAClient.FORGET)1 LEAVE (io.narayana.lra.client.NarayanaLRAClient.LEAVE)1 LRA_HTTP_HEADER (io.narayana.lra.client.NarayanaLRAClient.LRA_HTTP_HEADER)1 LRA_HTTP_RECOVERY_HEADER (io.narayana.lra.client.NarayanaLRAClient.LRA_HTTP_RECOVERY_HEADER)1 STATUS (io.narayana.lra.client.NarayanaLRAClient.STATUS)1 LRALogger (io.narayana.lra.logging.LRALogger)1