View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.model.schema.normalizers;
21  
22  
23  import java.text.ParseException;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
29  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
30  import org.apache.directory.api.ldap.model.schema.Normalizer;
31  import org.apache.directory.api.ldap.model.schema.PrepareString;
32  import org.apache.directory.api.util.GeneralizedTime;
33  import org.apache.directory.api.util.GeneralizedTime.Format;
34  import org.apache.directory.api.util.GeneralizedTime.FractionDelimiter;
35  import org.apache.directory.api.util.GeneralizedTime.TimeZoneFormat;
36  
37  
38  /**
39   * Normalizer which normalize a time following those rules :
40   * <ul>
41   * <li>if minutes are ommited, then they are replaced by 00</li>
42   * <li>if seconds are ommited, then they are replaced by 00</li>
43   * <li>if fraction is 0 or omitted, it is replaced by 000</li>
44   * <li>the time is supposed to be expressed in Zulu (GMT), so 
45   * increment is applied to hours/days/yeah, and a Z is added at the end</li>
46   * </ul>
47   * 
48   * Note : there is no Substring for this type of values.
49   *
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   */
52  @SuppressWarnings("serial")
53  public class GeneralizedTimeNormalizer extends Normalizer
54  {
55      /**
56       * Creates a new instance of GeneralizedTimeNormalizer.
57       */
58      public GeneralizedTimeNormalizer()
59      {
60          super( SchemaConstants.GENERALIZED_TIME_MATCH_MR_OID );
61      }
62  
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public String normalize( String value ) throws LdapException
69      {
70          return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
79      {
80          if ( value == null )
81          {
82              throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
83                  I18n.ERR_13724_INVALID_VALUE, value ) );
84          }
85          
86          // Special case : the PPolicy "0000010000Z", for permanently locked accounts
87          if ( "000001010000Z".equals( value ) )
88          {
89              return value;
90          }
91          
92          try
93          {
94              GeneralizedTime time = new GeneralizedTime( value );
95              return time.toGeneralizedTime( Format.YEAR_MONTH_DAY_HOUR_MIN_SEC_FRACTION,
96                  FractionDelimiter.DOT, 3, TimeZoneFormat.Z );
97          }
98          catch ( ParseException pe )
99          {
100             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
101                 I18n.ERR_13724_INVALID_VALUE, value ), pe );
102         }
103     }
104 }