001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.schema;
028    
029    
030    
031    import org.opends.server.admin.std.server.EqualityMatchingRuleCfg;
032    import org.opends.server.api.EqualityMatchingRule;
033    import org.opends.server.config.ConfigException;
034    import org.opends.server.core.DirectoryServer;
035    import org.opends.server.loggers.debug.DebugTracer;
036    import org.opends.server.protocols.asn1.ASN1OctetString;
037    import org.opends.server.types.ByteString;
038    import org.opends.server.types.DebugLogLevel;
039    import org.opends.server.types.DirectoryException;
040    import org.opends.server.types.InitializationException;
041    
042    import static org.opends.server.loggers.ErrorLogger.*;
043    import static org.opends.server.loggers.debug.DebugLogger.*;
044    import static org.opends.server.schema.SchemaConstants.*;
045    import static org.opends.server.util.ServerConstants.*;
046    
047    
048    /**
049     * This class defines the generalizedTimeMatch matching rule defined in X.520
050     * and referenced in RFC 2252.
051     */
052    public class GeneralizedTimeEqualityMatchingRule
053           extends EqualityMatchingRule
054    {
055      /**
056       * The tracer object for the debug logger.
057       */
058      private static final DebugTracer TRACER = getTracer();
059    
060    
061    
062      /**
063       * Creates a new instance of this generalizedTimeMatch matching rule.
064       */
065      public GeneralizedTimeEqualityMatchingRule()
066      {
067        super();
068      }
069    
070    
071    
072      /**
073       * {@inheritDoc}
074       */
075      public void initializeMatchingRule(EqualityMatchingRuleCfg configuration)
076             throws ConfigException, InitializationException
077      {
078        // No initialization is required.
079      }
080    
081    
082    
083      /**
084       * Retrieves the common name for this matching rule.
085       *
086       * @return  The common name for this matching rule, or <CODE>null</CODE> if
087       * it does not have a name.
088       */
089      public String getName()
090      {
091        return EMR_GENERALIZED_TIME_NAME;
092      }
093    
094    
095    
096      /**
097       * Retrieves the OID for this matching rule.
098       *
099       * @return  The OID for this matching rule.
100       */
101      public String getOID()
102      {
103        return EMR_GENERALIZED_TIME_OID;
104      }
105    
106    
107    
108      /**
109       * Retrieves the description for this matching rule.
110       *
111       * @return  The description for this matching rule, or <CODE>null</CODE> if
112       *          there is none.
113       */
114      public String getDescription()
115      {
116        // There is no standard description for this matching rule.
117        return null;
118      }
119    
120    
121    
122      /**
123       * Retrieves the OID of the syntax with which this matching rule is
124       * associated.
125       *
126       * @return  The OID of the syntax with which this matching rule is associated.
127       */
128      public String getSyntaxOID()
129      {
130        return SYNTAX_GENERALIZED_TIME_OID;
131      }
132    
133    
134    
135      /**
136       * Retrieves the normalized form of the provided value, which is best suited
137       * for efficiently performing matching operations on that value.
138       *
139       * @param  value  The value to be normalized.
140       *
141       * @return  The normalized version of the provided value.
142       *
143       * @throws  DirectoryException  If the provided value is invalid according to
144       *                              the associated attribute syntax.
145       */
146      public ByteString normalizeValue(ByteString value)
147             throws DirectoryException
148      {
149        try
150        {
151          long timestamp = GeneralizedTimeSyntax.decodeGeneralizedTimeValue(value);
152          return new ASN1OctetString(GeneralizedTimeSyntax.format(timestamp));
153        }
154        catch (DirectoryException de)
155        {
156          if (debugEnabled())
157          {
158            TRACER.debugCaught(DebugLogLevel.ERROR, de);
159          }
160    
161          switch (DirectoryServer.getSyntaxEnforcementPolicy())
162          {
163            case REJECT:
164              throw de;
165    
166            case WARN:
167              logError(de.getMessageObject());
168              return new ASN1OctetString(value.value());
169    
170            default:
171              return new ASN1OctetString(value.value());
172          }
173        }
174      }
175    
176    
177    
178      /**
179       * Indicates whether the two provided normalized values are equal to each
180       * other.
181       *
182       * @param  value1  The normalized form of the first value to compare.
183       * @param  value2  The normalized form of the second value to compare.
184       *
185       * @return  <CODE>true</CODE> if the provided values are equal, or
186       *          <CODE>false</CODE> if not.
187       */
188      public boolean areEqual(ByteString value1, ByteString value2)
189      {
190        try
191        {
192          long time1 = GeneralizedTimeSyntax.decodeGeneralizedTimeValue(value1);
193          long time2 = GeneralizedTimeSyntax.decodeGeneralizedTimeValue(value2);
194          return (time1 == time2);
195        }
196        catch (DirectoryException de)
197        {
198          if (debugEnabled())
199          {
200            TRACER.debugCaught(DebugLogLevel.ERROR, de);
201          }
202    
203          return false;
204        }
205      }
206    }
207