use of org.forgerock.opendj.ldap.LinkedHashMapEntry in project OpenAM by OpenRock.
the class LdapTokenAttributeConversionTest method shouldUnderstandEmptyStrings.
@Test
public void shouldUnderstandEmptyStrings() {
// Given
Entry entry = new LinkedHashMapEntry();
entry.addAttribute(CoreTokenField.TOKEN_ID.toString(), "id");
entry.addAttribute(CoreTokenField.TOKEN_TYPE.toString(), TokenType.OAUTH.toString());
entry.addAttribute(CoreTokenField.STRING_ONE.toString(), LdapTokenAttributeConversion.EMPTY);
LdapTokenAttributeConversion conversion = generateTokenAttributeConversion();
// When
Token result = conversion.tokenFromEntry(entry);
// Then
String string = result.getValue(CoreTokenField.STRING_ONE);
assertTrue(string.isEmpty());
}
use of org.forgerock.opendj.ldap.LinkedHashMapEntry in project OpenAM by OpenRock.
the class DJLDAPv3Repo method create.
/**
* Creates a new identity using the passed in attributes. The following steps will be performed with the passed in
* data:
* <ul>
* <li>The password will be encoded in case we are dealing with AD.</li>
* <li>If the attribute map contains the default status attribute, then it will be converted to the status values
* specified in the configuration.</li>
* <li>Performing creation attribute mapping, so certain attributes can have default values (coming from other
* attributes, or from the identity name if there is no mapping for the attribute).</li>
* <li>Removes all attributes that are not defined in the configuration.</li>
* </ul>
* If the default group member setting is being used and a new group identity is being created, the newly created
* group will also have the default group member assigned.
*
* @param token Not used.
* @param type The type of the identity.
* @param name The name of the identity.
* @param attrMap The attributes of the new identity, that needs to be stored.
* @return The DN of the newly created identity
* @throws IdRepoException If there is an error while creating the new identity, or if it's a group and there is a
* problem while adding the default group member.
*/
@Override
public String create(SSOToken token, IdType type, String name, Map<String, Set<String>> attrMap) throws IdRepoException {
if (DEBUG.messageEnabled()) {
DEBUG.message("Create invoked on " + type + ": " + name + " attrMap = " + IdRepoUtils.getAttrMapWithoutPasswordAttrs(attrMap, null));
}
String dn = generateDN(type, name);
Set<String> objectClasses = getObjectClasses(type);
//First we should make sure that we wrap the attributes with a case insensitive hashmap.
attrMap = new CaseInsensitiveHashMap(attrMap);
byte[] encodedPwd = helper.encodePassword(type, attrMap.get(AD_UNICODE_PWD_ATTR));
//Let's set the userstatus as it is configured in the datastore.
mapUserStatus(type, attrMap);
//In case some attributes are missing use the create attribute mapping to get those values.
mapCreationAttributes(type, name, attrMap);
//and lastly we should make sure that we get rid of the attributes that are not known by the datastore.
attrMap = removeUndefinedAttributes(type, attrMap);
Set<String> ocs = attrMap.get(OBJECT_CLASS_ATTR);
if (ocs != null) {
ocs.addAll(objectClasses);
} else {
attrMap.put(OBJECT_CLASS_ATTR, objectClasses);
}
attrMap.put(getSearchAttribute(type), asSet(name));
Entry entry = new LinkedHashMapEntry(dn);
Set<String> attributeValue;
for (Map.Entry<String, Set<String>> attr : attrMap.entrySet()) {
// Add only attributes whose values are not empty or null
attributeValue = attr.getValue();
if (attributeValue != null && !attributeValue.isEmpty()) {
entry.addAttribute(attr.getKey(), attributeValue.toArray());
}
}
if (type.equals(IdType.GROUP) && defaultGroupMember != null) {
entry.addAttribute(uniqueMemberAttr, defaultGroupMember);
}
if (encodedPwd != null) {
entry.replaceAttribute(AD_UNICODE_PWD_ATTR, encodedPwd);
}
Connection conn = null;
try {
conn = connectionFactory.getConnection();
conn.add(LDAPRequests.newAddRequest(entry));
if (type.equals(IdType.GROUP) && defaultGroupMember != null) {
if (memberOfAttr != null) {
ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(defaultGroupMember);
modifyRequest.addModification(ModificationType.ADD, memberOfAttr, dn);
conn.modify(modifyRequest);
}
}
} catch (LdapException ere) {
DEBUG.error("Unable to add a new entry: " + name + " attrMap: " + IdRepoUtils.getAttrMapWithoutPasswordAttrs(attrMap, null), ere);
if (ResultCode.ENTRY_ALREADY_EXISTS.equals(ere.getResult().getResultCode())) {
throw IdRepoDuplicateObjectException.nameAlreadyExists(name);
} else {
handleErrorResult(ere);
}
} finally {
IOUtils.closeIfNotNull(conn);
}
return dn;
}
use of org.forgerock.opendj.ldap.LinkedHashMapEntry in project OpenAM by OpenRock.
the class LdapTokenAttributeConversion method getEntry.
/**
* Generate an Entry based on the given Token.
*
* @param token Non null Token to base the Entry on.
*
* @return An Entry suitable for LDAP operations. Includes the Object Class.
*/
public Entry getEntry(Token token) {
Entry entry = new LinkedHashMapEntry(generateTokenDN(token));
addObjectClass(entry);
for (CoreTokenField field : token.getAttributeNames()) {
String key = field.toString();
// Token Type special case is an Enum
if (CoreTokenField.TOKEN_TYPE.equals(field)) {
TokenType type = token.getValue(field);
entry.addAttribute(key, type.name());
continue;
}
if (CoreTokenFieldTypes.isCalendar(field)) {
Calendar calendar = token.getValue(field);
String dateString = conversion.toLDAPDate(calendar);
entry.addAttribute(key, dateString);
} else if (CoreTokenFieldTypes.isByteArray(field)) {
byte[] array = token.getValue(field);
entry.addAttribute(key, array);
} else if (CoreTokenFieldTypes.isInteger(field)) {
Integer value = token.getValue(field);
entry.addAttribute(key, value);
} else if (CoreTokenFieldTypes.isString(field)) {
String value = token.getValue(field);
if (!value.isEmpty()) {
entry.addAttribute(key, value);
}
} else {
throw new IllegalStateException();
}
}
return entry;
}
use of org.forgerock.opendj.ldap.LinkedHashMapEntry in project OpenAM by OpenRock.
the class LdapQueryBuilderTest method shouldReturnTokensFromSearch.
@Test
public void shouldReturnTokensFromSearch() throws CoreTokenException {
// Given
final Collection<Entry> entries = new LinkedList<Entry>();
entries.add(new LinkedHashMapEntry());
entries.add(new LinkedHashMapEntry());
// Slightly more fiddly mocking to provide behaviour when the mock is called.
given(searchHandler.performSearch(any(Connection.class), any(SearchRequest.class), any(Collection.class))).will(new Answer() {
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Collection<Entry> list = (Collection<Entry>) invocationOnMock.getArguments()[2];
list.addAll(entries);
return null;
}
});
// Ensure that the Token Conversion returns a Token
given(tokenEntryConverter.convert(any(Entry.class), any(String[].class))).willReturn(new Token(Long.toString(System.currentTimeMillis()), TokenType.SESSION));
// When
Iterator<Collection<Token>> results = builder.execute(mockConnection);
// Then
verifyZeroInteractions(tokenEntryConverter);
assertThat(results.next().size()).isEqualTo(entries.size());
verify(tokenEntryConverter, times(2)).convert(any(Entry.class), any(String[].class));
}
Aggregations