예제 #1
0
  /**
   * Creates a new ldap attribute. The collection of values is inspected for either String or byte[]
   * and the appropriate attribute is created.
   *
   * @param sb sort behavior
   * @param name of this attribute
   * @param values of this attribute
   * @return ldap attribute
   * @throws IllegalArgumentException if values contains something other than String or byte[]
   */
  public static LdapAttribute createLdapAttribute(
      final SortBehavior sb, final String name, final Collection<Object> values) {
    final Collection<String> stringValues = new ArrayList<String>();
    final Collection<byte[]> binaryValues = new ArrayList<byte[]>();
    for (Object value : values) {
      if (value instanceof byte[]) {
        binaryValues.add((byte[]) value);
      } else if (value instanceof String) {
        stringValues.add((String) value);
      } else {
        throw new IllegalArgumentException("Values must contain either String or byte[]");
      }
    }

    LdapAttribute la;
    if (!binaryValues.isEmpty()) {
      la = new LdapAttribute(sb, true);
      la.setName(name);
      la.addBinaryValues(binaryValues);
    } else {
      la = new LdapAttribute(sb, false);
      la.setName(name);
      la.addStringValues(stringValues);
    }
    return la;
  }
 /**
  * Handle a single attribute.
  *
  * @param conn the search was performed on
  * @param request used to find the search entry
  * @param attr to handle
  * @throws LdapException if the LDAP returns an error
  */
 protected void handleAttribute(
     final Connection conn, final SearchRequest request, final LdapAttribute attr)
     throws LdapException {
   if (attr != null) {
     attr.setName(handleAttributeName(conn, request, attr.getName()));
     if (attr.isBinary()) {
       final Set<byte[]> newValues = new HashSet<byte[]>(attr.size());
       for (byte[] b : attr.getBinaryValues()) {
         newValues.add(handleAttributeValue(conn, request, b));
       }
       attr.clear();
       attr.addBinaryValues(newValues);
     } else {
       final Set<String> newValues = new HashSet<String>(attr.size());
       for (String s : attr.getStringValues()) {
         newValues.add(handleAttributeValue(conn, request, s));
       }
       attr.clear();
       attr.addStringValues(newValues);
     }
   }
 }
  /** {@inheritDoc} */
  @Override
  protected void processAttributes(
      final Connection conn, final SearchRequest request, final LdapEntry entry)
      throws LdapException {
    final Map<LdapAttribute, Matcher> matchingAttrs = new HashMap<LdapAttribute, Matcher>();
    for (LdapAttribute la : entry.getAttributes()) {
      // Match attribute ID against the pattern
      final Matcher matcher = RANGE_PATTERN.matcher(la.getName());

      // If the attribute ID matches the pattern
      if (matcher.find()) {
        matchingAttrs.put(la, matcher);
      }
    }

    for (Map.Entry<LdapAttribute, Matcher> mEntry : matchingAttrs.entrySet()) {
      final LdapAttribute la = mEntry.getKey();
      final Matcher matcher = mEntry.getValue();
      final String msg = String.format("attribute '%s' entry '%s'", la.getName(), entry.getDn());

      // Determine the attribute name without the range syntax
      final String attrTypeName = matcher.group(1);
      logger.debug("Found Range option {}", msg);
      if (attrTypeName == null || attrTypeName.isEmpty()) {
        logger.error("Unable to determine the attribute type name for {}", msg);
        throw new IllegalArgumentException(
            "Unable to determine the attribute type name for " + msg);
      }

      // Create or update the attribute whose ID has the range syntax removed
      LdapAttribute newAttr = entry.getAttribute(attrTypeName);
      if (newAttr == null) {
        newAttr = new LdapAttribute(la.getSortBehavior(), la.isBinary());
        newAttr.setName(attrTypeName);
        entry.addAttribute(newAttr);
      }

      // Copy values
      if (la.isBinary()) {
        newAttr.addBinaryValues(la.getBinaryValues());
      } else {
        newAttr.addStringValues(la.getStringValues());
      }

      // Remove original attribute with range syntax from returned attributes
      entry.removeAttribute(la);

      // If the attribute ID ends with * we're done, otherwise increment
      if (!la.getName().endsWith(END_OF_RANGE)) {

        // Determine next attribute ID
        // CheckStyle:MagicNumber OFF
        final int start = Integer.parseInt(matcher.group(2));
        final int end = Integer.parseInt(matcher.group(3));
        // CheckStyle:MagicNumber ON
        final int diff = end - start;
        final String nextAttrID =
            String.format(RANGE_FORMAT, attrTypeName, end + 1, end + diff + 1);

        // Search for next increment of values
        logger.debug("Searching for '{}' to increment {}", nextAttrID, msg);

        final SearchOperation search = new SearchOperation(conn);
        final SearchRequest sr =
            SearchRequest.newObjectScopeSearchRequest(entry.getDn(), new String[] {nextAttrID});
        final SearchResult result = search.execute(sr).getResult();

        // Add all attributes to the search result
        entry.addAttributes(result.getEntry().getAttributes());

        // Iterate
        processAttributes(conn, request, entry);
      }
    }
  }