code: public String generateQuery(String filter,
              Collection<String> attributes, String attributeValue) {
  
          // Build LDAP query for objects having at least one attribute and with
          // the given search filter
          StringBuilder ldapQuery = new StringBuilder();
          ldapQuery.append("(&");
          ldapQuery.append(filter);
  
          // Include all attributes within OR clause if there are more than one
          if (attributes.size() > 1)
              ldapQuery.append("(|");
  
          // Add equality comparison for each possible attribute
          for (String attribute : attributes) {
              ldapQuery.append("(");
              ldapQuery.append(escapingService.escapeLDAPSearchFilter(attribute));
  
              if (attributeValue != null) {
                  ldapQuery.append("=");
                  ldapQuery.append(escapingService.escapeLDAPSearchFilter(attributeValue));
                  ldapQuery.append(")");
              }
              else
                  ldapQuery.append("=*)");
  
          }
  
          // Close OR clause, if any
          if (attributes.size() > 1)
              ldapQuery.append(")");
  
          // Close overall query (AND clause)
          ldapQuery.append(")");
  
          return ldapQuery.toString();
  
      }
docs: Generates a properly-escaped LDAP query which finds all objects which
  match the given LDAP filter and which have at least one of the given
  attributes set to the specified value.
  
  @param filter
  The LDAP filter to apply to reduce the results of the query in
  addition to testing the values of the given attributes.
  
  @param attributes
  A collection of all attributes to test for equivalence to the given
  value, in order of decreasing priority.
  
  @param attributeValue
  The value that the resulting LDAP query should search for within the
  attributes of objects within the LDAP directory. If null, the
  resulting LDAP query will search for the presence of at least one of
  the given attributes on each object, regardless of the value of
  those attributes.
  
  @return
  An LDAP query which will search for arbitrary LDAP objects having at
  least one of the given attributes set to the specified value.