Role editorRole = db.getRole(AuthDB.EDITOR_ROLE);
if (editorRole==null) {
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return new StringRepresentation("Editor role not found.");
}
Realm realm = db.getRealm(name);
if (realm!=null) {
getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
return new StringRepresentation("Realm "+name+" already exists.");
}
realm = db.getRealm(id);
if (realm!=null) {
getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
return new StringRepresentation("Realm with id "+id+" already exists.");
}
realm = db.createRealm(name,id);
try {
Group group = db.createGroup(realm,UUID.randomUUID(),"admin");
group.addRole(rootRole);
group.addRole(editorRole);
Representation responseEntity = new DBObjectRepresentation(MediaType.APPLICATION_XML,realm);
responseEntity.setCharacterSet(CharacterSet.UTF_8);
getResponse().setStatus(Status.SUCCESS_CREATED);
return responseEntity;
} catch (SQLException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot create realm admin group.",ex);
try {
realm.delete();
} catch (SQLException ex2) {
getContext().getLogger().log(Level.SEVERE,"Cannot delete realm ("+name+","+id+") for cleanup",ex2);
}
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return new StringRepresentation("Cannot create realm admin group.");