mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

matthew_swift
28.47.2010 f2160f4bd1c8ac67e5a86a6710d431e8932877f9
sdk/src/com/sun/opends/sdk/ldap/LDAPUtils.java
File was renamed from sdk/src/org/opends/sdk/ldap/LDAPUtils.java
@@ -25,7 +25,7 @@
 *      Copyright 2009 Sun Microsystems, Inc.
 */
package org.opends.sdk.ldap;
package com.sun.opends.sdk.ldap;
@@ -36,36 +36,33 @@
import java.util.LinkedList;
import java.util.List;
import org.opends.sdk.ByteSequence;
import org.opends.sdk.ByteString;
import org.opends.sdk.Filter;
import org.opends.sdk.FilterVisitor;
import org.opends.sdk.*;
import org.opends.sdk.asn1.ASN1Reader;
import org.opends.sdk.asn1.ASN1Writer;
import org.opends.sdk.responses.SearchResultEntry;
import org.opends.sdk.schema.Schema;
/**
 * Common LDAP utility methods which may be used when implementing new
 * controls and extension.
 * Common LDAP utility methods which may be used when implementing new controls
 * and extension.
 */
public final class LDAPUtils
{
  private static final FilterVisitor<IOException, ASN1Writer> ASN1_ENCODER = new FilterVisitor<IOException, ASN1Writer>()
  private static final FilterVisitor<IOException, ASN1Writer> ASN1_ENCODER =
    new FilterVisitor<IOException, ASN1Writer>()
  {
    public IOException visitAndFilter(ASN1Writer writer,
        List<Filter> subFilters)
    public IOException visitAndFilter(final ASN1Writer writer,
        final List<Filter> subFilters)
    {
      try
      {
        writer.writeStartSequence(TYPE_FILTER_AND);
        for (Filter subFilter : subFilters)
        for (final Filter subFilter : subFilters)
        {
          IOException e = subFilter.accept(this, writer);
          final IOException e = subFilter.accept(this, writer);
          if (e != null)
          {
            return e;
@@ -74,7 +71,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -82,8 +79,8 @@
    public IOException visitApproxMatchFilter(ASN1Writer writer,
        String attributeDescription, ByteString assertionValue)
    public IOException visitApproxMatchFilter(final ASN1Writer writer,
        final String attributeDescription, final ByteString assertionValue)
    {
      try
      {
@@ -93,7 +90,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -101,8 +98,8 @@
    public IOException visitEqualityMatchFilter(ASN1Writer writer,
        String attributeDescription, ByteString assertionValue)
    public IOException visitEqualityMatchFilter(final ASN1Writer writer,
        final String attributeDescription, final ByteString assertionValue)
    {
      try
      {
@@ -112,7 +109,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -120,9 +117,9 @@
    public IOException visitExtensibleMatchFilter(ASN1Writer writer,
        String matchingRule, String attributeDescription,
        ByteString assertionValue, boolean dnAttributes)
    public IOException visitExtensibleMatchFilter(final ASN1Writer writer,
        final String matchingRule, final String attributeDescription,
        final ByteString assertionValue, final boolean dnAttributes)
    {
      try
      {
@@ -135,12 +132,11 @@
        if (attributeDescription != null)
        {
          writer.writeOctetString(TYPE_MATCHING_RULE_TYPE,
              attributeDescription);
          writer
              .writeOctetString(TYPE_MATCHING_RULE_TYPE, attributeDescription);
        }
        writer.writeOctetString(TYPE_MATCHING_RULE_VALUE,
            assertionValue);
        writer.writeOctetString(TYPE_MATCHING_RULE_VALUE, assertionValue);
        if (dnAttributes)
        {
@@ -150,7 +146,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -158,8 +154,8 @@
    public IOException visitGreaterOrEqualFilter(ASN1Writer writer,
        String attributeDescription, ByteString assertionValue)
    public IOException visitGreaterOrEqualFilter(final ASN1Writer writer,
        final String attributeDescription, final ByteString assertionValue)
    {
      try
      {
@@ -169,7 +165,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -177,8 +173,8 @@
    public IOException visitLessOrEqualFilter(ASN1Writer writer,
        String attributeDescription, ByteString assertionValue)
    public IOException visitLessOrEqualFilter(final ASN1Writer writer,
        final String attributeDescription, final ByteString assertionValue)
    {
      try
      {
@@ -188,7 +184,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -196,13 +192,13 @@
    public IOException visitNotFilter(ASN1Writer writer,
        Filter subFilter)
    public IOException visitNotFilter(final ASN1Writer writer,
        final Filter subFilter)
    {
      try
      {
        writer.writeStartSequence(TYPE_FILTER_NOT);
        IOException e = subFilter.accept(this, writer);
        final IOException e = subFilter.accept(this, writer);
        if (e != null)
        {
          return e;
@@ -210,7 +206,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -218,15 +214,15 @@
    public IOException visitOrFilter(ASN1Writer writer,
        List<Filter> subFilters)
    public IOException visitOrFilter(final ASN1Writer writer,
        final List<Filter> subFilters)
    {
      try
      {
        writer.writeStartSequence(TYPE_FILTER_OR);
        for (Filter subFilter : subFilters)
        for (final Filter subFilter : subFilters)
        {
          IOException e = subFilter.accept(this, writer);
          final IOException e = subFilter.accept(this, writer);
          if (e != null)
          {
            return e;
@@ -235,7 +231,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -243,16 +239,15 @@
    public IOException visitPresentFilter(ASN1Writer writer,
        String attributeDescription)
    public IOException visitPresentFilter(final ASN1Writer writer,
        final String attributeDescription)
    {
      try
      {
        writer.writeOctetString(TYPE_FILTER_PRESENCE,
            attributeDescription);
        writer.writeOctetString(TYPE_FILTER_PRESENCE, attributeDescription);
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -260,9 +255,9 @@
    public IOException visitSubstringsFilter(ASN1Writer writer,
        String attributeDescription, ByteString initialSubstring,
        List<ByteString> anySubstrings, ByteString finalSubstring)
    public IOException visitSubstringsFilter(final ASN1Writer writer,
        final String attributeDescription, final ByteString initialSubstring,
        final List<ByteString> anySubstrings, final ByteString finalSubstring)
    {
      try
      {
@@ -275,7 +270,7 @@
          writer.writeOctetString(TYPE_SUBINITIAL, initialSubstring);
        }
        for (ByteSequence anySubstring : anySubstrings)
        for (final ByteSequence anySubstring : anySubstrings)
        {
          writer.writeOctetString(TYPE_SUBANY, anySubstring);
        }
@@ -289,7 +284,7 @@
        writer.writeEndSequence();
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -297,15 +292,15 @@
    public IOException visitUnrecognizedFilter(ASN1Writer writer,
        byte filterTag, ByteString filterBytes)
    public IOException visitUnrecognizedFilter(final ASN1Writer writer,
        final byte filterTag, final ByteString filterBytes)
    {
      try
      {
        writer.writeOctetString(filterTag, filterBytes);
        return null;
      }
      catch (IOException e)
      catch (final IOException e)
      {
        return e;
      }
@@ -315,20 +310,19 @@
  /**
   * Reads the next ASN.1 element from the provided {@code ASN1Reader}
   * as a {@code Filter}.
   * Reads the next ASN.1 element from the provided {@code ASN1Reader} as a
   * {@code Filter}.
   *
   * @param reader
   *          The {@code ASN1Reader} from which the ASN.1 encoded
   *          {@code Filter} should be read.
   *          The {@code ASN1Reader} from which the ASN.1 encoded {@code Filter}
   *          should be read.
   * @return The decoded {@code Filter}.
   * @throws IOException
   *           If an error occurs while reading from {@code reader}.
   */
  public static Filter decodeFilter(ASN1Reader reader)
      throws IOException
  public static Filter decodeFilter(final ASN1Reader reader) throws IOException
  {
    byte type = reader.peekType();
    final byte type = reader.peekType();
    switch (type)
    {
@@ -357,58 +351,56 @@
      return decodeSubstringsFilter(reader);
    case TYPE_FILTER_PRESENCE:
      return Filter.newPresentFilter(reader
          .readOctetStringAsString(type));
      return Filter.newPresentFilter(reader.readOctetStringAsString(type));
    case TYPE_FILTER_EXTENSIBLE_MATCH:
      return decodeExtensibleMatchFilter(reader);
    default:
      return Filter.newUnrecognizedFilter(type, reader
          .readOctetString(type));
      return Filter.newUnrecognizedFilter(type, reader.readOctetString(type));
    }
  }
  /**
   * Reads the next ASN.1 element from the provided {@code ASN1Reader}
   * as a {@code SearchResultEntry}.
   * Reads the next ASN.1 element from the provided {@code ASN1Reader} as a
   * {@code SearchResultEntry}.
   *
   * @param reader
   *          The {@code ASN1Reader} from which the ASN.1 encoded
   *          {@code SearchResultEntry} should be read.
   * @param schema
   *          The schema to use when decoding the entry.
   *          The {@code ASN1Reader} from which the ASN.1 encoded {@code
   *          SearchResultEntry} should be read.
   * @param options
   *          The decode options to use when decoding the entry.
   * @return The decoded {@code SearchResultEntry}.
   * @throws IOException
   *           If an error occurs while reading from {@code reader}.
   */
  public static SearchResultEntry decodeSearchResultEntry(
      ASN1Reader reader, Schema schema) throws IOException
      final ASN1Reader reader, final DecodeOptions options) throws IOException
  {
    return LDAPDecoder.decodeEntry(reader, schema);
    return LDAPReader.decodeEntry(reader, options);
  }
  /**
   * Writes the ASN.1 encoding of the provided {@code Filter} to the
   * provided {@code ASN1Writer}.
   * Writes the ASN.1 encoding of the provided {@code Filter} to the provided
   * {@code ASN1Writer}.
   *
   * @param writer
   *          The {@code ASN1Writer} to which the ASN.1 encoding of the
   *          provided {@code Filter} should be written.
   *          The {@code ASN1Writer} to which the ASN.1 encoding of the provided
   *          {@code Filter} should be written.
   * @param filter
   *          The filter to be encoded.
   * @return The updated {@code ASN1Writer}.
   * @throws IOException
   *           If an error occurs while writing to {@code writer}.
   */
  public static ASN1Writer encodeFilter(ASN1Writer writer, Filter filter)
      throws IOException
  public static ASN1Writer encodeFilter(final ASN1Writer writer,
      final Filter filter) throws IOException
  {
    IOException e = filter.accept(ASN1_ENCODER, writer);
    final IOException e = filter.accept(ASN1_ENCODER, writer);
    if (e != null)
    {
      throw e;
@@ -422,29 +414,30 @@
  /**
   * Writes the ASN.1 encoding of the provided {@code SearchResultEntry}
   * to the provided {@code ASN1Writer}.
   * Writes the ASN.1 encoding of the provided {@code SearchResultEntry} to the
   * provided {@code ASN1Writer}.
   *
   * @param writer
   *          The {@code ASN1Writer} to which the ASN.1 encoding of the
   *          provided {@code SearchResultEntry} should be written.
   *          The {@code ASN1Writer} to which the ASN.1 encoding of the provided
   *          {@code SearchResultEntry} should be written.
   * @param entry
   *          The Search Result Entry to be encoded.
   * @return The updated {@code ASN1Writer}.
   * @throws IOException
   *           If an error occurs while writing to {@code writer}.
   */
  public static ASN1Writer encodeSearchResultEntry(ASN1Writer writer,
      SearchResultEntry entry) throws IOException
  public static ASN1Writer encodeSearchResultEntry(final ASN1Writer writer,
      final SearchResultEntry entry) throws IOException
  {
    LDAPEncoder.encodeEntry(writer, entry);
    // FIXME: this should include Controls.
    LDAPWriter.encodeEntry(writer, entry);
    return writer;
  }
  // Decodes an and filter.
  private static Filter decodeAndFilter(ASN1Reader reader)
  private static Filter decodeAndFilter(final ASN1Reader reader)
      throws IOException
  {
    Filter filter;
@@ -454,11 +447,12 @@
    {
      if (reader.hasNextElement())
      {
        List<Filter> subFilters = new LinkedList<Filter>();
        final List<Filter> subFilters = new LinkedList<Filter>();
        do
        {
          subFilters.add(decodeFilter(reader));
        } while (reader.hasNextElement());
        }
        while (reader.hasNextElement());
        filter = Filter.newAndFilter(subFilters);
      }
      else
@@ -478,7 +472,7 @@
  // Decodes an approximate match filter.
  private static Filter decodeApproxMatchFilter(ASN1Reader reader)
  private static Filter decodeApproxMatchFilter(final ASN1Reader reader)
      throws IOException
  {
    String attributeDescription;
@@ -495,14 +489,13 @@
      reader.readEndSequence();
    }
    return Filter.newApproxMatchFilter(attributeDescription,
        assertionValue);
    return Filter.newApproxMatchFilter(attributeDescription, assertionValue);
  }
  // Decodes an equality match filter.
  private static Filter decodeEqualityMatchFilter(ASN1Reader reader)
  private static Filter decodeEqualityMatchFilter(final ASN1Reader reader)
      throws IOException
  {
    String attributeDescription;
@@ -519,14 +512,13 @@
      reader.readEndSequence();
    }
    return Filter.newEqualityMatchFilter(attributeDescription,
        assertionValue);
    return Filter.newEqualityMatchFilter(attributeDescription, assertionValue);
  }
  // Decodes an extensible match filter.
  private static Filter decodeExtensibleMatchFilter(ASN1Reader reader)
  private static Filter decodeExtensibleMatchFilter(final ASN1Reader reader)
      throws IOException
  {
    String matchingRule;
@@ -540,8 +532,7 @@
      matchingRule = null;
      if (reader.peekType() == TYPE_MATCHING_RULE_ID)
      {
        matchingRule = reader
            .readOctetStringAsString(TYPE_MATCHING_RULE_ID);
        matchingRule = reader.readOctetStringAsString(TYPE_MATCHING_RULE_ID);
      }
      attributeDescription = null;
      if (reader.peekType() == TYPE_MATCHING_RULE_TYPE)
@@ -562,15 +553,15 @@
      reader.readEndSequence();
    }
    return Filter.newExtensibleMatchFilter(matchingRule,
        attributeDescription, assertionValue, dnAttributes);
    return Filter.newExtensibleMatchFilter(matchingRule, attributeDescription,
        assertionValue, dnAttributes);
  }
  // Decodes a greater than or equal filter.
  private static Filter decodeGreaterOrEqualMatchFilter(
      ASN1Reader reader) throws IOException
  private static Filter decodeGreaterOrEqualMatchFilter(final ASN1Reader reader)
      throws IOException
  {
    String attributeDescription;
    ByteString assertionValue;
@@ -585,14 +576,13 @@
    {
      reader.readEndSequence();
    }
    return Filter.newGreaterOrEqualFilter(attributeDescription,
        assertionValue);
    return Filter.newGreaterOrEqualFilter(attributeDescription, assertionValue);
  }
  // Decodes a less than or equal filter.
  private static Filter decodeLessOrEqualMatchFilter(ASN1Reader reader)
  private static Filter decodeLessOrEqualMatchFilter(final ASN1Reader reader)
      throws IOException
  {
    String attributeDescription;
@@ -609,14 +599,13 @@
      reader.readEndSequence();
    }
    return Filter.newLessOrEqualFilter(attributeDescription,
        assertionValue);
    return Filter.newLessOrEqualFilter(attributeDescription, assertionValue);
  }
  // Decodes a not filter.
  private static Filter decodeNotFilter(ASN1Reader reader)
  private static Filter decodeNotFilter(final ASN1Reader reader)
      throws IOException
  {
    Filter subFilter;
@@ -637,7 +626,7 @@
  // Decodes an or filter.
  private static Filter decodeOrFilter(ASN1Reader reader)
  private static Filter decodeOrFilter(final ASN1Reader reader)
      throws IOException
  {
    Filter filter;
@@ -647,11 +636,12 @@
    {
      if (reader.hasNextElement())
      {
        List<Filter> subFilters = new LinkedList<Filter>();
        final List<Filter> subFilters = new LinkedList<Filter>();
        do
        {
          subFilters.add(decodeFilter(reader));
        } while (reader.hasNextElement());
        }
        while (reader.hasNextElement());
        filter = Filter.newOrFilter(subFilters);
      }
      else
@@ -671,7 +661,7 @@
  // Decodes a sub-strings filter.
  private static Filter decodeSubstringsFilter(ASN1Reader reader)
  private static Filter decodeSubstringsFilter(final ASN1Reader reader)
      throws IOException
  {
    ByteString initialSubstring = null;
@@ -692,18 +682,16 @@
        {
          initialSubstring = reader.readOctetString(TYPE_SUBINITIAL);
        }
        if (reader.hasNextElement()
            && (reader.peekType() == TYPE_SUBANY))
        if (reader.hasNextElement() && (reader.peekType() == TYPE_SUBANY))
        {
          anySubstrings = new LinkedList<ByteString>();
          do
          {
            anySubstrings.add(reader.readOctetString(TYPE_SUBANY));
          } while (reader.hasNextElement()
              && (reader.peekType() == TYPE_SUBANY));
          }
          while (reader.hasNextElement() && (reader.peekType() == TYPE_SUBANY));
        }
        if (reader.hasNextElement()
            && (reader.peekType() == TYPE_SUBFINAL))
        if (reader.hasNextElement() && (reader.peekType() == TYPE_SUBFINAL))
        {
          finalSubstring = reader.readOctetString(TYPE_SUBFINAL);
        }
@@ -723,8 +711,8 @@
      anySubstrings = Collections.emptyList();
    }
    return Filter.newSubstringsFilter(attributeDescription,
        initialSubstring, anySubstrings, finalSubstring);
    return Filter.newSubstringsFilter(attributeDescription, initialSubstring,
        anySubstrings, finalSubstring);
  }