コード例 #1
0
ファイル: RestRules.java プロジェクト: FernandoDoming/cep
  /**
   * 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;
  }