use of org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder 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();
}
}
use of org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder in project stanbol by apache.
the class ReasoningServiceTaskResource method processRealTimeRequest.
/**
* Process a real-time operation. Returns 200 when the process is ready, 500 if some error occurs
*
* @return
*/
private Response processRealTimeRequest() {
// page
if (this.parameters.isEmpty() && file == null) {
log.debug("no parameters no input file, show default index page");
// return Response.ok(new Viewable("index", this)).build();
ResponseBuilder rb = Response.ok(new Viewable("index", this));
rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
return rb.build();
}
try {
String target = getTarget();
// Setup the input
ReasoningServiceInputManager imngr = prepareInput();
// The service executor
ReasoningServiceExecutor executor = new ReasoningServiceExecutor(tcManager, imngr, getCurrentService(), getCurrentTask(), target, parameters);
ReasoningServiceResult<?> result = executor.call();
return new ResponseTaskBuilder(new ReasoningTaskResult(uriInfo, headers)).build(result);
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
Aggregations