if (entry == null) {
return Response.status(400).
type("text/plain").
entity("Missing entry in request body\r\n").build();
}
User user = null;
try {
user = contentHelper.getContentEntity(entry, MediaType.APPLICATION_XML_TYPE, User.class);
} catch (IllegalArgumentException e) {
return Response.status(400).
type("text/plain").
entity("Missing content element in the supplied entry\r\n").build();
}
StringBuilder errors = new StringBuilder();
if ((user.getId() == null) || (user.getId().length() < 1)) {
user.setId(UUID.randomUUID().toString());
}
if ((user.getPassword() == null) || (user.getPassword().length() < 1)) {
errors.append("Missing 'password' property\r\n");
}
user.setUpdated(new Date());
if ((user.getUsername() == null) || (user.getUsername().length() < 1)) {
errors.append("Missing 'username' property\r\n");
}
if (errors.length() > 0) {
return Response.status(400).
type("text/plain").
entity(errors.toString()).build();
}
// Validate conditions that require locking the database
synchronized (Database.users) {
if (Database.users.get(user.getUsername()) != null) {
return Response.status(409).
type("text/plain").
entity("User '" + user.getUsername() + "' already exists\r\n").
build();
}
// Update the database with the new username
Database.users.put(user.getUsername(), user);
Database.usersUpdated = new Date();
synchronized (Database.contacts) {
Database.contacts.put(user.getUsername(), new ArrayList<Contact>());
Database.contactsUpdated.put(user.getUsername(), Database.usersUpdated);
}
return Response.created(uriInfo.getRequestUriBuilder().path(user.getUsername()).build()).
build();
}
}