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