Skip to content

Instantly share code, notes, and snippets.

@yock
Created July 18, 2011 19:37
Show Gist options
  • Save yock/1090437 to your computer and use it in GitHub Desktop.
Save yock/1090437 to your computer and use it in GitHub Desktop.
Refactored (after)
protected String getUserIdFromPrincipal(Principal prin) {
if (prin == null) {
throw new IllegalArgumentException(
"User principal must not be null.");
}
logger.debug("URL encoded principal: " + prin);
String strPrincipal = null;
try {
strPrincipal = URLDecoder.decode(prin.getName(), UTF_8);
logger.debug("URL decoded principal: " + strPrincipal);
logger.info("Parsing LDAP principal name");
LdapName ldapName = new LdapName(strPrincipal);
strPrincipal = ldapName.getRdn(2).getValue().toString();
logger.info("User is decoded as: " + strPrincipal);
strPrincipal = strPrincipal.toUpperCase();
} catch (UnsupportedEncodingException e) {
/*
* Exception only thrown when URLDecode.decode is called with an
* invalid encoding. Since we're calling it with a literal value,
* this catch block cannot be reached. Nonetheless, a logging
* statement is included here in case this method ever changes.
*/
logger.error("Unsupported encoding", e);
} catch (InvalidNameException e) {
logger.error("LDAP name string is not valid", e);
throw new IllegalArgumentException(
"User principal must be an LDAP distinguished name");
}
// Transpose user id to upper case
return strPrincipal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment