From 04d350bc99b4c7677aec7052329074b90841ca1b Mon Sep 17 00:00:00 2001 From: Arunan Sugunakumar Date: Mon, 13 Nov 2023 17:10:05 +0530 Subject: [PATCH] Add improvement to Search to allow Empty Result --- pom.xml | 2 +- .../carbon/connector/ldap/LDAPConstants.java | 1 + .../carbon/connector/ldap/SearchEntry.java | 29 ++++++++++--------- src/main/resources/ldap_entry/searchEntry.xml | 4 +++ 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index 8003cb5..86d3d3c 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ org.wso2.carbon.connector org.wso2.carbon.connector.ldap jar - 1.0.12 + 1.0.13 WSO2 Carbon - Mediation Library Connector For LDAP http://wso2.org diff --git a/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java b/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java index 96ba938..15b05e2 100644 --- a/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java +++ b/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java @@ -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; diff --git a/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java b/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java index 02083db..f680909 100644 --- a/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java +++ b/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java @@ -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); @@ -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) { @@ -191,7 +189,8 @@ private OMElement prepareNode(SearchResult entityResult, OMFactory factory, OMNa return entry; } - private SearchResult makeSureOnlyOneMatch(NamingEnumeration results) throws NamingException { + private SearchResult makeSureOnlyOneMatch(NamingEnumeration results, + boolean allowEmptySearchResult) throws NamingException { SearchResult searchResult = null; if (results.hasMoreElements()) { @@ -200,11 +199,15 @@ private SearchResult makeSureOnlyOneMatch(NamingEnumeration 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; } } diff --git a/src/main/resources/ldap_entry/searchEntry.xml b/src/main/resources/ldap_entry/searchEntry.xml index 271aa4e..f589f86 100644 --- a/src/main/resources/ldap_entry/searchEntry.xml +++ b/src/main/resources/ldap_entry/searchEntry.xml @@ -29,6 +29,8 @@ + @@ -37,6 +39,8 @@ +