Announcement Announcement Module
Collapse
No announcement yet.
search using objectGUID Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • search using objectGUID

    Can anyone tell me how I can search the ldap using an objectGUID?

    I'm using the ldapTemplate.search method using an EqualsFilter object and the escaped binary representation of the objectGUID, but don't succeed in getting any results...

    Code:
        private List listPersons(LdapTemplate ldapTemplate, String company, String objectGUID) {
            AndFilter filter = new AndFilter();
            filter.and(new EqualsFilter("objectclass", "person"));
            filter.and(new EqualsFilter("company", company));
            filter.and(new EqualsFilter("objectGUID", encodeObjectGUID(objectGUID)));
            
            return searchLdap(ldapTemplate, filter);
        }
    
        private List searchLdap(LdapTemplate ldapTemplate, Filter filter) {
            return ldapTemplate.search(
                    "", filter.encode(),
                    new AttributesMapper() {
                        public Object mapFromAttributes(Attributes attrs) throws NamingException {
                            return attrs.get("cn").get() 
                                    + "|" + attrs.get("company")
                                    + "|" + attrs.get("department")
                                    + "|" + attrs.get("title")
                                    + "|" + decodeObjectGUID((byte[]) attrs.get("objectGUID").get());
                        }
                    });
        }
        
        private String decodeObjectGUID(byte[] objectGUID) {
            StringBuffer sb = new StringBuffer("{");
            sb.append(addLeadingZero((int)objectGUID[3] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[2] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[1] & 0xFF)); 
            sb.append(addLeadingZero((int)objectGUID[0] & 0xFF));
            sb.append("-");
            sb.append(addLeadingZero((int)objectGUID[5] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[4] & 0xFF));
            sb.append("-");
            sb.append(addLeadingZero((int)objectGUID[7] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[6] & 0xFF));
            sb.append("-");
            sb.append(addLeadingZero((int)objectGUID[8] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[9] & 0xFF));
            sb.append("-");
            sb.append(addLeadingZero((int)objectGUID[10] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[11] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[12] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[13] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[14] & 0xFF));
            sb.append(addLeadingZero((int)objectGUID[15] & 0xFF));
            sb.append("}");
            
            return sb.toString ();
        }
        
        private String addLeadingZero(int k) {
            return (k <= 0xF)? "0" + Integer.toHexString(k) : Integer.toHexString(k);
        }
        
        private String encodeObjectGUID(String objectGUID) {
            StringBuffer sb = new StringBuffer();
            StringTokenizer st = new StringTokenizer(objectGUID, "-");
            String[] s = new String[st.countTokens()];
            for (int i = 0; st.hasMoreTokens(); i++){
                s[i] = st.nextToken();
            }
            
            sb.append("\\" + s[0].substring(6,8));
            sb.append("\\" + s[0].substring(4,6));
            sb.append("\\" + s[0].substring(2,4));
            sb.append("\\" + s[0].substring(0,2));
            
            sb.append("\\" + s[1].substring(2,4));
            sb.append("\\" + s[1].substring(0,2));
            
            sb.append("\\" + s[2].substring(2,4));
            sb.append("\\" + s[2].substring(0,2));
            
            sb.append("\\" + s[3].substring(0,2));
            sb.append("\\" + s[3].substring(2,4));
            
            sb.append("\\" + s[4].substring(0,2));
            sb.append("\\" + s[4].substring(2,4));
            sb.append("\\" + s[4].substring(4,6));
            sb.append("\\" + s[4].substring(6,8));
            sb.append("\\" + s[4].substring(8,10));
            sb.append("\\" + s[4].substring(10,12));
            
            return sb.toString();
        }

  • #2
    I got a bit further: using an objectGUID of "9ddad257-e190-4b7e-a7b9-8306ffc25ac4", which gets encoded to "\\57\\d2\\da\\9d\\90\\e1\\7e\\4b\\a7\\b9\\83\\06\ \ff\\c2\\5a\\c4" the filter.encode() encodes my query string to "(&(objectclass=person)(company=MME)(objectGUID=\5 c57\5cd2\5cda\5c9d\5c90\5ce1\5c7e\5c4b\5ca7\5cb9\5 c83\5c06\5cff\5cc2\5c5a\5cc4))".

    This does not give me any results. If, however, I peform the search with a string "(&(objectclass=person)(company=MME)(objectGUID=\\ 57\\d2\\da\\9d\\90\\e1\\7e\\4b\\a7\\b9\\83\\06\\ff \\c2\\5a\\c4))" I get the wanted result!!

    I see that the CompareFilter class uses a LdapEncoder class to encode the "\\" value to a "\5c" value. But this is not what I need...

    anyone??

    Comment


    • #3
      Making your guid interpretation into a Filter subclass would make it easier to work with. That might even be something we would be interested in including in Spring LDAP if you'd happen to invent it ...

      Comment

      Working...
      X