Exemplo n.º 1
0
  /**
   * Get job information and executions details. This response has one call to the BatchAdminClient
   * to get the job info and a short info about instances, and the response is completed with one
   * getExecutionDetail request for each instance to grab other useful information about the
   * execution.
   *
   * @param username
   * @param token
   * @return job
   */
  @GET
  @Path("/{jobName}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getJob(
      @CookieParam(value = "user") Cookie userc,
      @CookieParam(value = "token") String token,
      @PathParam("jobName") String jobName) {

    /*
     * authenticates the user, throw exception if failed
     */
    UserProfile user;
    try {
      // authenticates the user, throw exception if fail
      user = frameworkUserManager.validate(userc, token);
      if (user == null) {
        return Response.status(Response.Status.UNAUTHORIZED).entity("Invalid credentials").build();
      }
    } catch (Exception e) {
      log.error(e);
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }

    try {
      Registration job = jmanager.getJob(jobName, user);
      if (job != null) {
        JobExecutions executions = jmanager.getExcecutions(job);
        Gson gson = new Gson();
        String json =
            "{ \"job\" : "
                + gson.toJson(job)
                + ", \"executions\":"
                + gson.toJson(executions.getJobExecutions())
                + "}";
        return Response.status(Response.Status.OK)
            .entity(json)
            .type(MediaType.APPLICATION_JSON)
            .build();
      } else
        return Response.status(Response.Status.NO_CONTENT)
            .entity("User do not have job")
            .type(MediaType.APPLICATION_JSON)
            .build();

    } catch (Exception e) {
      log.error(e);
      e.printStackTrace();
      return Response.status(Response.Status.EXPECTATION_FAILED).entity(e.getMessage()).build();
    }
  }