class UserRest {

    @PUT
    @Path("{username}")
    @RolesAllowed("user")
    @ApiOperation("Update a Mobi user's information")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateUser(@Context ContainerRequestContext context,
                        @PathParam("username") String username,
                        String newUserStr) {
        if (StringUtils.isEmpty(username)) {
            throw ErrorUtils.sendError("Current username must be provided", Response.Status.BAD_REQUEST);
        }
        isAuthorizedUser(context, username);

        try {
            Model userModel = jsonldToModel(newUserStr, transformer);
            Set<Resource> subjects = userModel.filter(null, vf.createIRI(RDF.TYPE.stringValue()),
                    vf.createIRI(User.TYPE)).subjects();
            if (subjects.size() < 1) {
                throw ErrorUtils.sendError("User must have an ID", Response.Status.BAD_REQUEST);
            }
            User newUser = userFactory.createNew(subjects.iterator().next(), userModel);

            Value newUsername = newUser.getUsername().orElseThrow(() ->
                    ErrorUtils.sendError("Username must be provided in new user", Response.Status.BAD_REQUEST));
            if (!username.equals(newUsername.stringValue())) {
                throw ErrorUtils.sendError("Provided username and the username in the data must match",
                        Response.Status.BAD_REQUEST);
            }
            User savedUser = engineManager.retrieveUser(rdfEngine.getEngineName(), username).orElseThrow(() ->
                    ErrorUtils.sendError("User " + username + " not found", Response.Status.BAD_REQUEST));
            if (!savedUser.getUsername().isPresent()) {
                throw ErrorUtils.sendError("User must have a username", Response.Status.INTERNAL_SERVER_ERROR);
            }
            if (!savedUser.getPassword().isPresent()) {
                throw ErrorUtils.sendError("User must have a password", Response.Status.INTERNAL_SERVER_ERROR);
            }
            if (!savedUser.getUsername().get().equals(newUsername)) {
                throw ErrorUtils.sendError("Usernames must match", Response.Status.BAD_REQUEST);
            }

            if (!savedUser.getHasUserRole().isEmpty()) {
                newUser.setHasUserRole(savedUser.getHasUserRole());
            }
            newUser.setPassword(savedUser.getPassword().get());

            engineManager.updateUser(rdfEngine.getEngineName(), newUser);
            return Response.ok().build();
        } catch (IllegalArgumentException ex) {
            throw ErrorUtils.sendError(ex.getMessage(), Response.Status.BAD_REQUEST);
        }
    }

}