use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project openmeetings by apache.
the class LdapLoginManager method importUsers.
public void importUsers(Long domainId, boolean print) throws OmException {
try (LdapWorker w = new LdapWorker(domainId)) {
bindAdmin(w.conn, w.options);
Dn baseDn = new Dn(w.options.searchBase);
try (EntryCursor cursor = new EntryCursorImpl(w.conn.search(new SearchRequestImpl().setBase(baseDn).setFilter(w.options.importQuery).setScope(w.options.scope).addAttributes("*").setDerefAliases(w.options.derefMode)))) {
while (cursor.next()) {
try {
Entry e = cursor.get();
User u = userDao.getByLogin(getLogin(w.config, e), Type.ldap, domainId);
u = w.getUser(e, u);
if (print) {
log.info("Going to import user: {}", u);
} else {
userDao.update(u, null);
log.info("User {}, was imported", u);
}
} catch (CursorLdapReferralException cle) {
log.warn("Referral LDAP entry found, ignore it");
}
}
}
} catch (LdapAuthenticationException ae) {
log.error("Not authenticated.", ae);
throw BAD_CREDENTIALS;
} catch (OmException e) {
throw e;
} catch (Exception e) {
log.error("Unexpected exception.", e);
throw new OmException(e);
}
}
use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project account-identity by cryptofiat.
the class LdapService method lookupIdCode.
public LdapResponse lookupIdCode(long idCode) {
LdapResponse lResponse = LdapResponse.builder().build();
lResponse = tryLocalCache(idCode);
if (lResponse != null && lResponse.getIdCode() > 0) {
return lResponse;
}
LdapNetworkConnection connection = new LdapNetworkConnection("ldap.sk.ee");
try {
connection.bind();
EntryCursor cursor = connection.search("c=EE", "(serialNumber=" + String.valueOf(idCode) + ")", SearchScope.SUBTREE, "*");
while (cursor.next()) {
Entry entry = cursor.get();
log.info("got an entry: " + entry.toString());
String cn = entry.get("cn").getString();
lResponse = LdapResponse.builder().idCode(Long.valueOf(idCode)).firstName(cn.split(",")[1]).lastName(cn.split(",")[0]).build();
}
connection.unBind();
connection.close();
} catch (Exception e) {
log.error("Exception trying LDAP " + e.toString());
}
if (lResponse != null && lResponse.getIdCode() > 0) {
storeLocalCache(lResponse);
return lResponse;
} else {
return null;
}
}
use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project directory-ldap-api by apache.
the class LdapNetworkConnection method fetchRootDSE.
/**
* fetches the rootDSE from the server
* @throws LdapException
*/
private void fetchRootDSE(String... explicitAttributes) throws LdapException {
EntryCursor cursor = null;
String[] attributes = explicitAttributes;
if (attributes.length == 0) {
attributes = new String[] { SchemaConstants.ALL_USER_ATTRIBUTES, SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES };
}
try {
cursor = search("", LdapConstants.OBJECT_CLASS_STAR, SearchScope.OBJECT, attributes);
if (cursor.next()) {
rootDse = cursor.get();
} else {
throw new LdapException("Search for root DSE returned no entry");
}
} catch (Exception e) {
String msg = "Failed to fetch the RootDSE";
LOG.error(msg);
throw new LdapException(msg, e);
} finally {
if (cursor != null) {
try {
cursor.close();
} catch (Exception e) {
LOG.error(I18n.err(I18n.ERR_03201_CURSOR_CLOSE_FAIL), e);
}
}
}
}
use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project gpconnect-demonstrator by nhsconnect.
the class EndpointResolver method ldapQueryRequest.
private List<Collection<Attribute>> ldapQueryRequest(String queryBase, String queryFilter) throws IOException {
String uuid = java.util.UUID.randomUUID().toString();
List<Collection<Attribute>> returnList = new ArrayList<>();
LdapNetworkConnection connection = null;
LOG.debug(uuid + " ldapSDSQuery (Base:" + queryBase + " Filter:" + queryFilter + ")");
try {
connection = new LdapNetworkConnection(ldapUrl, ldapPort, ldapUseSSL);
if (serverKeyManager == null && trustManager == null) {
// Create Key Manager
try (FileInputStream keystoreInputStream = new FileInputStream(configPath + keystoreFilename)) {
KeyStore serverKeys = KeyStore.getInstance(keystoreType);
serverKeys.load(keystoreInputStream, keystorePassword.toCharArray());
serverKeyManager = KeyManagerFactory.getInstance("SunX509");
serverKeyManager.init(serverKeys, keystorePassword.toCharArray());
}
// Create New Trust Store
try (FileInputStream keystoreInputStream = new FileInputStream(configPath + keystoreFilename)) {
KeyStore serverTrustStore = KeyStore.getInstance(keystoreType);
serverTrustStore.load(keystoreInputStream, keystorePassword.toCharArray());
trustManager = TrustManagerFactory.getInstance("SunX509");
trustManager.init(serverTrustStore);
}
}
// Set SSL Trust and Key stores in the config
connection.getConfig().setKeyManagers(serverKeyManager.getKeyManagers());
connection.getConfig().setTrustManagers(trustManager.getTrustManagers());
connection.bind();
EntryCursor cursor = connection.search(queryBase, queryFilter, SearchScope.SUBTREE);
while (cursor.next()) {
returnList.add(cursor.get().getAttributes());
for (Attribute attribute : cursor.get().getAttributes()) {
LOG.debug(attribute.getId() + ":" + attribute.getString());
}
}
connection.unBind();
} catch (Exception e) {
LOG.error(uuid + " Error - " + e.getMessage());
} finally {
if (connection != null) {
connection.close();
}
}
return returnList;
}
Aggregations