001/*
002 * Copyright 2015-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2015-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) 2015-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.sdk.unboundidds.jsonfilter;
037
038
039
040import com.unboundid.asn1.ASN1OctetString;
041import com.unboundid.ldap.matchingrules.MatchingRule;
042import com.unboundid.ldap.sdk.LDAPException;
043import com.unboundid.ldap.sdk.ResultCode;
044import com.unboundid.util.Debug;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.Nullable;
047import com.unboundid.util.ThreadSafety;
048import com.unboundid.util.ThreadSafetyLevel;
049import com.unboundid.util.json.JSONException;
050import com.unboundid.util.json.JSONObject;
051
052import static com.unboundid.ldap.sdk.unboundidds.jsonfilter.JFMessages.*;
053
054
055
056/**
057 * This class provides an implementation of a matching rule that can be used in
058 * conjunction with JSON objects.
059 * <BR>
060 * <BLOCKQUOTE>
061 *   <B>NOTE:</B>  This class, and other classes within the
062 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
063 *   supported for use against Ping Identity, UnboundID, and
064 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
065 *   for proprietary functionality or for external specifications that are not
066 *   considered stable or mature enough to be guaranteed to work in an
067 *   interoperable way with other types of LDAP servers.
068 * </BLOCKQUOTE>
069 */
070@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
071public final class JSONObjectExactMatchingRule
072       extends MatchingRule
073{
074  /**
075   * The singleton instance that will be returned from the {@link #getInstance}
076   * method.
077   */
078  @NotNull private static final JSONObjectExactMatchingRule INSTANCE =
079       new JSONObjectExactMatchingRule();
080
081
082
083  /**
084   * The serial version UID for this serializable class.
085   */
086  private static final long serialVersionUID = -4476702301631553228L;
087
088
089
090  /**
091   * Retrieves a singleton instance of this matching rule.
092   *
093   * @return A singleton instance of this matching rule.
094   */
095  @NotNull public static JSONObjectExactMatchingRule getInstance()
096  {
097    return INSTANCE;
098  }
099
100
101
102  /**
103   * Creates a new instance of this JSON matching rule.
104   */
105  public JSONObjectExactMatchingRule()
106  {
107    // No implementation is required.
108  }
109
110
111
112  /**
113   * {@inheritDoc}
114   */
115  @Override()
116  @NotNull()
117  public String getEqualityMatchingRuleName()
118  {
119    return "jsonObjectExactMatch";
120  }
121
122
123
124  /**
125   * {@inheritDoc}
126   */
127  @Override()
128  @NotNull()
129  public String getEqualityMatchingRuleOID()
130  {
131    return "1.3.6.1.4.1.30221.2.4.12";
132  }
133
134
135
136  /**
137   * {@inheritDoc}
138   */
139  @Override()
140  @Nullable()
141  public String getOrderingMatchingRuleName()
142  {
143    // Ordering matching is not supported.
144    return null;
145  }
146
147
148
149  /**
150   * {@inheritDoc}
151   */
152  @Override()
153  @Nullable()
154  public String getOrderingMatchingRuleOID()
155  {
156    // Ordering matching is not supported.
157    return null;
158  }
159
160
161
162  /**
163   * {@inheritDoc}
164   */
165  @Override()
166  @Nullable()
167  public String getSubstringMatchingRuleName()
168  {
169    // Substring matching is not supported.
170    return null;
171  }
172
173
174
175  /**
176   * {@inheritDoc}
177   */
178  @Override()
179  @Nullable()
180  public String getSubstringMatchingRuleOID()
181  {
182    // Substring matching is not supported.
183    return null;
184  }
185
186
187
188  /**
189   * {@inheritDoc}
190   */
191  @Override()
192  public boolean valuesMatch(@NotNull final ASN1OctetString value1,
193                             @NotNull final ASN1OctetString value2)
194         throws LDAPException
195  {
196    final JSONObject o1;
197    try
198    {
199      o1 = new JSONObject(value1.stringValue());
200    }
201    catch (final JSONException e)
202    {
203      Debug.debugException(e);
204      throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
205           e.getMessage(), e);
206    }
207
208    final JSONObject o2;
209    try
210    {
211      o2 = new JSONObject(value2.stringValue());
212    }
213    catch (final JSONException e)
214    {
215      Debug.debugException(e);
216      throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
217           e.getMessage(), e);
218    }
219
220    return o1.equals(o2, false, true, false);
221  }
222
223
224
225  /**
226   * {@inheritDoc}
227   */
228  @Override()
229  public boolean matchesSubstring(@NotNull final ASN1OctetString value,
230                                  @Nullable final ASN1OctetString subInitial,
231                                  @Nullable final ASN1OctetString[] subAny,
232                                  @Nullable final ASN1OctetString subFinal)
233         throws LDAPException
234  {
235    // Substring matching is not supported for this matching rule.
236    throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING,
237         ERR_JSON_MATCHING_RULE_SUBSTRING_NOT_SUPPORTED.get());
238  }
239
240
241
242  /**
243   * {@inheritDoc}
244   */
245  @Override()
246  public int compareValues(@NotNull final ASN1OctetString value1,
247                           @NotNull final ASN1OctetString value2)
248         throws LDAPException
249  {
250    // Ordering matching is not supported for this matching rule.
251    throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING,
252         ERR_JSON_MATCHING_RULE_ORDERING_NOT_SUPPORTED.get());
253  }
254
255
256
257  /**
258   * {@inheritDoc}
259   */
260  @Override()
261  @NotNull()
262  public ASN1OctetString normalize(@NotNull final ASN1OctetString value)
263         throws LDAPException
264  {
265    final JSONObject o;
266    try
267    {
268      o = new JSONObject(value.stringValue());
269    }
270    catch (final JSONException e)
271    {
272      Debug.debugException(e);
273      throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
274           e.getMessage(), e);
275    }
276
277    return new ASN1OctetString(o.toNormalizedString());
278  }
279
280
281
282  /**
283   * {@inheritDoc}
284   */
285  @Override()
286  @NotNull()
287  public ASN1OctetString normalizeSubstring(
288              @NotNull final ASN1OctetString value, final byte substringType)
289         throws LDAPException
290  {
291    // Substring matching is not supported for this matching rule.
292    throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING,
293         ERR_JSON_MATCHING_RULE_SUBSTRING_NOT_SUPPORTED.get());
294  }
295}