Example #1
0
  /**
   * Get all jobs and instances of each job of a given user.
   *
   * @param username
   * @param token
   * @return registrations array json
   */
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response getJobs(
      @CookieParam(value = "user") Cookie userc, @CookieParam(value = "token") String token) {

    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);
      e.printStackTrace();
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }

    try {

      List<Registration> userRegistrations = jmanager.getUserJobs(user);
      Gson gson = new Gson();
      String json = "{ \"jobs\" : " + gson.toJson(userRegistrations) + "}";
      return Response.status(Response.Status.OK)
          .entity(json)
          .type(MediaType.APPLICATION_JSON)
          .build();

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