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