use of org.locationtech.geogig.rest.AsyncContext.AsyncCommand in project GeoGig by boundlessgeo.
the class TaskStatusResource method getRepresentation.
@Override
public Representation getRepresentation(Variant variant) {
final Request request = getRequest();
final String taskId = getStringAttribute(request, "taskId");
final boolean prune = Boolean.valueOf(getRequest().getResourceRef().getQueryAsForm().getFirstValue("prune"));
final boolean cancel = Boolean.valueOf(getRequest().getResourceRef().getQueryAsForm().getFirstValue("cancel"));
final AsyncContext asyncContext = AsyncContext.get();
MediaType mediaType = variant.getMediaType();
final String rootPath = request.getRootRef().toString();
if (Strings.isNullOrEmpty(taskId)) {
Iterable<AsyncCommand<? extends Object>> all = asyncContext.getAll();
return new TaskListResource(mediaType, rootPath, all);
}
Optional<AsyncCommand<?>> cmd;
if (prune) {
cmd = asyncContext.getAndPruneIfFinished(taskId);
} else {
cmd = asyncContext.get(taskId);
}
if (!cmd.isPresent()) {
throw new RestletException("Task not found: " + taskId, Status.CLIENT_ERROR_NOT_FOUND);
}
AsyncCommand<?> command = cmd.get();
if (cancel) {
command.tryCancel();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// ignore
}
if (prune) {
asyncContext.getAndPruneIfFinished(taskId);
}
}
return Representations.newRepresentation(command, mediaType, rootPath);
}
Aggregations