searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtls.setCountLimit(maxItemLimit);
if (filter.contains("?") || filter.contains("**")) {
log.error("Invalid search character " + filter);
throw new DirectoryServerManagerException(
"Invalid character sequence entered for service principle search. Please enter valid sequence.");
}
StringBuffer searchFilter;
searchFilter = new StringBuffer(this.realmConfiguration.getUserStoreProperty(LDAPConstants.USER_FILTER));
String searchBase = this.realmConfiguration.getUserStoreProperty(LDAPConstants.USER_SEARCH_BASE);
StringBuffer buff = new StringBuffer();
buff.append("(&").append(searchFilter).append("(")
.append(LDAPServerManagerConstants.KRB5_PRINCIPAL_NAME_ATTRIBUTE).append("=")
.append(filter).append(")").append(getServerPrincipleIncludeString()).append(")");
String returnedAtts[] = {LDAPServerManagerConstants.KRB5_PRINCIPAL_NAME_ATTRIBUTE,
LDAPServerManagerConstants.LDAP_COMMON_NAME};
searchCtls.setReturningAttributes(returnedAtts);
DirContext dirContext = null;
try {
dirContext = connectionSource.getContext();
NamingEnumeration<SearchResult> answer = dirContext.search(searchBase, buff.toString(),
searchCtls);
List<ServerPrinciple> list = new ArrayList<ServerPrinciple>();
int i = 0;
while (answer.hasMoreElements() && i < maxItemLimit) {
SearchResult sr = answer.next();
if (sr.getAttributes() != null) {
Attribute serverNameAttribute = sr.getAttributes()
.get(LDAPServerManagerConstants.KRB5_PRINCIPAL_NAME_ATTRIBUTE);
Attribute serverDescription = sr.getAttributes().get(LDAPServerManagerConstants.LDAP_COMMON_NAME);
if (serverNameAttribute != null) {
ServerPrinciple principle;
String serviceName;
String serverPrincipleFullName = (String) serverNameAttribute.get();
if (serverPrincipleFullName.toLowerCase(Locale.ENGLISH)
.contains(LDAPServerManagerConstants.KERBEROS_TGT)) {
continue;
}
if (serverPrincipleFullName.contains("@")) {
serviceName = serverPrincipleFullName.split("@")[0];
} else {
serviceName = serverPrincipleFullName;
}
if (serverDescription != null) {
principle = new ServerPrinciple(serviceName,
(String) serverDescription.get());
} else {
principle = new ServerPrinciple(serviceName);
}
list.add(principle);
i++;
}
}
}
serverNames = list.toArray(new ServerPrinciple[list.size()]);
Arrays.sort(serverNames);
} catch (NamingException e) {
log.error(e.getMessage(), e);
throw new DirectoryServerManagerException("Unable to list service principles.", e);
} catch (UserStoreException e) {
log.error("Unable to retrieve LDAP connection context.", e);
throw new DirectoryServerManagerException("Unable to list service principles.", e);
} finally {
try {
JNDIUtil.closeContext(dirContext);
} catch (UserStoreException e) {
log.error("Unable to close directory context.", e);