@GET @Path("/{id}") public Response get(@PathParam("id") int id, @Context Request request) { // Create cache control header CacheControl cc = new CacheControl(); // Set max age to one day cc.setMaxAge(86400); Response.ResponseBuilder rb = null; // Calculate the ETag on last modified date of user resource EntityTag etag = new EntityTag(UserDao.getLastModifiedById(id).hashCode() + ""); // Verify if it matched with etag available in http request rb = request.evaluatePreconditions(etag); // If ETag matches the rb will be non-null; // Use the rb to return the response without any further processing if (rb != null) { return rb.cacheControl(cc).tag(etag).build(); } // If rb is null then either it is first time request; or resource is modified // Get the updated representation and return with Etag attached to it rb = Response.ok(UserDao.get(id).get()).cacheControl(cc).tag(etag); return rb.build(); }
@GET @Path("/{id}") @RolesAllowed({"ADMINISTRATOR"}) public Response find(@PathParam("id") final Long id) { logger.debug("Find user by id: {}", id); Response.ResponseBuilder responseBuilder; try { User user = userService.find(id); OperationResult result = OperationResult.success(userJsonConverter.convertToJsonElement(user)); responseBuilder = Response.status(HttpCode.OK.getCode()).entity(OperationResultJsonWriter.toJson(result)); logger.debug("User found by id: {}", user); } catch (UserNotFoundException e) { logger.error("No user found for id", id); responseBuilder = Response.status(HttpCode.NOT_FOUND.getCode()); } return responseBuilder.build(); }
@POST @Path("/authenticate") @PermitAll public Response findByEmailAndPassword(final String body) { logger.debug("Find user by email and password"); Response.ResponseBuilder responseBuilder; try { User userWithEmailAndPassword = getUserWithEmailAndPasswordFromJson(body); User user = userService.find( userWithEmailAndPassword.getEmail(), userWithEmailAndPassword.getPassword()); OperationResult result = OperationResult.success(userJsonConverter.convertToJsonElement(user)); responseBuilder = Response.status(HttpCode.OK.getCode()).entity(OperationResultJsonWriter.toJson(result)); logger.debug("User found by email/password: {}", user); } catch (UserNotFoundException e) { logger.error("No user found for email/password"); responseBuilder = Response.status(HttpCode.NOT_FOUND.getCode()); } return responseBuilder.build(); }