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