@POST public Response add(final String body) { logger.debug("Adding a new user with body {}", body); User user = userJsonConverter.convertFrom(body); if (user.getUserType().equals(User.UserType.EMPLOYEE)) { return Response.status(HttpCode.FORBIDDEN.getCode()).build(); } HttpCode httpCode = HttpCode.CREATED; OperationResult result; try { user = userService.add(user); result = OperationResult.success(JsonUtils.getJsonElementWithId(user.getId())); } catch (final FieldNotValidException e) { httpCode = HttpCode.VALIDATION_ERROR; logger.error("One of the fields of the user is not valid", e); result = getOperationResultInvalidField(RESOURCE_MESSAGE, e); } catch (final UserExistException e) { httpCode = HttpCode.VALIDATION_ERROR; logger.error("There is already an user for the given email", e); result = getOperationResultExists(RESOURCE_MESSAGE, "email"); } logger.debug("Returning the operation result after adding user: {}", result); return Response.status(httpCode.getCode()) .entity(OperationResultJsonWriter.toJson(result)) .build(); }
@GET @RolesAllowed({"ADMINISTRATOR"}) public Response findByFilter() { final UserFilter userFilter = new UserUrlFilterExtractor(uriInfo).getFilter(); logger.debug("Finding users using filter: {}", userFilter); final PaginatedData<User> users = userService.find(userFilter); logger.debug("Found {} users", users.getNumberOfRows()); final JsonElement jsonWithPagingAndEntries = JsonUtils.getJsonElementWithPagingAndEntries(users, userJsonConverter); return Response.status(HttpCode.OK.getCode()) .entity(JsonWriter.writeToString(jsonWithPagingAndEntries)) .build(); }