protected long getUserID(String username) throws UserNotFoundException {
// Gets the part before of @ of the username param
if (username.contains("@")) {
// User's id are only for local users
if (!XMPPServer.getInstance().isLocal(new JID(username))) {
throw new UserNotFoundException("Cannot load user of remote server: " + username);
}
username = username.substring(0, username.lastIndexOf("@"));
}
// Checks if it is in the cache
if (userIDCache.containsKey(username)) {
return userIDCache.get(username);
}
// Un-escape username.
String unescapedUsername = JID.unescapeNode(username);
// Encode potentially non-ASCII characters
unescapedUsername = URLUTF8Encoder.encode(unescapedUsername);
// Gets the user's ID from Clearspace
try {
String path = ClearspaceUserProvider.USER_URL_PREFIX + "users/" + unescapedUsername;
Element element = executeRequest(org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.GET, path);
Long id = Long.valueOf(WSUtils.getElementText(element.selectSingleNode("return"), "ID"));
userIDCache.put(username, id);
return id;
} catch (UserNotFoundException unfe) {
// It is a supported exception, throw it again
throw unfe;
} catch (Exception e) {
// It is not a supported exception, wrap it into a UserNotFoundException
throw new UserNotFoundException("Unexpected error", e);
}
}