From 259b6a003e431580c2c7cebf1c192bdef8aae21a Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Mon, 23 Jun 2014 14:26:28 +0200 Subject: Combine various Lookup.search methods into a general search(Path, Property...) --- .../java/com/c2kernel/lookup/ldap/LDAPLookup.java | 71 +++++++++------------- 1 file changed, 29 insertions(+), 42 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java index ac36a10..faf8dab 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java @@ -15,8 +15,6 @@ import org.omg.CORBA.Object; import com.c2kernel.common.ObjectAlreadyExistsException; import com.c2kernel.common.ObjectCannotBeUpdated; import com.c2kernel.common.ObjectNotFoundException; -import com.c2kernel.entity.TraceableEntity; -import com.c2kernel.entity.agent.ActiveEntity; import com.c2kernel.entity.proxy.ProxyMessage; import com.c2kernel.lookup.AgentPath; import com.c2kernel.lookup.DomainPath; @@ -27,6 +25,7 @@ import com.c2kernel.lookup.Path; import com.c2kernel.lookup.RolePath; import com.c2kernel.process.Gateway; import com.c2kernel.process.auth.Authenticator; +import com.c2kernel.property.Property; import com.c2kernel.property.PropertyDescription; import com.c2kernel.property.PropertyDescriptionList; import com.c2kernel.utils.Logger; @@ -282,36 +281,39 @@ public class LDAPLookup implements Lookup } @Override - public LDAPPathSet search(Path start, String propname, String propvalue) + public LDAPPathSet search(Path start, Property... props) { - String val = propname+":"+propvalue; - Logger.msg(8,"LDAPLookup::search() From " + getDN(start) + " for cristalprop=" + val ); + StringBuffer filter = new StringBuffer(); + int propCount = 0; + for (Property prop: props) { + filter.append("(cristalprop="+LDAPLookupUtils.escapeSearchFilter(prop.getName()+ + ":"+prop.getValue())+")"); + propCount++; + } + + String filterParam; + if (propCount == 0) + filterParam = ""; + else if (propCount == 1) + filterParam = filter.toString(); + else + filterParam = "(&"+filter.toString()+")"; + LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); searchCons.setBatchSize(0); searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING); - return search(getFullDN(start),LDAPConnection.SCOPE_SUB,"cristalprop="+LDAPLookupUtils.escapeSearchFilter(val),searchCons); + return search(getFullDN(start),LDAPConnection.SCOPE_SUB,filterParam,searchCons); } @Override public LDAPPathSet search(Path start, PropertyDescriptionList props) { - StringBuffer query = new StringBuffer(); - int propCount = 0; + + ArrayList params = new ArrayList(); for (PropertyDescription propDesc: props.list) { - if (propDesc.getIsClassIdentifier()) { - String thisProp = LDAPLookupUtils.escapeSearchFilter(propDesc.getName()+(":")+propDesc.getDefaultValue()); - query.append("(cristalprop=").append(thisProp).append(")"); - propCount++; - } + if (propDesc.getIsClassIdentifier()) + params.add(propDesc.getProperty()); } - LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); - searchCons.setBatchSize(0); - searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING); - if (propCount == 0) - return search(getFullDN(start),LDAPConnection.SCOPE_SUB,"objectClass=cristalentity",searchCons); - else if (propCount == 1) - return search(getFullDN(start),LDAPConnection.SCOPE_SUB,query.toString(),searchCons); - else - return search(getFullDN(start),LDAPConnection.SCOPE_SUB,"(&"+query.toString()+")",searchCons); + return search(start, params.toArray(new Property[params.size()])); } protected LDAPPathSet search(String startDN, int scope, String filter, LDAPSearchConstraints searchCons) @@ -339,22 +341,6 @@ public class LDAPLookup implements Lookup searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER); return search(startDN,LDAPConnection.SCOPE_SUB,filter,searchCons); } - - @Override - public LDAPPathSet searchEntities(Path start) { - LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); - searchCons.setBatchSize(0); - searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING); - return search(getFullDN(start), LDAPConnection.SCOPE_SUB, "objectClass=cristalentity", searchCons); - } - - @Override - public LDAPPathSet searchAliases(DomainPath start) { - LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); - searchCons.setBatchSize(0); - searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER); - return search(getFullDN(start), LDAPConnection.SCOPE_SUB, "objectClass=aliasObject", searchCons); - } @Override public LDAPPathSet searchAliases(ItemPath entity) { @@ -371,15 +357,16 @@ public class LDAPLookup implements Lookup } @Override - public Class getItemClass(Path path) throws ObjectNotFoundException { + public ItemPath getItemPath(int sysKey) throws ObjectNotFoundException, InvalidItemPathException { String[] attr = { LDAPConnection.ALL_USER_ATTRS }; try { - LDAPEntry anEntry=mLDAPAuth.getAuthObject().read(getDN(path)+mLocalPath,attr); + ItemPath item = new ItemPath(sysKey); + LDAPEntry anEntry=mLDAPAuth.getAuthObject().read(getDN(item)+mLocalPath,attr); String type = LDAPLookupUtils.getFirstAttributeValue(anEntry, "objectClass"); if (type.equals("cristalentity")) - return TraceableEntity.class; + return item; else if (type.equals("cristalagent")) - return ActiveEntity.class; + return new AgentPath(sysKey); else throw new ObjectNotFoundException("Not an entity", ""); -- cgit v1.2.3