@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(); } }
@DELETE @Path("delete/{readingId}") public Response removeReading(@PathParam("readingId") int readingId) { try { if (!SecurityContextHolder.getContext() .getAuthentication() .getName() .equals(gasRDAO.getReading(readingId).getUsername())) { return Response.status(406).entity("Resource not owned by you.").build(); } } catch (DataAccessException e) { return Response.serverError() .entity("Failed to retrieve user identity for resource.") .build(); } try { gasRDAO.deleteReading(readingId); return Response.ok("Deleted.").build(); } catch (DataAccessException e) { return Response.serverError().entity("Server error.").build(); } }
@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(); } }
@GET @Path("account/{gasAcctId}") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response getReadings(@PathParam("gasAcctId") final int gasAcctId) { try { if (!SecurityContextHolder.getContext() .getAuthentication() .getName() .equals(gasDAO.getAccount(gasAcctId).getUsername())) { return Response.status(406).entity("Resource not owned by you").build(); } } catch (DataAccessException e) { return Response.serverError().entity("Failed to retrieve resource identity.").build(); } try { List<GasReading> readings = gasRDAO.getReadings(gasAcctId); if (readings.isEmpty()) { return Response.status(404).entity("Resource is empty.").build(); } return Response.ok(readings.toArray(new GasReading[readings.size()])).build(); } catch (DataAccessException e) { return Response.serverError().entity("Server error.").build(); } }