request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,cred.getName(),cred.getPassword()));
Response response = client.handle(request);
if (response.getStatus().isSuccess()) {
try {
// TODO: this should stream...
Document usersDoc = parser.load(response.getEntity());
final Iterator<Element> users = usersDoc.getDocumentElement().getElementsByName(USER);
return new Iterator<User>() {
public boolean hasNext() {
return users.hasNext();
}
public void remove() {
throw new UnsupportedOperationException("remove() is not supported.");
}
public User next() {
Element userE = users.next();
String alias = userE.getAttributeValue("alias");
try {
User user = userCache.get(null,alias);
if (user==null) {
// get groups
String groupsURL = serviceBase.resolve("./users/a/"+alias+"/groups").toString();
Request request = new Request(Method.GET,groupsURL);
request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,cred.getName(),cred.getPassword()));
Response response = client.handle(request);
if (response.getStatus().isSuccess()) {
try {
Document groupsDoc = parser.load(response.getEntity());
UUID uuid = UUID.fromString(userE.getAttributeValue("id"));
Element nameE = userE.getFirstElementNamed(NAME);
String name = nameE==null ? null : nameE.getText();
Element emailE = userE.getFirstElementNamed(EMAIL);
String email = emailE==null ? null : emailE.getText();
List<String> groups = new ArrayList<String>();
Iterator<Element> groupElements = groupsDoc.getDocumentElement().getElementsByName(GROUP);
while (groupElements.hasNext()) {
groups.add(groupElements.next().getAttributeValue("alias"));
}
// we won't cache this
return new User(alias,uuid,name,email,groups);