Search in sources :

Example 1 with Leave

use of org.eclipse.microprofile.lra.annotation.ws.rs.Leave in project narayana by jbosstm.

the class NarayanaLRAClient method getTerminationUris.

/**
 * For particular compensator class it returns termination uris based on the provided base uri.
 * You get map of string and URI.
 *
 * @param compensatorClass  compensator class to examine
 * @param uriInfo  the uri that triggered this join request.
 * @return map of URI
 */
public static Map<String, String> getTerminationUris(Class<?> compensatorClass, UriInfo uriInfo, Long timeout) {
    Map<String, String> paths = new HashMap<>();
    final boolean[] asyncTermination = { false };
    URI baseUri = uriInfo.getBaseUri();
    /*
         * Calculate which path to prepend to the LRA participant methods. If there is more than one matching URI
         * then the second matched URI comes from either the class level Path annotation or from a sub-resource locator.
         * In both cases the second matched URI can be used as a prefix for the LRA participant URIs:
         */
    List<String> matchedURIs = uriInfo.getMatchedURIs();
    int matchedURI = (matchedURIs.size() > 1 ? 1 : 0);
    final String uriPrefix = baseUri + matchedURIs.get(matchedURI);
    String timeoutValue = timeout != null ? Long.toString(timeout) : "0";
    Arrays.stream(compensatorClass.getMethods()).forEach(method -> {
        Path pathAnnotation = method.getAnnotation(Path.class);
        if (pathAnnotation != null) {
            if (checkMethod(paths, method, COMPENSATE, pathAnnotation, method.getAnnotation(Compensate.class), uriPrefix) != 0) {
                paths.put(TIMELIMIT_PARAM_NAME, timeoutValue);
                if (isAsyncCompletion(method)) {
                    asyncTermination[0] = true;
                }
            }
            if (checkMethod(paths, method, COMPLETE, pathAnnotation, method.getAnnotation(Complete.class), uriPrefix) != 0) {
                paths.put(TIMELIMIT_PARAM_NAME, timeoutValue);
                if (isAsyncCompletion(method)) {
                    asyncTermination[0] = true;
                }
            }
            checkMethod(paths, method, STATUS, pathAnnotation, method.getAnnotation(Status.class), uriPrefix);
            checkMethod(paths, method, FORGET, pathAnnotation, method.getAnnotation(Forget.class), uriPrefix);
            checkMethod(paths, method, LEAVE, pathAnnotation, method.getAnnotation(Leave.class), uriPrefix);
            checkMethod(paths, method, AFTER, pathAnnotation, method.getAnnotation(AfterLRA.class), uriPrefix);
        }
    });
    if (asyncTermination[0] && !paths.containsKey(STATUS) && !paths.containsKey(FORGET)) {
        String logMsg = LRALogger.i18nLogger.error_asyncTerminationBeanMissStatusAndForget(compensatorClass);
        LRALogger.logger.warn(logMsg);
        throw new WebApplicationException(Response.status(BAD_REQUEST).entity(logMsg).build());
    }
    StringBuilder linkHeaderValue = new StringBuilder();
    if (paths.size() != 0) {
        paths.forEach((k, v) -> makeLink(linkHeaderValue, null, k, v));
        paths.put(LINK_TEXT, linkHeaderValue.toString());
    }
    return paths;
}
Also used : Path(javax.ws.rs.Path) Status(org.eclipse.microprofile.lra.annotation.Status) LRAStatus(org.eclipse.microprofile.lra.annotation.LRAStatus) AfterLRA(org.eclipse.microprofile.lra.annotation.AfterLRA) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) Forget(org.eclipse.microprofile.lra.annotation.Forget) Leave(org.eclipse.microprofile.lra.annotation.ws.rs.Leave) URI(java.net.URI)

Aggregations

URI (java.net.URI)1 HashMap (java.util.HashMap)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AfterLRA (org.eclipse.microprofile.lra.annotation.AfterLRA)1 Forget (org.eclipse.microprofile.lra.annotation.Forget)1 LRAStatus (org.eclipse.microprofile.lra.annotation.LRAStatus)1 Status (org.eclipse.microprofile.lra.annotation.Status)1 Leave (org.eclipse.microprofile.lra.annotation.ws.rs.Leave)1