Skip to content

Commit

Permalink
If LDAP not supports SubtreeDeleteRequestControl do remove from code
Browse files Browse the repository at this point in the history
  • Loading branch information
yurem committed Jan 11, 2017
1 parent ab03647 commit 4977abe
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 20 deletions.
50 changes: 40 additions & 10 deletions oxLdap/src/main/java/org/gluu/site/ldap/LDAPConnectionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class LDAPConnectionProvider {

private ArrayList<String> binaryAttributes;

private boolean supportsSubtreeDeleteRequestControl;

@SuppressWarnings("unused")
private LDAPConnectionProvider() {}

Expand Down Expand Up @@ -143,6 +145,7 @@ public void init(Properties props) throws NumberFormatException, LDAPException,

this.supportedLDAPVersion = determineSupportedLdapVersion();
this.subschemaSubentry = determineSubschemaSubentry();
this.supportsSubtreeDeleteRequestControl = supportsSubtreeDeleteRequestControl();
this.creationResultCode = ResultCode.SUCCESS;
}
private LDAPConnectionPool createConnectionPoolWithWaitImpl(Properties props, FailoverServerSet failoverSet, BindRequest bindRequest, LDAPConnectionOptions connectionOptions,
Expand Down Expand Up @@ -243,12 +246,9 @@ private LDAPConnectionPool createSSLConnectionPoolWithPreviousProtocols(SSLUtil

private int determineSupportedLdapVersion() {
int resultSupportedLDAPVersion = LDAPConnectionProvider.DEFAULT_SUPPORTED_LDAP_VERSION;

if (StringHelper.isEmptyString(bindDn) || StringHelper.isEmptyString(bindPassword)) {
return resultSupportedLDAPVersion;
}

if (connectionPool == null) {
boolean validConnection = isValidConnection();
if (!validConnection) {
return resultSupportedLDAPVersion;
}

Expand All @@ -270,12 +270,9 @@ private int determineSupportedLdapVersion() {

private String determineSubschemaSubentry() {
String resultSubschemaSubentry = LDAPConnectionProvider.DEFAULT_SUBSCHEMA_SUBENTRY;

if (StringHelper.isEmptyString(bindDn) || StringHelper.isEmptyString(bindPassword)) {
return resultSubschemaSubentry;
}

if (connectionPool == null) {
boolean validConnection = isValidConnection();
if (!validConnection) {
return resultSubschemaSubentry;
}

Expand All @@ -292,6 +289,35 @@ private String determineSubschemaSubentry() {
return resultSubschemaSubentry;
}

private boolean supportsSubtreeDeleteRequestControl() {
boolean supportsSubtreeDeleteRequestControl = false;

boolean validConnection = isValidConnection();
if (!validConnection) {
return supportsSubtreeDeleteRequestControl;
}

try {
supportsSubtreeDeleteRequestControl = connectionPool.getRootDSE().supportsControl(com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl.SUBTREE_DELETE_REQUEST_OID);
} catch (Exception ex) {
log.error("Failed to determine if LDAP server supports Subtree Delete Request Control", ex);
}

return supportsSubtreeDeleteRequestControl;
}

private boolean isValidConnection() {
if (StringHelper.isEmptyString(bindDn) || StringHelper.isEmptyString(bindPassword)) {
return false;
}

if (connectionPool == null) {
return false;
}

return true;
}

public int getSupportedLDAPVersion() {
return supportedLDAPVersion;
}
Expand All @@ -300,6 +326,10 @@ public String getSubschemaSubentry() {
return subschemaSubentry;
}

public boolean isSupportsSubtreeDeleteRequestControl() {
return supportsSubtreeDeleteRequestControl;
}

/**
* This method is used to get LDAP connection from connectionPool if the
* connection is not available it will return new connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public class LdapEntryManager extends AbstractEntryManager implements Serializab
private static final Logger log = LoggerFactory.getLogger(LdapEntryManager.class);

private static final Class<?>[] GROUP_BY_ALLOWED_DATA_TYPES = { String.class, Date.class, Integer.class, LdapEnum.class };
private static final Class<?>[] SUM_BY_ALLOWED_DATA_TYPES = { int.class, Integer.class, float.class, Float.class, double.class,
Double.class };
private static final Class<?>[] SUM_BY_ALLOWED_DATA_TYPES = { int.class, Integer.class, float.class, Float.class, double.class, Double.class };
private static final String[] NO_STRINGS = new String[0];

private static final Comparator<String> LINE_LENGHT_COMPARATOR = new LineLenghtComparator<String>(false);

private transient OperationsFacade ldapOperationService;
private transient List<DeleteNotifier> subscribers;
Expand Down Expand Up @@ -213,19 +214,46 @@ protected void remove(String dn) {
@Override
public void removeWithSubtree(String dn) {
try {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
}
this.ldapOperationService.deleteWithSubtree(dn);
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
}
if (this.ldapOperationService.getConnectionProvider().isSupportsSubtreeDeleteRequestControl()) {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
}
this.ldapOperationService.deleteWithSubtree(dn);
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
}
} else {
removeSubtreeThroughIteration(dn);
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to remove entry: %s", dn), ex);
}
}

@Override
private void removeSubtreeThroughIteration(String dn) {
SearchResult searchResult = null;
try {
searchResult = this.ldapOperationService.search(dn, Filter.createPresenceFilter("objectClass"), 0, 0, null, "dn");
if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn));
}
} catch (LDAPSearchException ex) {
throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn), ex);
}

List<String> removeEntriesDn = new ArrayList<String>(searchResult.getEntryCount());
for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
removeEntriesDn.add(searchResultEntry.getDN());
}

Collections.sort(removeEntriesDn, LINE_LENGHT_COMPARATOR);

for (String removeEntryDn : removeEntriesDn) {
remove(removeEntryDn);
}
}

@Override
protected List<AttributeData> find(String dn, String ... ldapReturnAttributes) {
// Load entry

Expand Down Expand Up @@ -838,6 +866,34 @@ public int compare(T entry1, T entry2, Getter[] propertyGetters) {
}
}

private static final class LineLenghtComparator<T> implements Comparator<T>, Serializable {

private static final long serialVersionUID = 575848841116711467L;
private boolean ascending;

public LineLenghtComparator(boolean ascending) {
this.ascending = ascending;
}

public int compare(T entry1, T entry2) {
if ((entry1 == null) && (entry2 == null)) {
return 0;
}
if ((entry1 == null) && (entry2 != null)) {
return -1;
} else if ((entry1 != null) && (entry2 == null)) {
return 1;
}

int result = entry1.toString().length() - entry2.toString().length();
if (ascending) {
return result;
} else {
return -result;
}
}
}

public <T> Map<T, List<T>> groupListByProperties(Class<T> entryClass, List<T> entries, boolean caseSensetive, String groupByProperties,
String sumByProperties) {
// Check input parameters
Expand Down

0 comments on commit 4977abe

Please sign in to comment.