Example #1
0
  /**
   * Authenticates the users session and creates a xml job file of the required services and
   * registers this job in the batch-admin.
   *
   * <p>OneStepServiceJob.setBody should be encoded to avoid confusion with the job object in the
   * case where the body content for the service is also json.
   *
   * @param userc information about the registered user
   * @param token for authentication
   * @param serviceJob {@link MultiStepJob} JSON
   * @return job in JSON format
   */
  @PUT
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public Response createMultiStepJob(
      @CookieParam(value = "user") Cookie userc,
      @CookieParam(value = "token") String token,
      MultiStepJob serviceJob) {
    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();
      }
      log.info(serviceJob.toString());

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

    try {
      Registration job = jmanager.createJob(serviceJob, user);
      // read and return response
      Gson gson = new Gson();
      String json = "{ \"job\" : " + gson.toJson(job) + "}";
      log.debug("registered:" + json);

      return Response.status(Response.Status.CREATED).entity(json).build();

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