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
 @GET
 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
 @Produces("application/json")
 public Response getAllTimezones(@HeaderParam("token") String tokenString) {
   try {
     JSONObject jsonObject = new JSONObject();
     User user = verifyToken(tokenString);
     if (user == null) {
       return Response.status(Response.Status.UNAUTHORIZED).build();
     } else if (user.getRole().getIdRole() == 1) {
       List<Timezone> timezoneList = timezoneDAO.listTimezones();
       TimezoneDto[] timezones = Converters.listTimezoneToDto(timezoneList);
       jsonObject.put("timezones", timezones);
     } else {
       List<Timezone> timezoneList = timezoneDAO.listTimezones(user);
       TimezoneDto[] timezones = Converters.listTimezoneToDto(timezoneList);
       jsonObject.put("timezones", timezones);
     }
     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.º 3
0
 @POST
 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
 @Produces("application/json")
 public Response createTimezone(
     @HeaderParam("token") String tokenString,
     @FormParam("idUser") Long idUser,
     @FormParam("name") String name,
     @FormParam("city") String city,
     @FormParam("hourDiff") int hourDiff) {
   try {
     JSONObject jsonObject = new JSONObject();
     if (idUser == null
         || name == null
         || name.isEmpty()
         || city == null
         || city.isEmpty()
         || hourDiff < -12
         || hourDiff > 13) {
       jsonObject.put("Response", "missing or incorrect parameters");
       return Response.status(Response.Status.BAD_REQUEST)
           .entity(String.valueOf(jsonObject))
           .build();
     }
     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.createTimezone(idUser, name, city, hourDiff);
       jsonObject.put("Response", result);
       if (result.equals("Timezone alredy exists")) {
         return Response.status(Response.Status.CONFLICT)
             .entity(String.valueOf(jsonObject))
             .build();
       } else if (result.equals("Non existent user")) {
         return Response.status(Response.Status.BAD_REQUEST)
             .entity(String.valueOf(jsonObject))
             .build();
       }
       return Response.status(Response.Status.CREATED).entity(String.valueOf(jsonObject)).build();
     }
   } catch (Exception ex) {
     return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
   }
 }
Exemplo n.º 4
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();
    }
  }