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;
}
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;
}
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();
}
}
Aggregations