Search in sources :

Example 1 with Status

use of io.narayana.sra.annotation.Status in project narayana by jbosstm.

the class SRAClient method getTerminationUris.

public Map<String, String> getTerminationUris(Class<?> compensatorClass, URI baseUri, boolean validate) {
    Map<String, String> paths = new HashMap<>();
    Annotation resourcePathAnnotation = compensatorClass.getAnnotation(Path.class);
    String resourcePath = resourcePathAnnotation == null ? "/" : ((Path) resourcePathAnnotation).value();
    String uriPrefix = String.format("%s:%s%s", baseUri.getScheme(), baseUri.getSchemeSpecificPart(), resourcePath.substring(1));
    final int[] validCnt = { 0 };
    Arrays.stream(compensatorClass.getMethods()).forEach(method -> {
        Annotation pathAnnotation = method.getAnnotation(Path.class);
        if (pathAnnotation != null) {
            if (checkMethod(paths, STATUS, (Path) pathAnnotation, method.getAnnotation(Status.class), uriPrefix)) {
                validCnt[0] += 1;
            }
            if (checkMethod(paths, PREPARE, (Path) pathAnnotation, method.getAnnotation(Prepare.class), uriPrefix)) {
                validCnt[0] += 1;
            }
            if (checkMethod(paths, COMMIT, (Path) pathAnnotation, method.getAnnotation(Commit.class), uriPrefix)) {
                validCnt[0] += 1;
            }
            if (checkMethod(paths, ROLLBACK, (Path) pathAnnotation, method.getAnnotation(Rollback.class), uriPrefix)) {
                validCnt[0] += 1;
            }
            checkMethod(paths, ONEPHASECOMMIT, (Path) pathAnnotation, method.getAnnotation(OnePhaseCommit.class), uriPrefix);
        }
    });
    if (validate && validCnt[0] < 4) {
        if (!paths.containsKey(COMMIT) && !paths.containsKey(ONEPHASECOMMIT))
            throw new GenericSRAException(null, Response.Status.BAD_REQUEST.getStatusCode(), String.format(MISSING_ANNOTATION_FORMAT, compensatorClass.getName()), null);
    }
    StringBuilder linkHeaderValue = new StringBuilder();
    paths.forEach((k, v) -> makeLink(linkHeaderValue, null, k, v));
    paths.put("Link", linkHeaderValue.toString());
    return paths;
}
Also used : Status(io.narayana.sra.annotation.Status) Commit(io.narayana.sra.annotation.Commit) OnePhaseCommit(io.narayana.sra.annotation.OnePhaseCommit) HashMap(java.util.HashMap) Prepare(io.narayana.sra.annotation.Prepare) Rollback(io.narayana.sra.annotation.Rollback) OnePhaseCommit(io.narayana.sra.annotation.OnePhaseCommit) Annotation(java.lang.annotation.Annotation)

Example 2 with Status

use of io.narayana.sra.annotation.Status in project narayana by jbosstm.

the class ServerSRAFilter method getTerminationUris.

/**
 * Checks for Complete, Compensate and Status annotations and returns the JAX-RS paths of the methods
 * they are associated with
 */
private Map<String, String> getTerminationUris(Class<?> compensatorClass) {
    Map<String, String> paths = new HashMap<>();
    Arrays.stream(compensatorClass.getMethods()).forEach(method -> {
        Annotation pathAnnotation = method.getAnnotation(Path.class);
        if (pathAnnotation != null) {
            checkMethod(paths, SRAClient.COMMIT, (Path) pathAnnotation, method.getAnnotation(Commit.class));
            checkMethod(paths, SRAClient.PREPARE, (Path) pathAnnotation, method.getAnnotation(Prepare.class));
            checkMethod(paths, SRAClient.ROLLBACK, (Path) pathAnnotation, method.getAnnotation(Rollback.class));
            checkMethod(paths, SRAClient.STATUS, (Path) pathAnnotation, method.getAnnotation(Status.class));
            checkMethod(paths, SRAClient.ONEPHASECOMMIT, (Path) pathAnnotation, method.getAnnotation(OnePhaseCommit.class));
        }
    // TODO do we need to tell the coordinaor which HTTP verb the annotations are using
    });
    return paths;
}
Also used : Status(io.narayana.sra.annotation.Status) Commit(io.narayana.sra.annotation.Commit) OnePhaseCommit(io.narayana.sra.annotation.OnePhaseCommit) HashMap(java.util.HashMap) Prepare(io.narayana.sra.annotation.Prepare) Rollback(io.narayana.sra.annotation.Rollback) OnePhaseCommit(io.narayana.sra.annotation.OnePhaseCommit) Annotation(java.lang.annotation.Annotation)

Example 3 with Status

use of io.narayana.sra.annotation.Status in project narayana by jbosstm.

the class ServerSRAFilter 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 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 (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 != null && closeCurrent) {
                lraTrace(requestContext, (URL) newLRA, "ServerLRAFilter after: closing LRA becasue http status is " + status);
                lraClient.cancelSRA(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.commitSRA((URL) newLRA);
        }
    } finally {
        Current.updateLRAContext(responseContext.getHeaders());
        Current.popAll();
    }
}
Also used : Response(javax.ws.rs.core.Response) Status(io.narayana.sra.annotation.Status) SRA_HTTP_HEADER(io.narayana.sra.client.SRAClient.SRA_HTTP_HEADER) Arrays(java.util.Arrays) Provider(javax.ws.rs.ext.Provider) URL(java.net.URL) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Commit(io.narayana.sra.annotation.Commit) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) ResourceInfo(javax.ws.rs.container.ResourceInfo) Map(java.util.Map) URI(java.net.URI) TimeLimit(io.narayana.sra.annotation.TimeLimit) Method(java.lang.reflect.Method) Prepare(io.narayana.sra.annotation.Prepare) Context(javax.ws.rs.core.Context) Status(io.narayana.sra.annotation.Status) IOException(java.io.IOException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TimeUnit(java.util.concurrent.TimeUnit) RTS_HTTP_RECOVERY_HEADER(io.narayana.sra.client.SRAClient.RTS_HTTP_RECOVERY_HEADER) Response(javax.ws.rs.core.Response) Annotation(java.lang.annotation.Annotation) OnePhaseCommit(io.narayana.sra.annotation.OnePhaseCommit) SRA(io.narayana.sra.annotation.SRA) WebApplicationException(javax.ws.rs.WebApplicationException) Rollback(io.narayana.sra.annotation.Rollback) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Link(javax.ws.rs.core.Link) URL(java.net.URL)

Aggregations

Commit (io.narayana.sra.annotation.Commit)3 OnePhaseCommit (io.narayana.sra.annotation.OnePhaseCommit)3 Prepare (io.narayana.sra.annotation.Prepare)3 Rollback (io.narayana.sra.annotation.Rollback)3 Status (io.narayana.sra.annotation.Status)3 Annotation (java.lang.annotation.Annotation)3 HashMap (java.util.HashMap)3 SRA (io.narayana.sra.annotation.SRA)1 TimeLimit (io.narayana.sra.annotation.TimeLimit)1 RTS_HTTP_RECOVERY_HEADER (io.narayana.sra.client.SRAClient.RTS_HTTP_RECOVERY_HEADER)1 SRA_HTTP_HEADER (io.narayana.sra.client.SRAClient.SRA_HTTP_HEADER)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 URL (java.net.URL)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 Inject (javax.inject.Inject)1 Path (javax.ws.rs.Path)1