Exemplo n.º 1
0
  @PUT
  @Path("/{idTimezone}")
  @Produces("application/json")
  public Response updateTimezone(
      @HeaderParam("token") String tokenString,
      @PathParam("idTimezone") Long idTimezone,
      @FormParam("name") String name,
      @FormParam("city") String city,
      @FormParam("hourDiff") Integer hourDiff) {
    try {
      JSONObject jsonObject = new JSONObject();

      Timezone timezone = timezoneDAO.getTimezone(idTimezone);
      if (timezone == null) {
        jsonObject.put("Response", "Non existent timezone");
        return Response.status(Response.Status.NOT_FOUND)
            .entity(String.valueOf(jsonObject))
            .build();
      }

      Long idUser = timezone.getUser().getIdUser();
      String tokenVerified = verifyToken(tokenString, idUser);

      if (tokenVerified == null) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
      } else if (!tokenVerified.equals("OK")) {
        jsonObject.put("Response", tokenVerified);
        return Response.status(Response.Status.UNAUTHORIZED)
            .entity(String.valueOf(jsonObject))
            .build();
      } else {
        String result = timezoneDAO.updateTimezone(timezone, name, city, hourDiff);
        jsonObject.put("Response", result);
        return Response.status(Response.Status.OK).entity(String.valueOf(jsonObject)).build();
      }
    } catch (Exception ex) {
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
  }
Exemplo n.º 2
0
  @DELETE
  @Path("/{idTimezone}")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces("application/json")
  public Response deleteTimezone(
      @HeaderParam("token") String tokenString, @PathParam("idTimezone") Long idTimezone) {
    try {
      JSONObject jsonObject = new JSONObject();

      Timezone timezone = timezoneDAO.getTimezone(idTimezone);
      if (timezone == null) {
        jsonObject.put("Response", "Non existent timezone");
        return Response.status(Response.Status.NOT_FOUND)
            .entity(String.valueOf(jsonObject))
            .build();
      }

      Long idUser = timezone.getUser().getIdUser();
      String tokenVerified = verifyToken(tokenString, idUser);

      if (tokenVerified == null) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
      } else if (!tokenVerified.equals("OK")) {
        jsonObject.put("Response", tokenVerified);
        return Response.status(Response.Status.UNAUTHORIZED)
            .entity(String.valueOf(jsonObject))
            .build();
      } else {
        String result = timezoneDAO.deleteTimezone(timezone);
        jsonObject.put("Response", result);
        return Response.status(Response.Status.OK).entity(String.valueOf(jsonObject)).build();
      }
    } catch (Exception ex) {
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
  }