001/*
002 * Copyright 2009-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2020 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) 2015-2020 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.sdk.unboundidds.examples;
037
038
039
040import java.io.Serializable;
041import java.util.Arrays;
042import java.util.TreeSet;
043
044import com.unboundid.ldap.sdk.Filter;
045import com.unboundid.util.NotMutable;
046import com.unboundid.util.StaticUtils;
047import com.unboundid.util.ThreadSafety;
048import com.unboundid.util.ThreadSafetyLevel;
049
050
051
052/**
053 * This class provides a data structure for representing search filters in a
054 * generic way.
055 * <BR>
056 * <BLOCKQUOTE>
057 *   <B>NOTE:</B>  This class, and other classes within the
058 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
059 *   supported for use against Ping Identity, UnboundID, and
060 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
061 *   for proprietary functionality or for external specifications that are not
062 *   considered stable or mature enough to be guaranteed to work in an
063 *   interoperable way with other types of LDAP servers.
064 * </BLOCKQUOTE>
065 * <BR>
066 * This includes:
067 * <UL>
068 *   <LI>Using a consistent order for AND and OR components.</LI>
069 *   <LI>Converting all attribute names to lowercase.</LI>
070 *   <LI>Replacing the assertion value with a "?" character for equality,
071 *       greater-or-equal, less-or-equal, approximate match, and extensible
072 *       match filters.</LI>
073 *   <LI>Replacing all subInitial, subAny, and subFinal elements with "?"
074 *       characters in substring filters.</LI>
075 * </UL>
076 */
077@NotMutable()
078@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
079public final class GenericFilter
080       implements Serializable
081{
082  /**
083   * The serial version UID for this serializable class.
084   */
085  private static final long serialVersionUID = -7875317078624475546L;
086
087
088
089  // The hash code for this generic filter.
090  private final int hashCode;
091
092  // The string representation for this filter.
093  private final String filterString;
094
095
096
097  /**
098   * Creates a new generic filter from the provided search filter.
099   *
100   * @param  f  The filter to use to create a generic filter.
101   */
102  public GenericFilter(final Filter f)
103  {
104    final StringBuilder b = new StringBuilder();
105    b.append('(');
106
107    switch (f.getFilterType())
108    {
109      case Filter.FILTER_TYPE_AND:
110      case Filter.FILTER_TYPE_OR:
111        appendComponents(f, b);
112        break;
113
114      case Filter.FILTER_TYPE_NOT:
115        b.append('!');
116        b.append(new GenericFilter(f.getNOTComponent()).toString());
117        break;
118
119      case Filter.FILTER_TYPE_EQUALITY:
120        b.append(StaticUtils.toLowerCase(f.getAttributeName()));
121        b.append("=?");
122        break;
123
124      case Filter.FILTER_TYPE_SUBSTRING:
125        b.append(StaticUtils.toLowerCase(f.getAttributeName()));
126        b.append('=');
127        if (f.getRawSubInitialValue() != null)
128        {
129          b.append('?');
130        }
131        for (int i=0; i < f.getRawSubAnyValues().length; i++)
132        {
133          b.append("*?");
134        }
135        b.append('*');
136        if (f.getRawSubFinalValue() != null)
137        {
138          b.append('?');
139        }
140        break;
141
142      case Filter.FILTER_TYPE_GREATER_OR_EQUAL:
143        b.append(StaticUtils.toLowerCase(f.getAttributeName()));
144        b.append(">=?");
145        break;
146
147      case Filter.FILTER_TYPE_LESS_OR_EQUAL:
148        b.append(StaticUtils.toLowerCase(f.getAttributeName()));
149        b.append("<=?");
150        break;
151
152      case Filter.FILTER_TYPE_PRESENCE:
153        b.append(StaticUtils.toLowerCase(f.getAttributeName()));
154        b.append("=*");
155        break;
156
157      case Filter.FILTER_TYPE_APPROXIMATE_MATCH:
158        b.append(StaticUtils.toLowerCase(f.getAttributeName()));
159        b.append("~=?");
160        break;
161
162      case Filter.FILTER_TYPE_EXTENSIBLE_MATCH:
163        final String attrName = StaticUtils.toLowerCase(f.getAttributeName());
164        final String mrID     = StaticUtils.toLowerCase(f.getMatchingRuleID());
165        if (attrName != null)
166        {
167          b.append(attrName);
168        }
169        if (f.getDNAttributes())
170        {
171          b.append(":dn");
172        }
173        if (mrID != null)
174        {
175          b.append(':');
176          b.append(mrID);
177        }
178        b.append(":=?");
179        break;
180    }
181
182    b.append(')');
183
184    filterString = b.toString();
185    hashCode     = filterString.hashCode();
186  }
187
188
189
190  /**
191   * Appends a string representation of the provided AND or OR filter to the
192   * given buffer.
193   *
194   * @param  f  The filter for which to provide the string representation.
195   * @param  b  The buffer to which to append the string representation.
196   */
197  private static void appendComponents(final Filter f, final StringBuilder b)
198  {
199    if (f.getFilterType() == Filter.FILTER_TYPE_AND)
200    {
201      b.append('&');
202    }
203    else
204    {
205      b.append('|');
206    }
207
208    final TreeSet<Filter> compSet =
209         new TreeSet<>(FilterComparator.getInstance());
210    compSet.addAll(Arrays.asList(f.getComponents()));
211    for (final Filter fc : compSet)
212    {
213      b.append(new GenericFilter(fc).toString());
214    }
215  }
216
217
218
219  /**
220   * Retrieves a hash code for this generic filter.
221   *
222   * @return  A hash code for this generic filter.
223   */
224  @Override()
225  public int hashCode()
226  {
227    return hashCode;
228  }
229
230
231
232  /**
233   * Indicates whether the provided object is equal to this generic filter.
234   *
235   * @param  o  The object for which to make the determination.
236   *
237   * @return  {@code true} the provided object is equal to this generic filter,
238   *          or {@code false} if not.
239   */
240  @Override()
241  public boolean equals(final Object o)
242  {
243    if (o == null)
244    {
245      return false;
246    }
247
248    if (o == this)
249    {
250      return true;
251    }
252
253    return ((o instanceof GenericFilter) &&
254            filterString.equals(((GenericFilter) o).filterString));
255  }
256
257
258
259  /**
260   * Retrieves a string representation of this generic filter.
261   *
262   * @return  A string representation of this generic filter.
263   */
264  @Override()
265  public String toString()
266  {
267    return filterString;
268  }
269}