use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.
the class LdifDataUtility method deleteEntryWithAllSubs.
/**
* Remove base entry with all sub entries
*
* @param connection
* Connection to LDAP server
* @param baseDN
* Base DN entry
* @return The result code for the processing that was performed.
*/
public ResultCode deleteEntryWithAllSubs(LDAPConnection connection, String baseDN) {
ResultCode resultCode = ResultCode.SUCCESS;
SearchResult searchResult = null;
try {
searchResult = connection.search(baseDN, SearchScope.SUB, "objectClass=*");
if ((searchResult == null) || (searchResult.getEntryCount() == 0)) {
return ResultCode.LOCAL_ERROR;
}
} catch (LDAPSearchException le) {
LOG.error("Failed to search subordinate entries", le);
return ResultCode.LOCAL_ERROR;
}
LinkedList<String> dns = new LinkedList<String>();
for (SearchResultEntry entry : searchResult.getSearchEntries()) {
dns.add(entry.getDN());
}
ListIterator<String> listIterator = dns.listIterator(dns.size());
while (listIterator.hasPrevious()) {
try {
connection.delete(listIterator.previous());
} catch (LDAPException le) {
LOG.error("Failed to delete entry", le);
resultCode = ResultCode.LOCAL_ERROR;
break;
}
}
return resultCode;
}
use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.
the class LdifDataUtility method getAttributeResultEntryLDIF.
public List<SearchResultEntry> getAttributeResultEntryLDIF(LDAPConnection connection, List<String> patterns, String baseDN) {
List<SearchResultEntry> searchResultEntryList = new ArrayList<SearchResultEntry>();
try {
for (String pattern : patterns) {
String[] targetArray = new String[] { pattern };
Filter inumFilter = Filter.createSubstringFilter("inum", null, targetArray, null);
Filter searchFilter = Filter.createORFilter(inumFilter);
SearchResultEntry sr = connection.searchForEntry(baseDN, SearchScope.SUB, searchFilter, null);
searchResultEntryList.add(sr);
}
return searchResultEntryList;
} catch (LDAPException le) {
if (le.getResultCode() != ResultCode.NO_SUCH_OBJECT) {
LOG.error("Failed to search ldif record", le);
return null;
}
}
return null;
}
use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.
the class LdapConnectionProvider method createConnectionPoolWithWaitImpl.
private LDAPConnectionPool createConnectionPoolWithWaitImpl(Properties props, FailoverServerSet failoverSet, BindRequest bindRequest, LDAPConnectionOptions connectionOptions, int maxConnections, SSLUtil sslUtil) throws LDAPException {
String connectionPoolMaxWaitTime = props.getProperty("connection-pool-max-wait-time");
int connectionPoolMaxWaitTimeSeconds = 30;
if (StringHelper.isNotEmpty(connectionPoolMaxWaitTime)) {
connectionPoolMaxWaitTimeSeconds = Integer.parseInt(connectionPoolMaxWaitTime);
}
LOG.debug("Using LDAP connection pool timeout: '" + connectionPoolMaxWaitTimeSeconds + "'");
LDAPConnectionPool createdConnectionPool = null;
LDAPException lastException = null;
int attempt = 0;
long currentTime = System.currentTimeMillis();
long maxWaitTime = currentTime + connectionPoolMaxWaitTimeSeconds * 1000;
do {
attempt++;
if (attempt > 0) {
LOG.info("Attempting to create connection pool: " + attempt);
}
try {
createdConnectionPool = createConnectionPoolImpl(failoverSet, bindRequest, connectionOptions, maxConnections, sslUtil);
break;
} catch (LDAPException ex) {
if (ex.getResultCode().intValue() != ResultCode.CONNECT_ERROR_INT_VALUE) {
throw ex;
}
lastException = ex;
}
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
LOG.error("Exception happened in sleep", ex);
return null;
}
currentTime = System.currentTimeMillis();
} while (maxWaitTime > currentTime);
if ((createdConnectionPool == null) && (lastException != null)) {
throw lastException;
}
return createdConnectionPool;
}
use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.
the class LdapOperationsServiceImpl method search.
/*
* (non-Javadoc)
*
* @see org.gluu.site.ldap.PlatformOperationFacade#search(java.lang.String,
* com.unboundid.ldap.sdk.Filter, org.xdi.ldap.model.SearchScope,
* org.gluu.site.ldap.persistence.BatchOperation, int, int, int,
* com.unboundid.ldap.sdk.Control[], java.lang.String)
*/
@Override
public <T> SearchResult search(String dn, Filter filter, SearchScope scope, LdapBatchOperationWraper<T> batchOperationWraper, int startIndex, int searchLimit, int sizeLimit, Control[] controls, String... attributes) throws SearchException {
SearchRequest searchRequest;
BatchOperation<T> ldapBatchOperation = null;
if (batchOperationWraper != null) {
ldapBatchOperation = (BatchOperation<T>) batchOperationWraper.getBatchOperation();
}
if (LOG.isTraceEnabled()) {
// Find whole tree search
if (StringHelper.equalsIgnoreCase(dn, "o=gluu")) {
LOG.trace("Search in whole LDAP tree", new Exception());
}
}
if (attributes == null) {
searchRequest = new SearchRequest(dn, scope, filter);
} else {
searchRequest = new SearchRequest(dn, scope, filter, attributes);
}
boolean useSizeLimit = sizeLimit > 0;
if (useSizeLimit) {
// Use paged result to limit search
searchLimit = sizeLimit;
}
SearchResult searchResult = null;
List<SearchResult> searchResultList = new ArrayList<SearchResult>();
List<SearchResultEntry> searchResultEntries = new ArrayList<SearchResultEntry>();
List<SearchResultReference> searchResultReferences = new ArrayList<SearchResultReference>();
if ((searchLimit > 0) || (startIndex > 0)) {
if (searchLimit == 0) {
// Default page size
searchLimit = 100;
}
boolean collectSearchResult;
LDAPConnection ldapConnection = null;
try {
ldapConnection = getConnectionPool().getConnection();
ASN1OctetString cookie = null;
if (startIndex > 0) {
try {
cookie = scrollSimplePagedResultsControl(ldapConnection, dn, filter, scope, controls, startIndex);
} catch (InvalidSimplePageControlException ex) {
throw new LDAPSearchException(ex.getResultCode(), "Failed to scroll to specified startIndex", ex);
} catch (LDAPException ex) {
throw new LDAPSearchException(ex.getResultCode(), "Failed to scroll to specified startIndex", ex);
}
}
do {
collectSearchResult = true;
searchRequest.setControls(new Control[] { new SimplePagedResultsControl(searchLimit, cookie) });
setControls(searchRequest, controls);
searchResult = ldapConnection.search(searchRequest);
if (ldapBatchOperation != null) {
collectSearchResult = ldapBatchOperation.collectSearchResult(searchResult.getEntryCount());
}
if (collectSearchResult) {
searchResultList.add(searchResult);
searchResultEntries.addAll(searchResult.getSearchEntries());
searchResultReferences.addAll(searchResult.getSearchReferences());
}
if (ldapBatchOperation != null) {
List<T> entries = batchOperationWraper.createEntities(searchResult);
ldapBatchOperation.performAction(entries);
}
cookie = null;
try {
SimplePagedResultsControl c = SimplePagedResultsControl.get(searchResult);
if (c != null) {
cookie = c.getCookie();
}
} catch (LDAPException ex) {
LOG.error("Error while accessing cookies" + ex.getMessage());
}
if (useSizeLimit) {
break;
}
} while ((cookie != null) && (cookie.getValueLength() > 0));
} catch (LDAPException ex) {
throw new SearchException("Failed to scroll to specified startIndex", ex, ex.getResultCode().intValue());
} finally {
if (ldapConnection != null) {
getConnectionPool().releaseConnection(ldapConnection);
}
}
if (!collectSearchResult) {
return new SearchResult(searchResult.getMessageID(), searchResult.getResultCode(), searchResult.getDiagnosticMessage(), searchResult.getMatchedDN(), searchResult.getReferralURLs(), searchResultEntries, searchResultReferences, searchResultEntries.size(), searchResultReferences.size(), searchResult.getResponseControls());
}
if (!searchResultList.isEmpty()) {
SearchResult searchResultTemp = searchResultList.get(0);
return new SearchResult(searchResultTemp.getMessageID(), searchResultTemp.getResultCode(), searchResultTemp.getDiagnosticMessage(), searchResultTemp.getMatchedDN(), searchResultTemp.getReferralURLs(), searchResultEntries, searchResultReferences, searchResultEntries.size(), searchResultReferences.size(), searchResultTemp.getResponseControls());
}
} else {
setControls(searchRequest, controls);
try {
searchResult = getConnectionPool().search(searchRequest);
} catch (LDAPSearchException ex) {
throw new SearchException(ex.getMessage(), ex, ex.getResultCode().intValue());
}
}
return searchResult;
}
use of com.unboundid.ldap.sdk.LDAPException in project gitblit by gitblit.
the class LdapConnection method bind.
/**
* Bind using the given credentials, by filling in the username in the given {@code bindPattern} to
* create the DN.
* @return A bind result, or null if binding failed.
*/
public BindResult bind(String bindPattern, String simpleUsername, String password) {
BindResult result = null;
try {
String bindUser = StringUtils.replace(bindPattern, "${username}", escapeLDAPSearchFilter(simpleUsername));
SimpleBindRequest request = new SimpleBindRequest(bindUser, password);
result = conn.bind(request);
userBindRequest = request;
currentBindRequest = userBindRequest;
} catch (LDAPException e) {
logger.error("Error authenticating to LDAP with user account to search the directory.");
logger.error(" Please check your settings for realm.ldap.bindpattern.");
logger.debug(" Received exception when binding to LDAP", e);
return null;
}
return result;
}
Aggregations