| File was renamed from sdk/src/org/opends/sdk/ldap/LDAPUtils.java |
| | |
| | | * Copyright 2009 Sun Microsystems, Inc. |
| | | */ |
| | | |
| | | package org.opends.sdk.ldap; |
| | | package com.sun.opends.sdk.ldap; |
| | | |
| | | |
| | | |
| | |
| | | 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; |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | public IOException visitApproxMatchFilter(ASN1Writer writer, |
| | | String attributeDescription, ByteString assertionValue) |
| | | public IOException visitApproxMatchFilter(final ASN1Writer writer, |
| | | final String attributeDescription, final ByteString assertionValue) |
| | | { |
| | | try |
| | | { |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | public IOException visitEqualityMatchFilter(ASN1Writer writer, |
| | | String attributeDescription, ByteString assertionValue) |
| | | public IOException visitEqualityMatchFilter(final ASN1Writer writer, |
| | | final String attributeDescription, final ByteString assertionValue) |
| | | { |
| | | try |
| | | { |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | 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 |
| | | { |
| | |
| | | |
| | | 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) |
| | | { |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | public IOException visitGreaterOrEqualFilter(ASN1Writer writer, |
| | | String attributeDescription, ByteString assertionValue) |
| | | public IOException visitGreaterOrEqualFilter(final ASN1Writer writer, |
| | | final String attributeDescription, final ByteString assertionValue) |
| | | { |
| | | try |
| | | { |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | public IOException visitLessOrEqualFilter(ASN1Writer writer, |
| | | String attributeDescription, ByteString assertionValue) |
| | | public IOException visitLessOrEqualFilter(final ASN1Writer writer, |
| | | final String attributeDescription, final ByteString assertionValue) |
| | | { |
| | | try |
| | | { |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | 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; |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | 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; |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | 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; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | 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 |
| | | { |
| | |
| | | writer.writeOctetString(TYPE_SUBINITIAL, initialSubstring); |
| | | } |
| | | |
| | | for (ByteSequence anySubstring : anySubstrings) |
| | | for (final ByteSequence anySubstring : anySubstrings) |
| | | { |
| | | writer.writeOctetString(TYPE_SUBANY, anySubstring); |
| | | } |
| | |
| | | writer.writeEndSequence(); |
| | | return null; |
| | | } |
| | | catch (IOException e) |
| | | catch (final IOException e) |
| | | { |
| | | return e; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | 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; |
| | | } |
| | |
| | | |
| | | |
| | | /** |
| | | * 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) |
| | | { |
| | |
| | | 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; |
| | |
| | | |
| | | |
| | | /** |
| | | * 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; |
| | |
| | | { |
| | | 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 |
| | |
| | | |
| | | |
| | | // Decodes an approximate match filter. |
| | | private static Filter decodeApproxMatchFilter(ASN1Reader reader) |
| | | private static Filter decodeApproxMatchFilter(final ASN1Reader reader) |
| | | throws IOException |
| | | { |
| | | String attributeDescription; |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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) |
| | |
| | | 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; |
| | |
| | | { |
| | | 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; |
| | |
| | | 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; |
| | |
| | | |
| | | |
| | | // Decodes an or filter. |
| | | private static Filter decodeOrFilter(ASN1Reader reader) |
| | | private static Filter decodeOrFilter(final ASN1Reader reader) |
| | | throws IOException |
| | | { |
| | | Filter filter; |
| | |
| | | { |
| | | 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 |
| | |
| | | |
| | | |
| | | // Decodes a sub-strings filter. |
| | | private static Filter decodeSubstringsFilter(ASN1Reader reader) |
| | | private static Filter decodeSubstringsFilter(final ASN1Reader reader) |
| | | throws IOException |
| | | { |
| | | ByteString initialSubstring = null; |
| | |
| | | { |
| | | 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); |
| | | } |
| | |
| | | anySubstrings = Collections.emptyList(); |
| | | } |
| | | |
| | | return Filter.newSubstringsFilter(attributeDescription, |
| | | initialSubstring, anySubstrings, finalSubstring); |
| | | return Filter.newSubstringsFilter(attributeDescription, initialSubstring, |
| | | anySubstrings, finalSubstring); |
| | | } |
| | | |
| | | |