Search in sources :

Example 1 with ReasoningServiceResult

use of org.apache.stanbol.reasoners.web.utils.ReasoningServiceResult in project stanbol by apache.

the class JobsResource method get.

/**
     * To read a /reasoners job output.
     * 
     * @param id
     * @return
     */
@GET
@Path("/{jid}")
public Response get(@PathParam("jid") String id, @Context HttpHeaders headers) {
    log.info("Pinging job {}", id);
    // No id
    if (id == null || id.equals("")) {
        ResponseBuilder rb = Response.status(Status.BAD_REQUEST);
        return rb.build();
    }
    JobManager m = getJobManager();
    // If the job exists
    if (m.hasJob(id)) {
        log.info("Found job with id {}", id);
        Future<?> f = m.ping(id);
        if (f.isDone() && (!f.isCancelled())) {
            /**
                 * We return OK with the result
                 */
            Object o;
            try {
                o = f.get();
                if (o instanceof ReasoningServiceResult) {
                    log.debug("Is a ReasoningServiceResult");
                    ReasoningServiceResult<?> result = (ReasoningServiceResult<?>) o;
                    return new ResponseTaskBuilder(new JobResultResource(uriInfo, headers)).build(result);
                } else {
                    log.error("Job {} does not belong to reasoners", id);
                    throw new WebApplicationException(Response.Status.NOT_FOUND);
                }
            } catch (InterruptedException e) {
                log.error("Error: ", e);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            } catch (ExecutionException e) {
                log.error("Error: ", e);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            /**
                 * We return 404 with additional info
                 */
            String jobService = new StringBuilder().append(getPublicBaseUri()).append("jobs/").append(id).toString();
            this.jobLocation = jobService;
            Viewable viewable = new Viewable("404.ftl", this);
            ResponseBuilder rb = Response.status(Status.NOT_FOUND);
            rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
            rb.entity(viewable);
            return rb.build();
        }
    } else {
        log.info("No job found with id {}", id);
        ResponseBuilder rb = Response.status(Status.NOT_FOUND);
        rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
        return rb.build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ReasoningServiceResult(org.apache.stanbol.reasoners.web.utils.ReasoningServiceResult) JobManager(org.apache.stanbol.commons.jobs.api.JobManager) ResponseTaskBuilder(org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ExecutionException(java.util.concurrent.ExecutionException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 JobManager (org.apache.stanbol.commons.jobs.api.JobManager)1 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)1 ReasoningServiceResult (org.apache.stanbol.reasoners.web.utils.ReasoningServiceResult)1 ResponseTaskBuilder (org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder)1