001/*
002 * Copyright 2008-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2008-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.matchingrules;
022
023
024
025import com.unboundid.asn1.ASN1OctetString;
026import com.unboundid.ldap.sdk.LDAPException;
027import com.unboundid.util.Debug;
028import com.unboundid.util.Extensible;
029import com.unboundid.util.ThreadSafety;
030import com.unboundid.util.ThreadSafetyLevel;
031
032
033
034/**
035 * This class provides a common matching rule framework that may be extended by
036 * matching rule implementations in which equality, ordering, and substring
037 * matching can all be made based on byte-for-byte comparisons of the normalized
038 * value, and any value is acceptable.
039 */
040@Extensible()
041@ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE)
042public abstract class AcceptAllSimpleMatchingRule
043       extends SimpleMatchingRule
044{
045  /**
046   * The serial version UID for this serializable class.
047   */
048  private static final long serialVersionUID = -7450007924568660003L;
049
050
051
052  /**
053   * {@inheritDoc}
054   */
055  @Override()
056  public boolean valuesMatch(final ASN1OctetString value1,
057                             final ASN1OctetString value2)
058  {
059    return normalize(value1).equalsIgnoreType(normalize(value2));
060  }
061
062
063
064  /**
065   * {@inheritDoc}
066   */
067  @Override()
068  public boolean matchesAnyValue(final ASN1OctetString assertionValue,
069                                 final ASN1OctetString[] attributeValues)
070  {
071    if ((assertionValue == null) || (attributeValues == null) ||
072        (attributeValues.length == 0))
073    {
074      return false;
075    }
076
077    final ASN1OctetString normalizedAssertionValue = normalize(assertionValue);
078
079    for (final ASN1OctetString attributeValue : attributeValues)
080    {
081      if (normalizedAssertionValue.equalsIgnoreType(normalize(attributeValue)))
082      {
083        return true;
084      }
085    }
086
087    return false;
088  }
089
090
091
092  /**
093   * {@inheritDoc}
094   */
095  @Override()
096  public boolean matchesSubstring(final ASN1OctetString value,
097                                  final ASN1OctetString subInitial,
098                                  final ASN1OctetString[] subAny,
099                                  final ASN1OctetString subFinal)
100  {
101    try
102    {
103      return super.matchesSubstring(value, subInitial, subAny, subFinal);
104    }
105    catch (final LDAPException le)
106    {
107      Debug.debugException(le);
108
109      // This should never happen, as the only reason the superclass version of
110      // this method will throw an exception is if an exception is thrown by
111      // normalize or normalizeSubstring.
112      return false;
113    }
114  }
115
116
117
118  /**
119   * {@inheritDoc}
120   */
121  @Override()
122  public int compareValues(final ASN1OctetString value1,
123                           final ASN1OctetString value2)
124  {
125    try
126    {
127      return super.compareValues(value1, value2);
128    }
129    catch (final LDAPException le)
130    {
131      Debug.debugException(le);
132
133      // This should never happen, as the only reason the superclass version of
134      // this method will throw an exception is if an exception is thrown by
135      // normalize or normalizeSubstring.
136      return 0;
137    }
138  }
139
140
141
142  /**
143   * {@inheritDoc}  This variant of the {@code normalize} method is not allowed
144   * to throw exceptions.
145   */
146  @Override()
147  public abstract ASN1OctetString normalize(ASN1OctetString value);
148
149
150
151  /**
152   * {@inheritDoc}  This variant of the {@code normalizeSubstring} method is not
153   * allowed to throw exceptions.
154   */
155  @Override()
156  public abstract ASN1OctetString normalizeSubstring(ASN1OctetString value,
157                                                     byte substringType);
158}