001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.model.schema.normalizers;
021
022
023import java.text.ParseException;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.constants.SchemaConstants;
027import org.apache.directory.api.ldap.model.exception.LdapException;
028import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
029import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
030import org.apache.directory.api.ldap.model.schema.Normalizer;
031import org.apache.directory.api.ldap.model.schema.PrepareString;
032import org.apache.directory.api.util.GeneralizedTime;
033import org.apache.directory.api.util.GeneralizedTime.Format;
034import org.apache.directory.api.util.GeneralizedTime.FractionDelimiter;
035import org.apache.directory.api.util.GeneralizedTime.TimeZoneFormat;
036
037
038/**
039 * Normalizer which normalize a time following those rules :
040 * <ul>
041 * <li>if minutes are ommited, then they are replaced by 00</li>
042 * <li>if seconds are ommited, then they are replaced by 00</li>
043 * <li>if fraction is 0 or omitted, it is replaced by 000</li>
044 * <li>the time is supposed to be expressed in Zulu (GMT), so 
045 * increment is applied to hours/days/yeah, and a Z is added at the end</li>
046 * </ul>
047 * 
048 * Note : there is no Substring for this type of values.
049 *
050 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
051 */
052@SuppressWarnings("serial")
053public class GeneralizedTimeNormalizer extends Normalizer
054{
055    /**
056     * Creates a new instance of GeneralizedTimeNormalizer.
057     */
058    public GeneralizedTimeNormalizer()
059    {
060        super( SchemaConstants.GENERALIZED_TIME_MATCH_MR_OID );
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public String normalize( String value ) throws LdapException
069    {
070        return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
079    {
080        if ( value == null )
081        {
082            throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
083                I18n.ERR_13724_INVALID_VALUE, value ) );
084        }
085        
086        // Special case : the PPolicy "0000010000Z", for permanently locked accounts
087        if ( "000001010000Z".equals( value ) )
088        {
089            return value;
090        }
091        
092        try
093        {
094            GeneralizedTime time = new GeneralizedTime( value );
095            return time.toGeneralizedTime( Format.YEAR_MONTH_DAY_HOUR_MIN_SEC_FRACTION,
096                FractionDelimiter.DOT, 3, TimeZoneFormat.Z );
097        }
098        catch ( ParseException pe )
099        {
100            throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
101                I18n.ERR_13724_INVALID_VALUE, value ), pe );
102        }
103    }
104}