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.asn1.actions;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.Asn1Container;
025import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
026import org.apache.directory.api.asn1.ber.tlv.BerValue;
027import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
028import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
029import org.apache.directory.api.asn1.ber.tlv.TLV;
030import org.apache.directory.api.i18n.I18n;
031import org.apache.directory.api.util.Strings;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035
036/**
037 * The action used to read an integer value
038 *
039 * @param <E> The container type
040 * 
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public abstract class AbstractReadInteger<E extends Asn1Container> extends GrammarAction<E>
044{
045    /** The logger */
046    private static final Logger LOG = LoggerFactory.getLogger( AbstractReadInteger.class );
047
048    /** the acceptable minimum value for the expected value to be parsed */
049    private int minValue = 0;
050
051    /** the acceptable maximum value for the expected value to be parsed */
052    private int maxValue = Integer.MAX_VALUE;
053
054
055    /**
056     * Instantiates a new AbstractReadInteger action.
057     * 
058     * @param name the action's name
059     */
060    public AbstractReadInteger( String name )
061    {
062        super( name );
063    }
064
065
066    /**
067     *
068     * Creates a new instance of AbstractReadInteger.
069     *
070     * @param name the action's name
071     * @param minValue the acceptable minimum value for the expected value to be read
072     * @param maxValue the acceptable maximum value for the value to be read
073     */
074    public AbstractReadInteger( String name, int minValue, int maxValue )
075    {
076        super( name );
077
078        this.minValue = minValue;
079        this.maxValue = maxValue;
080    }
081
082
083    /**
084     *
085     * set the integer value to the appropriate field of ASN.1 object present in the container
086     *
087     * @param value the integer value
088     * @param container the ASN.1 object's container
089     */
090    protected abstract void setIntegerValue( int value, E container );
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public final void action( E container ) throws DecoderException
098    {
099        TLV tlv = container.getCurrentTLV();
100
101        // The Length should not be null
102        if ( tlv.getLength() == 0 )
103        {
104            String msg = I18n.err( I18n.ERR_01101_NULL_LENGTH );
105            
106            LOG.error( msg );
107
108            // This will generate a PROTOCOL_ERROR
109            throw new DecoderException( msg );
110        }
111
112        BerValue value = tlv.getValue();
113
114        try
115        {
116            int number = IntegerDecoder.parse( value, minValue, maxValue );
117
118            if ( LOG.isDebugEnabled() )
119            {
120                LOG.debug( I18n.msg( I18n.MSG_01100_INTEGER_VALUE, number ) );
121            }
122
123            setIntegerValue( number, container );
124        }
125        catch ( IntegerDecoderException ide )
126        {
127            LOG.error( I18n.err( I18n.ERR_01102_INVALID_INTEGER, Strings.dumpBytes( value.getData() ), ide
128                .getLocalizedMessage() ) );
129
130            // This will generate a PROTOCOL_ERROR
131            throw new DecoderException( ide.getMessage(), ide );
132        }
133    }
134}