Express (Distributed operating systems), v8.0 > Secure applications and their environment > Authenticate users > Select a registry or repository > Manage realms in a federated repository > Virtual member manager > Develop with virtual member manager > Integrate virtual member manager into the application > Sample code
Sample code for searching using search bases
Use the end-to-end sample code and data graphs to search by using the different search bases.
The following steps are covered in this sample code snippet:
- Create two users in one search base and another two users in another search base.
- Set different search bases.
- Search each of the search bases and validate that the entities returned belong to the respective search bases.
Prerequisites
Ensure that we have read the information and completed the steps described in the topic, Program prerequisites.
Sample code
Add the following end-to-end sample code to the application code and replace the variables with the actual values to use.
/** * testSearchBases This test sets different search bases * The results from the two searches returns entities * that are present in a particular search base only */ public static void testSearchBases() throws Exception { // Create two users with uid SalesPerson1 and SalesPerson2 respectively // in search base o=SalesPerson,dc=yourco,dc=com DataObject user1 = addPersonAccount("SalesPerson1", "SalesPerson1cn","SalesPerson1sn", "o=SalesPerson,dc=yourco,dc=com"); DataObject user2 = addPersonAccount("SalesPerson2", "SalesPerson2cn","SalesPerson2sn", "o=SalesPerson,dc=yourco,dc=com"); // Create two users with uid SalesManager1 and SalesManager2 respectively // in search base o=SalesManager,dc=yourco,dc=com DataObject user3 = addPersonAccount("SalesManager1", "SalesManager1cn","SalesManager1sn", "o=SalesManager,dc=yourco,dc=com"); DataObject user4 = addPersonAccount("SalesManager2", "SalesManager2cn","SalesManager2sn", "o=SalesManager,dc=yourco,dc=com"); // Now do a search in the two search bases. The output should have users belonging to that search base String searchBase1 = "o=SalesPerson,dc=yourco,dc=com"; String searchBase2 = "o=SalesManager,dc=yourco,dc=com"; // Search with the searchBase1 set search(searchBase1); // Search with the searchBase2 set search(searchBase2); } /** * addPersonAccount * Adds an entity of PersonAccount entity type * @param uid value to be set * @param cn value to be set * @param sn value to be set * @param parent value to be set * @return DataObject * @throws Exception */ public static DataObject addPersonAccount(String uid, String cn, String sn, String parent) throws Exception { DataObject root = SDOHelper.createRootDataObject(); DataObject entity = SDOHelper.createEntityDataObject(root, null, SchemaConstants.DO_PERSON_ACCOUNT); entity.set("uid", uid); entity.set("cn", cn); entity.set("sn", sn); entity.createDataObject(SchemaConstants.DO_PARENT).createDataObject(SchemaConstants.DO_IDENTIFIER) .setString(SchemaConstants.PROP_UNIQUE_NAME, parent); root = service.create(root); return root; } /** * Gets the search results for the search base set * @param searchBase the search base to be set on search control * @throws Exception */ public static void search(String searchBase) throws Exception { DataObject root = SDOHelper.createRootDataObject(); DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, SchemaConstants.DO_SEARCH_CONTROL); searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn"); searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn"); searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("telephoneNumber"); searchCtrl.getList(SchemaConstants.PROP_SEARCH_BASES).add(searchBase); searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount' and uid='*'"); System.out.println("Input datagraph before searching with search base "+ searchBase +" " + printDO(root)); DataObject searchRoot = service.search(root); System.out.println("Output datagraph after searching with search base "+ searchBase +" " + printDO(searchRoot)); }
Input and output data graphs
Input data graph for searching for users in the search base "o=SalesPerson,dc=yourco,dc=com":
<?xml version="1.0" encoding="UTF-8"?> <sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim"> <wim:Root> <wim:controls xsi:type="wim:SearchControl" expression="@xsi:type='PersonAccount' and uid='*'"> <wim:properties>cn </wim:properties> <wim:properties>sn </wim:properties> <wim:properties>telephoneNumber </wim:properties> <wim:searchBases>o=SalesPerson,dc=yourco,dc=com </wim:searchBases> </wim:controls> </wim:Root> </sdo:datagraph>Output data graph after searching for users in the search base "o=SalesPerson,dc=yourco,dc=com":
<?xml version="1.0" encoding="UTF-8"?> <sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim"> <wim:Root> <wim:entities xsi:type="wim:PersonAccount"> <wim:identifier externalName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com" repositoryId="ldapRepo" uniqueId="e0db3534-a0ec-4c8b-bfff-a399895a7ca3" uniqueName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com"/> <wim:cn>SalesPerson1cn </wim:cn> <wim:sn>SalesPerson1sn </wim:sn> <wim:telephoneNumber>11111111 </wim:telephoneNumber> </wim:entities> <wim:entities xsi:type="wim:PersonAccount"> <wim:identifier externalName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com" repositoryId="ldapRepo" uniqueId="bc9d1275-a2dc-4348-ae2c-4eabb54ad98a" uniqueName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com"/> <wim:cn>SalesPerson2cn </wim:cn> <wim:sn>SalesPerson2sn </wim:sn> <wim:telephoneNumber>22222222 </wim:telephoneNumber> </wim:entities> </wim:Root> </sdo:datagraph>Input data graph for searching for users in the search base "o=SalesManager,dc=yourco,dc=com":
<?xml version="1.0" encoding="UTF-8"?> <sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim"> <wim:Root> <wim:controls xsi:type="wim:SearchControl" expression="@xsi:type='PersonAccount' and uid='*'"> <wim:properties>cn </wim:properties> <wim:properties>sn </wim:properties> <wim:properties>telephoneNumber </wim:properties> <wim:searchBases>o=SalesManager,dc=yourco,dc=com </wim:searchBases> </wim:controls> </wim:Root> </sdo:datagraph>Output data graph after searching for users in the search base "o=SalesManager,dc=yourco,dc=com":
<?xml version="1.0" encoding="UTF-8"?> <sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim"> <wim:Root> <wim:entities xsi:type="wim:PersonAccount"> <wim:identifier externalName="uid=SalesManager1,o=SalesManager,dc=yourco,dc=com" repositoryId="ldapRepo" uniqueId="b94cc18f-adbb-41aa-a48f-a0e710cd21ed" uniqueName="uid=SalesManager1,o=SalesManager,dc=yourco,dc=com"/> <wim:cn>SalesManager1cn </wim:cn> <wim:sn>SalesManager1sn </wim:sn> <wim:telephoneNumber>11111111 </wim:telephoneNumber> </wim:entities> <wim:entities xsi:type="wim:PersonAccount"> <wim:identifier externalName="uid=SalesManager2,o=SalesManager,dc=yourco,dc=com" repositoryId="ldapRepo" uniqueId="de158d76-f710-44cc-9e21-747baf2f8944" uniqueName="uid=SalesManager2,o=SalesManager,dc=yourco,dc=com"/> <wim:cn>SalesManager2cn </wim:cn> <wim:sn>SalesManager2sn </wim:sn> <wim:telephoneNumber>22222222 </wim:telephoneNumber> </wim:entities> </wim:Root> </sdo:datagraph>
Parent topic: Sample code