Search in sources :

Example 1 with AsyncCommand

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);
}
Also used : AsyncCommand(org.locationtech.geogig.rest.AsyncContext.AsyncCommand) Request(org.restlet.data.Request) MediaType(org.restlet.data.MediaType)

Aggregations

AsyncCommand (org.locationtech.geogig.rest.AsyncContext.AsyncCommand)1 MediaType (org.restlet.data.MediaType)1 Request (org.restlet.data.Request)1