log.info("Called GET (create test job)");
// If an Id have been provided, check whether the job has finished and return the result
if(!jid.equals("")){
log.info("Looking for test job {}", jid);
JobManager m = getJobManager();
// Remove first slash from param value
jid = jid.substring(1);
// If the job exists
if (m.hasJob(jid)){
log.info("Found job with id {}", jid);
Future<?> f = m.ping(jid);
if(f.isDone() && (!f.isCancelled())){
/**
* We return OK with the result
*/
Object o;
try {
o = f.get();
if(o instanceof JobResult){
JobResult result = (JobResult) o;
return Response.ok(result.getMessage()).build();
}else{
log.error("Job {} is not a test job", jid);
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 (Content-Location, the related job resource)
*
* TODO
* Change into json representations
*/
String location = getPublicBaseUri() + "jobs/" + jid;
String info = new StringBuilder().append("Result not ready.\n").append("Job Location: ").append(location).toString();
return Response.status(404).header("Content-Location", location).header("Content-type","text/plain").entity(info).build();
}
}else {
log.info("No job found with id {}", jid);
return Response.status(Response.Status.NOT_FOUND).build();
}
}else{
// No id have been provided, we create a new test job
JobManager m = getJobManager();
String id = m.execute(new Job() {
@Override
public JobResult call() throws Exception {
for (int i = 0; i < 30; i++) {
try {
log.info("Test Process is working");