use of org.apache.directory.ldap.client.api.LdapConnection in project directory-fortress-core by apache.
the class ExampleDAO method findExamples.
/**
* @param searchVal
* @return
* @throws org.apache.directory.fortress.core.FinderException
*/
public List<Example> findExamples(String searchVal) throws FinderException {
List<Example> exampleList = new ArrayList<>();
LdapConnection ld = null;
String exampleRoot = Config.getInstance().getProperty(EIds.EXAMPLE_ROOT);
if (LOG.isDebugEnabled()) {
LOG.debug("findExamples: " + EIds.EXAMPLE_ROOT + " [" + exampleRoot + "]");
}
try {
searchVal = encodeSafeText(searchVal, GlobalIds.ROLE_LEN);
ld = getAdminConnection();
String filter = GlobalIds.FILTER_PREFIX + Arrays.toString(EIds.EXAMPLE_OBJ_CLASS) + ")(" + EIds.EXAMPLE_NM + "=" + searchVal + "*))";
SearchCursor searchResults = search(ld, exampleRoot, SearchScope.SUBTREE, filter, EXAMPLE_ATRS, false, GlobalIds.BATCH_SIZE);
while (searchResults.next()) {
exampleList.add(getEntityFromLdapEntry(searchResults.getEntry()));
}
} catch (LdapException e) {
String error = "findExamples caught LDAPException=" + e;
LOG.warn(error);
throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error);
} catch (CursorException e) {
String error = "findExamples caught CursorException=" + e;
throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error, e);
} finally {
closeAdminConnection(ld);
}
return exampleList;
}
use of org.apache.directory.ldap.client.api.LdapConnection in project directory-fortress-core by apache.
the class ExampleDAO method create.
/**
* @param entity
* @return
* @throws org.apache.directory.fortress.core.CreateException
*/
public Example create(Example entity) throws CreateException {
LdapConnection ld = null;
String dn = SchemaConstants.CN_AT + "=" + entity.getName() + "," + Config.getInstance().getProperty(EIds.EXAMPLE_ROOT);
if (LOG.isDebugEnabled()) {
LOG.debug("create dn [" + dn + "]");
}
try {
/*
public class Example
implements Constraint, java.io.Serializable
{
private String id; // this maps to oamId
private String name; // this is oamRoleName
private String description; // this is description
private String dn; // this attribute is automatically saved to each ldap record.
private String beginTime; // this attribute is oamBeginTime
private String endTime; // this attribute is oamEndTime
private String beginDate; // this attribute is oamBeginDate
private String endDate; // this attribute is oamEndDate
private String beginLockDate;// this attribute is oamBeginLockDate
private String endLockDate; // this attribute is oamEndLockDate
private String dayMask; // this attribute is oamDayMask
private int timeout; // this attribute is oamTimeOut
*/
ld = getAdminConnection();
Entry entry = new DefaultEntry(dn);
entry.add(createAttributes(SchemaConstants.OBJECT_CLASS_AT, EIds.EXAMPLE_OBJ_CLASS));
entity.setId();
entry.add(GlobalIds.FT_IID, entity.getId());
entry.add(EIds.EXAMPLE_NM, entity.getName());
if (entity.getDescription() != null && entity.getDescription().length() > 0)
entry.add(SchemaConstants.DESCRIPTION_AT, entity.getDescription());
// organizational name requires CN attribute:
entry.add(SchemaConstants.CN_AT, entity.getName());
// AttrHelper.loadTemporalAttrs(entity, attrs);
entity.setName("EXAMPLE");
entry.add(GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint(entity));
add(ld, entry);
} catch (LdapException e) {
String error = "create [" + entity.getName() + "] caught LDAPException=" + e;
LOG.error(error);
throw new CreateException(EErrIds.EXAMPLE_ADD_FAILED, error);
} finally {
closeAdminConnection(ld);
}
return entity;
}
use of org.apache.directory.ldap.client.api.LdapConnection in project structr by structr.
the class LDAPService method doUpdate.
public void doUpdate() throws IOException, LdapException, CursorException, FrameworkException {
final LdapConnection connection = new LdapNetworkConnection(host, port, useSsl);
final App app = StructrApp.getInstance();
if (connection != null) {
// make connection persistent
connection.setTimeOut(0);
if (connection.connect()) {
logger.info("Updating user/group information from LDAP server {}:{}..", new Object[] { host, port });
if (StringUtils.isNotBlank(binddn) && StringUtils.isNotBlank(secret)) {
connection.bind(binddn, secret);
} else if (StringUtils.isNotBlank(binddn)) {
connection.bind(binddn);
}
// step 1: fetch / update all users from LDAP server
final EntryCursor cursor = connection.search(baseDn, filter, SearchScope.valueOf(scope));
while (cursor.next()) {
final Entry entry = cursor.get();
synchronizeUserEntry(connection, entry);
}
// step 2: examine local users and refresh / remove
try (final Tx tx = app.tx()) {
for (final LDAPUser user : app.nodeQuery(LDAPUser.class).getAsList()) {
final String dn = user.getDistinguishedName();
if (dn != null) {
final Entry userEntry = connection.lookup(dn);
if (userEntry != null) {
// update user information
user.initializeFrom(userEntry);
} else {
logger.info("User {} doesn't exist in LDAP directory, deleting.", user);
app.delete(user);
}
} else {
logger.warn("User {} doesn't have an LDAP distinguished name, ignoring.", user);
}
}
tx.success();
}
cursor.close();
connection.close();
} else {
logger.info("Connection to LDAP server {} failed", host);
}
}
}
use of org.apache.directory.ldap.client.api.LdapConnection in project structr by structr.
the class LDAPService method fetchObjectInfo.
// ----- public methods -----
public String fetchObjectInfo(final String dn) {
final LdapConnection connection = new LdapNetworkConnection(host, port, useSsl);
final StringBuilder buf = new StringBuilder();
if (connection != null) {
try {
if (connection.connect()) {
if (StringUtils.isNotBlank(binddn) && StringUtils.isNotBlank(secret)) {
connection.bind(binddn, secret);
} else if (StringUtils.isNotBlank(binddn)) {
connection.bind(binddn);
}
final EntryCursor cursor = connection.search(dn, "(objectclass=*)", SearchScope.OBJECT);
while (cursor.next()) {
buf.append(cursor.get());
buf.append("\n");
}
cursor.close();
connection.close();
}
connection.close();
} catch (CursorException | LdapException | IOException ex) {
logger.warn("", ex);
}
}
return buf.toString();
}
use of org.apache.directory.ldap.client.api.LdapConnection in project activemq-artemis by apache.
the class CachedLDAPAuthorizationModuleLegacyTest method getLdapConnection.
@Override
protected LdapConnection getLdapConnection() throws LdapException, IOException {
LdapConnection connection = new LdapNetworkConnection("localhost", getLdapServer().getPort());
connection.bind(new Dn("uid=admin,ou=system"), "secret");
return connection;
}
Aggregations