private static DefaultIncrementalAttributesMapper loopForAllAttributeValues(LdapOperations ldapOperations, Name dn, String[] attributes) { DefaultIncrementalAttributesMapper mapper = new DefaultIncrementalAttributesMapper(attributes); while (mapper.hasMore()) { ldapOperations.lookup(dn, mapper.getAttributesForLookup(), mapper); } return mapper; }
/** * Lookup all values for the specified attribute, looping through the results incrementally if necessary. * * @param ldapOperations The instance to use for performing the actual lookup. * @param dn The distinguished name of the object to find. * @param attribute name of the attribute to request. * @return a list with all attribute values found for the requested attribute. * Never <code>null</code>, an empty list indicates that the attribute was not set or empty. */ public static List<Object> lookupAttributeValues(LdapOperations ldapOperations, Name dn, String attribute) { List<Object> values = loopForAllAttributeValues(ldapOperations, dn, new String[]{attribute}).getValues(attribute); if(values == null) { values = Collections.emptyList(); } return values; }
/** * Lookup all values for the specified attributes, looping through the results incrementally if necessary. * * @param ldapOperations The instance to use for performing the actual lookup. * @param dn The distinguished name of the object to find. * @param attributes names of the attributes to request. * @return an Attributes instance, populated with all found values for the requested attributes. * Never <code>null</code>, though the actual attributes may not be set if they was not * set on the requested object. */ public static Attributes lookupAttributes(LdapOperations ldapOperations, Name dn, String[] attributes) { return loopForAllAttributeValues(ldapOperations, dn, attributes).getCollectedAttributes(); }
@Test public void test1LoopWithPageSizeExact() throws Exception { tested = new DefaultIncrementalAttributesMapper(10, "member"); Attributes attributes = createAttributes("member", new RangeOption(0, 10)); tested.mapFromAttributes(attributes); assertThat(tested.hasMore()).isFalse(); assertThat(tested.getValues("member")).hasSize(11); }
DefaultIncrementalAttributesMapper attributeMapper = new DefaultIncrementalAttributesMapper(new String[]{"member"}); while (attributeMapper.hasMore()) { ldapTemplate.lookup(GROUP_DN, attributeMapper.getAttributesForLookup(), attributeMapper); DefaultIncrementalAttributesMapper.lookupAttributeValues( ldapTemplate, GROUP_DN, "member").size()).isEqualTo(1501);
@Override public final DefaultIncrementalAttributesMapper mapFromAttributes(Attributes attributes) throws NamingException { if (!hasMore()) { throw new IllegalStateException("No more attributes!"); } // Reset the affected attributes. rangedAttributesInNextIteration = new HashSet<String>(); NamingEnumeration<String> attributeNameEnum = attributes.getIDs(); while (attributeNameEnum.hasMore()) { String attributeName = attributeNameEnum.next(); String[] attributeNameSplit = attributeName.split(";"); IncrementalAttributeState state = getState(attributeNameSplit[0]); if (attributeNameSplit.length == 1) { // No range specification for this attribute state.processValues(attributes, attributeName); } else { for (String option : attributeNameSplit) { RangeOption responseRange = RangeOption.parse(option); if (responseRange != null) { state.processValues(attributes, attributeName); state.calculateNextRange(responseRange); if (state.hasMore()) { rangedAttributesInNextIteration.add(state.getRequestedAttributeName()); } } } } } return this; }
@Test public void testLoopEmpty() throws Exception { assertThat(tested.hasMore()).isTrue(); Attributes attributes = new BasicAttributes(); tested.mapFromAttributes(attributes); assertThat(tested.hasMore()).isFalse(); assertThat(tested.getValues("member")).isNull(); }
@Test public void testGetAttributesArray() throws Exception { String[] attributes = tested.getAttributesForLookup(); assertThat(attributes.length).isEqualTo(1); assertThat(attributes[0]).isEqualTo("member"); tested = new DefaultIncrementalAttributesMapper(10, "member"); attributes = tested.getAttributesForLookup(); assertThat(attributes.length).isEqualTo(1); assertThat(attributes[0]).isEqualTo("member;Range=0-10"); }
/** * Get an {@link AttributesMapper} that just returns the supplied * Attributes. * * @return the {@link AttributesMapper} to use for getting the current * Attributes of the target DN. */ IncrementalAttributesMapper<?> getAttributesMapper(String[] attributeNames) { return new DefaultIncrementalAttributesMapper(attributeNames); }
/** * Lookup all values for the specified attribute, looping through the results incrementally if necessary. * * @param ldapOperations The instance to use for performing the actual lookup. * @param dn The distinguished name of the object to find. * @param attribute name of the attribute to request. * @return an Attributes instance, populated with all found values for the requested attribute. * Never <code>null</code>, though the actual attribute may not be set if it was not * set on the requested object. */ public static Attributes lookupAttributes(LdapOperations ldapOperations, Name dn, String attribute) { return lookupAttributes(ldapOperations, dn, new String[]{attribute}); }
@Override public final Attributes getCollectedAttributes() { BasicAttributes attributes = new BasicAttributes(); Set<String> attributeNames = stateMap.keySet(); for (String attributeName : attributeNames) { BasicAttribute oneAttribute = new BasicAttribute(attributeName); List<Object> values = getValues(attributeName); if (values != null) { for (Object oneValue : values) { oneAttribute.add(oneValue); } } attributes.put(oneAttribute); } return attributes; }
/** * Lookup all values for the specified attribute, looping through the results incrementally if necessary. * * @param ldapOperations The instance to use for performing the actual lookup. * @param dn The distinguished name of the object to find. * @param attribute name of the attribute to request. * @return a list with all attribute values found for the requested attribute. * Never <code>null</code>, an empty list indicates that the attribute was not set or empty. */ public static List<Object> lookupAttributeValues(LdapOperations ldapOperations, String dn, String attribute) { return lookupAttributeValues(ldapOperations, LdapUtils.newLdapName(dn), attribute); }
@Override public final List<Object> getValues(String attributeName) { return getState(attributeName).getValues(); }
@Test public void test2LoopsWithPageSizeExact() throws Exception { tested = new DefaultIncrementalAttributesMapper(20, "member"); Attributes attributes = createAttributes("member", new RangeOption(0, 10)); tested.mapFromAttributes(attributes); assertThat(tested.hasMore()).isTrue(); assertThat(tested.getValues("member")).hasSize(11); attributes = createAttributes("member", new RangeOption(11, 30)); tested.mapFromAttributes(attributes); assertThat(tested.hasMore()).isFalse(); assertThat(tested.getValues("member")).hasSize(31); }
@Override public final DefaultIncrementalAttributesMapper mapFromAttributes(Attributes attributes) throws NamingException { if (!hasMore()) { throw new IllegalStateException("No more attributes!"); } // Reset the affected attributes. rangedAttributesInNextIteration = new HashSet<String>(); NamingEnumeration<String> attributeNameEnum = attributes.getIDs(); while (attributeNameEnum.hasMore()) { String attributeName = attributeNameEnum.next(); String[] attributeNameSplit = attributeName.split(";"); IncrementalAttributeState state = getState(attributeNameSplit[0]); if (attributeNameSplit.length == 1) { // No range specification for this attribute state.processValues(attributes, attributeName); } else { for (String option : attributeNameSplit) { RangeOption responseRange = RangeOption.parse(option); if (responseRange != null) { state.processValues(attributes, attributeName); state.calculateNextRange(responseRange); if (state.hasMore()) { rangedAttributesInNextIteration.add(state.getRequestedAttributeName()); } } } } } return this; }
@Test public void testLoop() throws Exception { Attributes attributes = createAttributes("member", new RangeOption(0, 10)); tested.mapFromAttributes(attributes); assertThat(tested.hasMore()).isTrue(); assertThat(tested.getValues("member")).hasSize(11); attributes = createAttributes("member", new RangeOption(11), 5); tested.mapFromAttributes(attributes); assertThat(tested.hasMore()).isFalse(); assertThat(tested.getValues("member")).hasSize(16); }
@Test public void testGetAttributesArrayWithTwoAttributes() { tested = new DefaultIncrementalAttributesMapper(20, new String[]{"member", "cn"}); String[] attributes = tested.getAttributesForLookup(); assertThat(attributes.length).isEqualTo(2); assertThat(attributes[0]).isEqualTo("member;Range=0-20"); assertThat(attributes[1]).isEqualTo("cn;Range=0-20"); }
/** * Get an {@link AttributesMapper} that just returns the supplied * Attributes. * * @return the {@link AttributesMapper} to use for getting the current * Attributes of the target DN. */ IncrementalAttributesMapper<?> getAttributesMapper(String[] attributeNames) { return new DefaultIncrementalAttributesMapper(attributeNames); }
/** * Lookup all values for the specified attribute, looping through the results incrementally if necessary. * * @param ldapOperations The instance to use for performing the actual lookup. * @param dn The distinguished name of the object to find. * @param attribute name of the attribute to request. * @return an Attributes instance, populated with all found values for the requested attribute. * Never <code>null</code>, though the actual attribute may not be set if it was not * set on the requested object. */ public static Attributes lookupAttributes(LdapOperations ldapOperations, Name dn, String attribute) { return lookupAttributes(ldapOperations, dn, new String[]{attribute}); }
@Override public final Attributes getCollectedAttributes() { BasicAttributes attributes = new BasicAttributes(); Set<String> attributeNames = stateMap.keySet(); for (String attributeName : attributeNames) { BasicAttribute oneAttribute = new BasicAttribute(attributeName); List<Object> values = getValues(attributeName); if (values != null) { for (Object oneValue : values) { oneAttribute.add(oneValue); } } attributes.put(oneAttribute); } return attributes; }