Exemplo n.º 1
0
 @POST
 @Path("add")
 @Produces(MediaType.TEXT_PLAIN)
 public Response add(
     @FormParam("gasAcctId") final int gasAcctId,
     @FormParam("reading") final int reading,
     @FormParam("readDate") final Date readDate) {
   try {
     if (!SecurityContextHolder.getContext()
         .getAuthentication()
         .getName()
         .equals(gasDAO.getAccount(gasAcctId).getUsername())) {
       return Response.status(406).entity("Resource identity not correct.").build();
     }
   } catch (DataAccessException e) {
     return Response.serverError().entity("Failed to retrieve resource identity.").build();
   }
   GasReading gasReading = new GasReading();
   gasReading.setGasAcctId(gasAcctId);
   gasReading.setReading(reading);
   gasReading.setReadDate(readDate);
   try {
     return Response.status(201).entity(String.valueOf(gasRDAO.addReading(gasReading))).build();
   } catch (DataAccessException e) {
     return Response.serverError().entity("Server error.").build();
   }
 }
Exemplo n.º 2
0
 @GET
 @Path("{readingId}")
 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 public Response getReading(@PathParam("readingId") final int readingId) {
   try {
     GasReading gasReading = gasRDAO.getReading(readingId);
     if (gasReading
         .getUsername()
         .equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
       return Response.ok(gasReading).build();
     } else {
       return Response.status(406).entity("Resource not owned by you.").build();
     }
   } catch (IncorrectResultSizeDataAccessException e) {
     return Response.status(404).entity("Resource not exist.").build();
   } catch (DataAccessException e) {
     return Response.serverError().entity("Server error").build();
   }
 }