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.lastReq;
021
022
023import org.apache.directory.api.asn1.actions.CheckNotNullLength;
024import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
025import org.apache.directory.api.asn1.ber.grammar.Grammar;
026import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
027import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
028import org.apache.directory.shared.kerberos.KerberosConstants;
029import org.apache.directory.shared.kerberos.codec.lastReq.actions.LastReqInit;
030import org.apache.directory.shared.kerberos.codec.lastReq.actions.StoreLrType;
031import org.apache.directory.shared.kerberos.codec.lastReq.actions.StoreLrValue;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035
036/**
037 * This class implements the LastReq structure. All the actions are declared
038 * in this class. As it is a singleton, these declaration are only done once. If
039 * an action is to be added or modified, this is where the work is to be done !
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public final class LastReqGrammar extends AbstractGrammar<LastReqContainer>
044{
045    /** The logger */
046    static final Logger LOG = LoggerFactory.getLogger( LastReqGrammar.class );
047
048    /** A speedup for logger */
049    static final boolean IS_DEBUG = LOG.isDebugEnabled();
050
051    /** The instance of grammar. LastReqGrammar is a singleton */
052    private static Grammar<LastReqContainer> instance = new LastReqGrammar();
053
054
055    /**
056     * Creates a new LastReqGrammar object.
057     */
058    @SuppressWarnings("unchecked")
059    private LastReqGrammar()
060    {
061        setName( LastReqGrammar.class.getName() );
062
063        // Create the transitions table
064        super.transitions = new GrammarTransition[LastReqStatesEnum.LAST_LAST_REQ_STATE.ordinal()][256];
065
066        // ============================================================================================
067        // LastReq
068        // ============================================================================================
069        // --------------------------------------------------------------------------------------------
070        // Transition from LastReq init to LastReq SEQ OF
071        // --------------------------------------------------------------------------------------------
072        // LastReq   ::= SEQUENCE OF
073        super.transitions[LastReqStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
074            new GrammarTransition<LastReqContainer>(
075                LastReqStatesEnum.START_STATE,
076                LastReqStatesEnum.LAST_REQ_SEQ_STATE,
077                UniversalTag.SEQUENCE,
078                new LastReqInit() );
079
080        // --------------------------------------------------------------------------------------------
081        // Transition from LastReq SEQ OF to LastReq SEQ OF SEQ
082        // --------------------------------------------------------------------------------------------
083        // LastReq   ::= SEQUENCE OF SEQUENCE {
084        super.transitions[LastReqStatesEnum.LAST_REQ_SEQ_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
085            new GrammarTransition<LastReqContainer>(
086                LastReqStatesEnum.LAST_REQ_SEQ_STATE,
087                LastReqStatesEnum.LAST_REQ_SEQ_SEQ_STATE,
088                UniversalTag.SEQUENCE,
089                new CheckNotNullLength<LastReqContainer>() );
090
091        // --------------------------------------------------------------------------------------------
092        // Transition from LastReq SEQ OF SEQ to lr-type tag
093        // --------------------------------------------------------------------------------------------
094        // LastReq   ::= SEQUENCE OF SEQUENCE {
095        //         lr-type         [0]
096        super.transitions[LastReqStatesEnum.LAST_REQ_SEQ_SEQ_STATE.ordinal()][KerberosConstants.LAST_REQ_LR_TYPE_TAG] =
097            new GrammarTransition<LastReqContainer>(
098                LastReqStatesEnum.LAST_REQ_SEQ_SEQ_STATE,
099                LastReqStatesEnum.LAST_REQ_LR_TYPE_TAG_STATE,
100                KerberosConstants.LAST_REQ_LR_TYPE_TAG,
101                new CheckNotNullLength<LastReqContainer>() );
102
103        // --------------------------------------------------------------------------------------------
104        // Transition from lr-type tag to lr-type value
105        // --------------------------------------------------------------------------------------------
106        // LastReq   ::= SEQUENCE OF SEQUENCE {
107        //         lr-type         [0]  Int32,
108        super.transitions[LastReqStatesEnum.LAST_REQ_LR_TYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
109            new GrammarTransition<LastReqContainer>(
110                LastReqStatesEnum.LAST_REQ_LR_TYPE_TAG_STATE,
111                LastReqStatesEnum.LAST_REQ_LR_TYPE_STATE,
112                UniversalTag.INTEGER,
113                new StoreLrType() );
114
115        // --------------------------------------------------------------------------------------------
116        // Transition from lr-type value lr-value tag
117        // --------------------------------------------------------------------------------------------
118        // LastReq   ::= SEQUENCE OF SEQUENCE {
119        //         ...
120        //         lr-value        [1]
121        super.transitions[LastReqStatesEnum.LAST_REQ_LR_TYPE_STATE.ordinal()][KerberosConstants.LAST_REQ_LR_VALUE_TAG] =
122            new GrammarTransition<LastReqContainer>(
123                LastReqStatesEnum.LAST_REQ_LR_TYPE_STATE,
124                LastReqStatesEnum.LAST_REQ_LR_VALUE_TAG_STATE,
125                KerberosConstants.LAST_REQ_LR_VALUE_TAG,
126                new CheckNotNullLength<LastReqContainer>() );
127
128        // --------------------------------------------------------------------------------------------
129        // Transition from lr-value tag to lr-value value
130        // --------------------------------------------------------------------------------------------
131        // LastReq   ::= SEQUENCE OF SEQUENCE {
132        //         ...
133        //         lr-value        [1] KerberosTime
134        super.transitions[LastReqStatesEnum.LAST_REQ_LR_VALUE_TAG_STATE.ordinal()][UniversalTag.GENERALIZED_TIME
135            .getValue()] =
136            new GrammarTransition<LastReqContainer>(
137                LastReqStatesEnum.LAST_REQ_LR_VALUE_TAG_STATE,
138                LastReqStatesEnum.LAST_REQ_LR_VALUE_STATE,
139                UniversalTag.GENERALIZED_TIME,
140                new StoreLrValue() );
141
142        // --------------------------------------------------------------------------------------------
143        // Transition from lr-value value to SEQ OF SEQ
144        // --------------------------------------------------------------------------------------------
145        // LastReq   ::= SEQUENCE OF SEQUENCE {
146        //         ...
147        //         lr-value        [1] KerberosTime
148        // }
149        super.transitions[LastReqStatesEnum.LAST_REQ_LR_VALUE_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
150            new GrammarTransition<LastReqContainer>(
151                LastReqStatesEnum.LAST_REQ_LR_VALUE_STATE,
152                LastReqStatesEnum.LAST_REQ_SEQ_SEQ_STATE,
153                UniversalTag.SEQUENCE,
154                new CheckNotNullLength<LastReqContainer>() );
155    }
156
157
158    /**
159     * Get the instance of this grammar
160     *
161     * @return An instance on the LastReq Grammar
162     */
163    public static Grammar<LastReqContainer> getInstance()
164    {
165        return instance;
166    }
167}