下面列出了javax.naming.NameParser#javax.naming.PartialResultException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convenience method that wraps {@link NamingEnumeration#hasMore()} but ignores {@link PartialResultException}s
* that may be thrown as a result. This is typically an issue with a directory server that does not support
* {@link Context#REFERRAL} being set to 'ignore' (which is the default value).
*
* Issue: https://github.com/stevespringett/Alpine/issues/19
* @since 1.4.3
*/
private boolean hasMoreEnum(final NamingEnumeration<SearchResult> ne) throws NamingException {
if (ne == null) {
return false;
}
boolean hasMore = true;
try {
if (!ne.hasMore()) {
hasMore = false;
}
} catch (PartialResultException e) {
hasMore = false;
LOGGER.warn("Partial results returned. If this is an Active Directory server, try using port 3268 or 3269 in " + Config.AlpineKey.LDAP_SERVER_URL.name());
}
return hasMore;
}
/**
* Add values of a specified attribute to a list
*
* @param attrId Attribute name
* @param attrs Attributes containing the new values
* @param values ArrayList containing values found so far
* @return the list of attribute values
* @exception NamingException if a directory server error occurs
*/
private ArrayList<String> addAttributeValues(String attrId,
Attributes attrs,
ArrayList<String> values)
throws NamingException{
if (containerLog.isTraceEnabled())
containerLog.trace(" retrieving values for attribute " + attrId);
if (attrId == null || attrs == null)
return values;
if (values == null)
values = new ArrayList<>();
Attribute attr = attrs.get(attrId);
if (attr == null)
return values;
NamingEnumeration<?> e = attr.getAll();
try {
while(e.hasMore()) {
String value = (String)e.next();
values.add(value);
}
} catch (PartialResultException ex) {
if (!adCompat)
throw ex;
} finally {
e.close();
}
return values;
}
/**
* Add values of a specified attribute to a list
*
* @param attrId Attribute name
* @param attrs Attributes containing the new values
* @param values ArrayList containing values found so far
*
* @exception NamingException if a directory server error occurs
*/
private ArrayList<String> addAttributeValues(String attrId,
Attributes attrs,
ArrayList<String> values)
throws NamingException{
if (containerLog.isTraceEnabled())
containerLog.trace(" retrieving values for attribute " + attrId);
if (attrId == null || attrs == null)
return values;
if (values == null)
values = new ArrayList<String>();
Attribute attr = attrs.get(attrId);
if (attr == null)
return (values);
NamingEnumeration<?> e = attr.getAll();
try {
while(e.hasMore()) {
String value = (String)e.next();
values.add(value);
}
} catch (PartialResultException ex) {
if (!adCompat)
throw ex;
}
return values;
}
/**
* Add values of a specified attribute to a list
*
* @param attrId Attribute name
* @param attrs Attributes containing the new values
* @param values ArrayList containing values found so far
*
* @exception NamingException if a directory server error occurs
*/
private ArrayList<String> addAttributeValues(String attrId,
Attributes attrs,
ArrayList<String> values)
throws NamingException{
if (containerLog.isTraceEnabled())
containerLog.trace(" retrieving values for attribute " + attrId);
if (attrId == null || attrs == null)
return values;
if (values == null)
values = new ArrayList<String>();
Attribute attr = attrs.get(attrId);
if (attr == null)
return values;
NamingEnumeration<?> e = attr.getAll();
try {
while(e.hasMore()) {
String value = (String)e.next();
values.add(value);
}
} catch (PartialResultException ex) {
if (!adCompat)
throw ex;
} finally {
e.close();
}
return values;
}
@Test
public void search_withPartialResultException() throws Exception {
// given
initAttribute(SEARCH_ATTRIBUTES[0], aMock);
initAttribute(SEARCH_ATTRIBUTES[1], aMock);
initAttribute(SEARCH_ATTRIBUTES[2], aMock);
when(Boolean.valueOf(neMock.hasMore())).thenReturn(Boolean.TRUE)
.thenThrow(new PartialResultException());
Map<SettingType, String> map = new HashMap<SettingType, String>();
map.put(SettingType.LDAP_ATTR_UID, SEARCH_ATTRIBUTES[0]);
map.put(SettingType.LDAP_ATTR_FIRST_NAME, SEARCH_ATTRIBUTES[1]);
map.put(SettingType.LDAP_ATTR_EMAIL, SEARCH_ATTRIBUTES[2]);
ILdapResultMapper<VOUserDetails> mapper = new LdapVOUserDetailsMapper(
null, map);
List<VOUserDetails> list = new ArrayList<VOUserDetails>();
// when
list = bean.search(new Properties(), "baseDN", "filter", mapper, false);
// then
assertNotNull(list);
assertEquals(1, list.size());
VOUserDetails det = list.get(0);
assertEquals(SEARCH_ATTRIBUTES[0], det.getRealmUserId());
assertEquals(SEARCH_ATTRIBUTES[1], det.getFirstName());
assertEquals(SEARCH_ATTRIBUTES[2], det.getEMail());
}
private boolean hasMoreEnum(NamingEnumeration<SearchResult> namingEnum)
throws NamingException {
boolean hasMore = true;
try {
if (!namingEnum.hasMore()) {
hasMore = false;
}
} catch (PartialResultException e) {
hasMore = false;
logger.logWarn(Log4jLogger.SYSTEM_LOG, e,
LogMessageIdentifier.WARN_LDAP_PARTIAL_EXCEPTION);
}
return hasMore;
}
public String getFirstSearchResult(NamingEnumeration<SearchResult> searchResultEnum) throws NamingException {
String result=null;
try {
if (searchResultEnum.hasMore()) {
result=getFirstAttribute(searchResultEnum.next());
}
} catch(PartialResultException e) {
if (log.isDebugEnabled()) log.debug("ignoring Exception: "+e);
} finally {
searchResultEnum.close();
}
return result;
}
public void mapMultiValuedAttribute(NamingEnumeration<SearchResult> searchResultEnum, Callback<Attribute,Object> callback) throws NamingException {
try {
while (searchResultEnum.hasMore()) {
Attributes attributes=searchResultEnum.next().getAttributes();
NamingEnumeration<? extends Attribute> attrenum=attributes.getAll();
try {
while (attrenum.hasMore()) {
Attribute attr=attrenum.next();
NamingEnumeration<?> multivalueattribute=attr.getAll();
try {
while (multivalueattribute.hasMore()) {
callback.handle(attr,multivalueattribute.next());
}
} finally {
multivalueattribute.close();
}
}
} finally {
attrenum.close();
}
}
} catch(PartialResultException e) {
if (log.isDebugEnabled()) log.debug("ignoring Exception: "+e);
} finally {
searchResultEnum.close();
}
}
/**
* runs a set of attribute values through a Mapper. Only the first value of each attribute is mapped.
*/
public void mapMultipleAttributes(NamingEnumeration<SearchResult> searchResultEnum, Callback<Attribute,Object> callback) throws NamingException {
try {
while (searchResultEnum.hasMore()) {
Attributes attributes=searchResultEnum.next().getAttributes();
NamingEnumeration<? extends Attribute> attrenum=attributes.getAll();
try {
while (attrenum.hasMore()) {
Attribute attr=attrenum.next();
NamingEnumeration<?> multivalueattribute=attr.getAll();
try {
if (multivalueattribute.hasMore()) {
callback.handle(attr,multivalueattribute.next());
}
} finally {
multivalueattribute.close();
}
}
} finally {
attrenum.close();
}
}
} catch(PartialResultException e) {
if (log.isDebugEnabled()) log.debug("ignoring Exception: "+e);
} finally {
searchResultEnum.close();
}
}
private Collection<String> retrieveGroupsByFilter(DirContext ctx, DirContext referralCtx, String userDN) {
Collection<String> groupNames = new HashSet<>();
try {
SearchGroupsUsingFilter groupRetrieval = (SearchGroupsUsingFilter) getGroupRetrieval();
String groupNameAttribute = groupRetrieval.getGroupNameAttribute();
Name groupSearchBase = new CompositeName().add(groupRetrieval.getGroupSearchBase());
String groupSearchFilter = StringUtils.replace(groupRetrieval.getGroupSearchFilter(), "{0}", userDN);
groupSearchFilter = StringUtils.replace(groupSearchFilter, "\\", "\\\\");
logger.debug("Evaluated group search filter: " + groupSearchFilter);
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setReturningAttributes(new String[]{groupNameAttribute});
searchControls.setReturningObjFlag(true);
NamingEnumeration<SearchResult> results;
if (referralCtx != null)
results = referralCtx.search(groupSearchBase, groupSearchFilter, searchControls);
else
results = ctx.search(groupSearchBase, groupSearchFilter, searchControls);
if (results != null) {
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes searchResultAttributes = searchResult.getAttributes();
if (searchResultAttributes == null
|| searchResultAttributes.get(groupNameAttribute) == null
|| searchResultAttributes.get(groupNameAttribute).get() == null) {
throw new RuntimeException("Can not find attribute '"
+ groupNameAttribute + "' in the returned group object.");
}
groupNames.add((String) searchResultAttributes.get(groupNameAttribute).get());
}
}
} catch (PartialResultException pre) {
logger.warn("Partial exception detected. You may try to set property " +
"'follow referrals' to true to avoid this exception.", pre);
} catch (NamingException e) {
logger.error("Error retrieving groups by filter", e);
}
return groupNames;
}