Пример #1
0
  /**
   * This method handles HTTP POST requests with JSON data. It sends an add operation to the
   * listener passing it the JSON data.
   *
   * @param json A string in JSON format.
   * @return Response with the appropriate HTTP code.
   */
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response add(String json) {
    RestListener listener = RestManager.getListener();
    Response response;

    try {
      log.info("Add request with json: {}", json);
      listener.add(parseMap(json));
      response = Response.ok().build();
    } catch (RestInvalidException e) {
      log.info("Add request was invalid: {}", e.getMessage());
      response =
          Response.status(Response.Status.BAD_REQUEST)
              .entity(toMap(e, Response.Status.BAD_REQUEST))
              .build();
    } catch (Exception e) {
      e.printStackTrace();
      response =
          Response.serverError().entity(toMap(e, Response.Status.INTERNAL_SERVER_ERROR)).build();
    }

    return response;
  }
Пример #2
0
  /**
   * This methods handles HTTP DELETE requests. It sends an remove operation to the listener passing
   * it an ID.
   *
   * @param id The ID sent by the user on the request
   * @return Response with the appropriate HTTP code.
   */
  @DELETE
  @Path("/{id}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response remove(@PathParam("id") String id) {
    RestListener listener = RestManager.getListener();
    Response response;

    // Check if the listener accepted the operation
    try {
      log.info("Remove request with id: {}", id);
      listener.remove(id);
      response = Response.ok().build();
    } catch (RestNotFoundException e) {
      log.info("Remove request invalid: {}", e.getMessage());
      e.printStackTrace();
      response =
          Response.status(Response.Status.NOT_FOUND)
              .entity(toMap(e, Response.Status.NOT_FOUND))
              .build();
    } catch (Exception e) {
      e.printStackTrace();
      response =
          Response.serverError().entity(toMap(e, Response.Status.INTERNAL_SERVER_ERROR)).build();
    }

    return response;
  }
Пример #3
0
  /**
   * This method handles HTTP GET requests at path /list. It responds with a list in JSON form.
   *
   * @return Response with the appropriate HTTP code.
   */
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response list() {
    RestListener listener = RestManager.getListener();
    Response response;

    try {
      List<Map<String, Object>> list = listener.list();
      log.info("List request {}", list);
      response = Response.ok().entity(list).build();
    } catch (Exception e) {
      e.printStackTrace();
      response =
          Response.serverError().entity(toMap(e, Response.Status.INTERNAL_SERVER_ERROR)).build();
    }

    return response;
  }