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 *    http://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.shared.kerberos.codec.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.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * The action used to read the PVNO
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public abstract class AbstractReadPvno<E extends Asn1Container> extends GrammarAction<E>
041{
042    /** The logger */
043    private static final Logger LOG = LoggerFactory.getLogger( AbstractReadPvno.class );
044
045    /** Speedup for logs */
046    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
047
048
049    /**
050     * Creates a new instance of AbstractReadPvno.
051     *
052     * @param name the name of the action
053     */
054    public AbstractReadPvno( String name )
055    {
056        super( name );
057    }
058
059
060    /**
061     * sets the pvno on the ASN.1 object present in the container
062     *
063     * @param pvno the protocol version number received
064     * @param container the ASN.1 object's container
065     */
066    protected abstract void setPvno( int pvno, E container );
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public final void action( E container ) throws DecoderException
073    {
074        TLV tlv = container.getCurrentTLV();
075
076        // The Length should not be null and should be 1
077        if ( tlv.getLength() != 1 )
078        {
079            LOG.error( I18n.err( I18n.ERR_01308_ZERO_LENGTH_TLV ) );
080
081            // This will generate a PROTOCOL_ERROR
082            throw new DecoderException( I18n.err( I18n.ERR_01309_EMPTY_TLV ) );
083        }
084
085        BerValue value = tlv.getValue();
086
087        try
088        {
089            int pvno = IntegerDecoder.parse( value );
090
091            if ( pvno != 5 )
092            {
093                LOG.error( I18n.err( I18n.ERR_01306_VALUE_NOT_IN_RANGE, 5, 5 ) );
094
095                // This will generate a PROTOCOL_ERROR
096                throw new DecoderException( "The PVNO should be 5" );
097            }
098
099            if ( IS_DEBUG )
100            {
101                LOG.debug( "pvno : {}", pvno );
102            }
103
104            setPvno( pvno, container );
105        }
106        catch ( IntegerDecoderException ide )
107        {
108            LOG.error( I18n.err( I18n.ERR_01306_VALUE_NOT_IN_RANGE, 5, 5 ) );
109
110            // This will generate a PROTOCOL_ERROR
111            throw new DecoderException( ide.getMessage() );
112        }
113    }
114}