use of org.forgerock.opendj.ldap.Attribute in project OpenAM by OpenRock.
the class LdapTokenAttributeConversion method mapFromEntry.
/**
* Convert an Entry into a more convenient Mapping of CoreTokenField to Object.
*
* This function is important because no every operation with LDAP needs to return a
* fully initialised Token. Instead users may be interested in only certain
* attributes of the Token, and choose to query just those as a performance enhancement.
*
* @param entry Non null entry to convert.
*
* @return A mapping of zero or more CoreTokenFields to Objects.
*/
public Map<CoreTokenField, Object> mapFromEntry(Entry entry) {
stripObjectClass(entry);
Map<CoreTokenField, Object> r = new LinkedHashMap<>();
for (Attribute a : entry.getAllAttributes()) {
AttributeDescription description = a.getAttributeDescription();
CoreTokenField field = CoreTokenField.fromLDAPAttribute(description.toString());
// Special case for Token Type
if (CoreTokenField.TOKEN_TYPE.equals(field)) {
String value = entry.parseAttribute(description).asString();
r.put(field, TokenType.valueOf(value));
continue;
}
if (CoreTokenFieldTypes.isCalendar(field)) {
String dateString = entry.parseAttribute(description).asString();
Calendar calendar = conversion.fromLDAPDate(dateString);
r.put(field, calendar);
} else if (CoreTokenFieldTypes.isString(field)) {
String value = entry.parseAttribute(description).asString();
if (EMPTY.equals(value)) {
value = "";
}
r.put(field, value);
} else if (CoreTokenFieldTypes.isInteger(field)) {
Integer value = entry.parseAttribute(description).asInteger();
r.put(field, value);
} else if (CoreTokenFieldTypes.isByteArray(field)) {
byte[] data = entry.parseAttribute(description).asByteString().toByteArray();
r.put(field, data);
} else {
throw new IllegalStateException();
}
}
return r;
}
use of org.forgerock.opendj.ldap.Attribute in project OpenAM by OpenRock.
the class IndexChangeHandler method handleEntry.
@Override
public boolean handleEntry(SearchResultEntry entry) {
EntryChangeNotificationResponseControl control = null;
try {
// Retrieve details of the policy change.
control = entry.getControl(EntryChangeNotificationResponseControl.DECODER, new DecodeOptions());
} catch (DecodeException dE) {
DEBUG.error("Error occurred attempting to read policy rule change.", dE);
// Notify observers of the exception and proceed no further.
observable.notifyObservers(ErrorEventType.SEARCH_FAILURE.createEvent());
return true;
}
// Extract the realm from the DN to be passed as part of the event.
String dn = entry.getName().toString();
String orgName = dn.substring(dn.indexOf(SERVICE_DECLARATION) + SERVICE_DECLARATION.length());
String realm = dnMapper.orgNameToRealmName(orgName);
// Retrieve all sunxmlKeyValue attributes.
Attribute attributes = entry.getAttribute(AttributeDescription.valueOf("sunxmlKeyValue"));
for (ByteString attrValue : attributes) {
String attStrValue = attrValue.toString();
if (attStrValue.startsWith(INDEX_PATH_ATT)) {
// Extract the path index out of the attribute value.
String pathIndex = attStrValue.substring(INDEX_PATH_ATT.length() + 1);
switch(control.getChangeType()) {
case MODIFY:
// this will result in the old index remaining.
case ADD:
observable.notifyObservers(ModificationEventType.ADD.createEvent(pathIndex, realm));
break;
case DELETE:
observable.notifyObservers(ModificationEventType.DELETE.createEvent(pathIndex, realm));
break;
}
}
}
return true;
}
use of org.forgerock.opendj.ldap.Attribute in project OpenAM by OpenRock.
the class LdifUtils method createSchemaFromLDIF.
/**
* Creates LDAP schema from LDIF file.
*
* @param ldif LDIF object.
* @param ld LDAP Connection.
* @throws IOException If an error occurs when reading the LDIF file.
*/
public static void createSchemaFromLDIF(LDIFChangeRecordReader ldif, final Connection ld) throws IOException {
while (ldif.hasNext()) {
final ChangeRecord changeRecord = ldif.readChangeRecord();
changeRecord.accept(new ChangeRecordVisitor<Void, Void>() {
@Override
public Void visitChangeRecord(Void aVoid, AddRequest change) {
try {
change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
ld.add(change);
} catch (LdapException e) {
if (ResultCode.ENTRY_ALREADY_EXISTS.equals(e.getResult().getResultCode())) {
for (Attribute attr : change.getAllAttributes()) {
ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(change.getName());
modifyRequest.addModification(new Modification(ModificationType.ADD, attr));
try {
ld.modify(modifyRequest);
} catch (LdapException ex) {
DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", modifyRequest, ex);
}
}
} else {
DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not add to schema: {}", change, e);
}
}
return null;
}
@Override
public Void visitChangeRecord(Void aVoid, ModifyRequest change) {
try {
change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
ld.modify(change);
} catch (LdapException e) {
DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", change, e);
}
return null;
}
@Override
public Void visitChangeRecord(Void aVoid, ModifyDNRequest change) {
return null;
}
@Override
public Void visitChangeRecord(Void aVoid, DeleteRequest change) {
DEBUG.message("Delete request ignored: {}", changeRecord);
return null;
}
}, null);
}
}
use of org.forgerock.opendj.ldap.Attribute in project OpenAM by OpenRock.
the class DataLayer method getAttributes.
/**
* Returns attributes for the given attribute names.
*
* @param principal Authentication Principal.
* @param guid Distinguished name.
* @param attrNames Attribute names.
* @return collection of Attr.
*
* @supported.api
*/
public Collection<Attr> getAttributes(Principal principal, Guid guid, Collection<String> attrNames) {
String id = guid.getDn();
SearchRequest request = LDAPRequests.newSearchRequest(id, SearchScope.BASE_OBJECT, "(objectclass=*)", attrNames.toArray(EMPTY_STRING_ARRAY));
ConnectionEntryReader ldapEntry;
try {
ldapEntry = readLDAPEntry(principal, request);
if (ldapEntry == null) {
debug.warning("No attributes returned may not have permission to read");
return Collections.emptySet();
}
Collection<Attr> attributes = new ArrayList<>();
while (ldapEntry.hasNext()) {
if (ldapEntry.isEntry()) {
SearchResultEntry entry = ldapEntry.readEntry();
for (Attribute attr : entry.getAllAttributes()) {
attributes.add(new Attr(attr));
}
}
}
return attributes;
} catch (Exception e) {
debug.warning("Exception in DataLayer.getAttributes for DN: {}", id, e);
return null;
}
}
use of org.forgerock.opendj.ldap.Attribute in project OpenAM by OpenRock.
the class DataLayer method addEntry.
/**
* Adds entry to the server.
*
* @param principal Authenticated Principal.
* @param guid Distinguished name.
* @param attrSet attribute set containing name/value pairs.
* @exception AccessRightsException if insufficient access>
* @exception EntryAlreadyExistsException if the entry already exists.
* @exception UMSException if fail to add entry.
*
* @supported.api
*/
public void addEntry(java.security.Principal principal, Guid guid, AttrSet attrSet) throws UMSException {
String id = guid.getDn();
ResultCode errorCode;
try {
AddRequest request = LDAPRequests.newAddRequest(id);
for (Attribute attribute : attrSet.toLDAPAttributeSet()) {
request.addAttribute(attribute);
}
int retry = 0;
while (retry <= connNumRetry) {
if (debug.messageEnabled()) {
debug.message("DataLayer.addEntry retry: " + retry);
}
try (Connection conn = getConnection(principal)) {
conn.add(request);
return;
} catch (LdapException e) {
errorCode = e.getResult().getResultCode();
if (!retryErrorCodes.contains(errorCode) || retry == connNumRetry) {
throw e;
}
retry++;
try {
Thread.sleep(connRetryInterval);
} catch (InterruptedException ex) {
}
}
}
} catch (LdapException e) {
if (debug.warningEnabled()) {
debug.warning("Exception in DataLayer.addEntry for DN: " + id, e);
}
errorCode = e.getResult().getResultCode();
String[] args = { id };
if (ResultCode.ENTRY_ALREADY_EXISTS.equals(errorCode)) {
throw new EntryAlreadyExistsException(i18n.getString(IUMSConstants.ENTRY_ALREADY_EXISTS, args), e);
} else if (ResultCode.INSUFFICIENT_ACCESS_RIGHTS.equals(errorCode)) {
throw new AccessRightsException(i18n.getString(IUMSConstants.INSUFFICIENT_ACCESS_ADD, args), e);
} else {
throw new UMSException(i18n.getString(IUMSConstants.UNABLE_TO_ADD_ENTRY, args), e);
}
}
}
Aggregations