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