try {
DocumentDestination dest = new DocumentDestination();
parser.parse(entity,dest);
doc = dest.getDocument();
Element top = doc.getDocumentElement();
String sid = top.getAttributeValue("id");
String alias = top.getAttributeValue("alias");
if (sid==null && alias==null) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("One of the 'id' or 'alias' attributes must be specified.");
}
UUID id = null;
if (sid!=null) {
try {
id = UUID.fromString(sid);
} catch (IllegalArgumentException ex) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Invalid UUID value for user id.");
}
}
String password = top.getAttributeValue("password");
Element name = top.getFirstElementNamed(XML.NAME_NAME);
Element email = top.getFirstElementNamed(XML.EMAIL_NAME);
if (alias!=null && !User.isAlias(alias)) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("The alias '"+alias+"' does not contain all letters or digits.");
}
try {
Realm realm = null;
try {
realm = fetch();
} catch (IllegalArgumentException ex) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Invalid UUID value for realm id.");
}
if (realm==null) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return new StringRepresentation("Realm not found.");
}
User user = null;
if (id!=null) {
user = db.getUser(id);
if (user==null) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return new StringRepresentation("User with id "+id+" not found.");
} else if (alias==null && user.getAlias()==null) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("The global user "+id+" does not have an alias and the alias was not specified on the request.");
}
}
if (user==null) {
// we have a request to create a realm user not tied to an existing user.
// create a new user and then the realm user
user = db.createUser(UUID.randomUUID(),null,null,null);
if (user==null) {
// this really shouldn't happen because we don't have an alias
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return new StringRepresentation("User creation is not available.");
}
// set the password if they specified it
if (password!=null) {
user.setPassword(password);
}
}
if (alias!=null && alias.equals(user.getAlias())) {
// inherit the alias as it is the same
alias = null;
}
if (db.isRealmUserAliasAvailable(realm,user,alias)) {
RealmUser realmUser = db.createRealmUser(realm,user,alias,name==null ? null : name.getText(),email==null ? null : email.getText());
if (realmUser==null) {
getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED);
return new StringRepresentation("The realm user could not be created.");
} else {
Representation responseEntity = new DBObjectRepresentation(MediaType.APPLICATION_XML,realmUser);