Skip to content

Commit

Permalink
Add improvement to Search to allow Empty Result
Browse files Browse the repository at this point in the history
  • Loading branch information
arunans23 committed Nov 13, 2023
1 parent b764503 commit 04d350b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<groupId>org.wso2.carbon.connector</groupId>
<artifactId>org.wso2.carbon.connector.ldap</artifactId>
<packaging>jar</packaging>
<version>1.0.12</version>
<version>1.0.13</version>
<name>WSO2 Carbon - Mediation Library Connector For LDAP</name>
<url>http://wso2.org</url>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class LDAPConstants {
public static final String TIMEOUT = "timeout";
public static final String OLD_NAME = "oldName";
public static final String NEW_NAME = "newName";
public static final String ALLOW_EMPTY_SEARCH_RESULT = "allowEmptySearchResult";

public static final class ErrorConstants {
public static final int SEARCH_ERROR = 7000001;
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ public void connect(MessageContext messageContext) {
}
}

boolean onlyOneReference = Boolean.valueOf(
boolean onlyOneReference = Boolean.parseBoolean(
(String) getParameter(messageContext, LDAPConstants.ONLY_ONE_REFERENCE));
boolean allowEmptySearchResult = Boolean.parseBoolean(
(String) getParameter(messageContext, LDAPConstants.ALLOW_EMPTY_SEARCH_RESULT));
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE,
LDAPConstants.NAMESPACE);
Expand Down Expand Up @@ -105,17 +107,13 @@ public void connect(MessageContext messageContext) {
result.addChild(prepareNode(entityResult, factory, ns, returnAttributes));
}
} else {

throw new NamingException("No matching result or entity found for this search");
if (!allowEmptySearchResult) {
throw new NamingException("No matching result or entity found for this search");
}
}
} else {
entityResult = makeSureOnlyOneMatch(results);
if (entityResult == null) {
throw new NamingException("Multiple objects for the searched target have been found. Try to " +
"change onlyOneReference option");
} else {
result.addChild(prepareNode(entityResult, factory, ns, returnAttributes));
}
entityResult = makeSureOnlyOneMatch(results, allowEmptySearchResult);
result.addChild(prepareNode(entityResult, factory, ns, returnAttributes));
}
LDAPUtils.preparePayload(messageContext, result);
if (context != null) {
Expand Down Expand Up @@ -191,7 +189,8 @@ private OMElement prepareNode(SearchResult entityResult, OMFactory factory, OMNa
return entry;
}

private SearchResult makeSureOnlyOneMatch(NamingEnumeration<SearchResult> results) throws NamingException {
private SearchResult makeSureOnlyOneMatch(NamingEnumeration<SearchResult> results,
boolean allowEmptySearchResult) throws NamingException {
SearchResult searchResult = null;

if (results.hasMoreElements()) {
Expand All @@ -200,11 +199,15 @@ private SearchResult makeSureOnlyOneMatch(NamingEnumeration<SearchResult> result
// Make sure there is not another item available, there should be only 1 match
if (results.hasMoreElements()) {
// Here the code has matched multiple objects for the searched target
return null;
throw new NamingException("Multiple objects for the searched target have been found. Try to " +
"change onlyOneReference option");
}
return searchResult;
} else {
throw new NamingException("Could not find a matching entry for this search");
if (!allowEmptySearchResult) {
throw new NamingException("Could not find a matching entry for this search");
}
return null;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/ldap_entry/searchEntry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<parameter name="attributes"
description="The attributes of the LDAP entry that should be included in the search
result."/>
<parameter name="allowEmptySearchResult"
description="Boolean value to allow empty Search Result or throw Exception"/>
<sequence>
<property expression="$func:onlyOneReference" name="onlyOneReference" scope="default"
type="STRING"/>
Expand All @@ -37,6 +39,8 @@
<property expression="$func:dn" name="dn" scope="default" type="STRING"/>
<property expression="$func:attributes" name="attributes" scope="default" type="STRING"/>
<property expression="$func:limit" name="limit" scope="default" type="STRING"/>
<property expression="$func:allowEmptySearchResult" name="allowEmptySearchResult" scope="default"
type="STRING"/>

<class name="org.wso2.carbon.connector.ldap.SearchEntry"/>
</sequence>
Expand Down

0 comments on commit 04d350b

Please sign in to comment.