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.extras.extended.ads_impl.cancel;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
25  import org.apache.directory.api.asn1.ber.grammar.Grammar;
26  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
27  import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
28  import org.apache.directory.api.asn1.ber.tlv.BerValue;
29  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
30  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
31  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32  import org.apache.directory.api.i18n.I18n;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * This class implements the Cancel operation. All the actions are declared
40   * in this class. As it is a singleton, these declaration are only done once.
41   * The grammar is :
42   * 
43   * <pre>
44   *  cancelRequestValue ::= SEQUENCE {
45   *      cancelId     MessageID 
46   *                   -- MessageID is as defined in [RFC2251]
47   * }
48   * </pre>
49   * 
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   */
52  public final class CancelRequestGrammar extends AbstractGrammar<CancelRequestContainer>
53  {
54      /** The logger */
55      static final Logger LOG = LoggerFactory.getLogger( CancelRequestGrammar.class );
56  
57      /** The instance of grammar. CancelGrammar is a singleton */
58      private static Grammar<CancelRequestContainer> instance = new CancelRequestGrammar();
59  
60  
61      /**
62       * Creates a new GracefulDisconnectGrammar object.
63       */
64      @SuppressWarnings("unchecked")
65      private CancelRequestGrammar()
66      {
67          setName( CancelRequestGrammar.class.getName() );
68  
69          // Create the transitions table
70          super.transitions = new GrammarTransition[CancelStatesEnum.LAST_CANCEL_STATE.ordinal()][256];
71  
72          /**
73           * Transition from init state to cancel sequence
74           * cancelRequestValue ::= SEQUENCE {
75           *     ... 
76           * 
77           * Creates the Cancel object
78           */
79          super.transitions[CancelStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
80              new GrammarTransition<CancelRequestContainer>( CancelStatesEnum.START_STATE,
81                  CancelStatesEnum.CANCEL_SEQUENCE_STATE,
82                  UniversalTag.SEQUENCE.getValue(),
83                  null );
84  
85          /**
86           * Transition from cancel SEQ to cancelId
87           * 
88           * cancelRequestValue ::= SEQUENCE {
89           *     cancelId   MessageID 
90           * }
91           *     
92           * Set the cancelId value into the Cancel object.    
93           */
94          super.transitions[CancelStatesEnum.CANCEL_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
95              new GrammarTransition<CancelRequestContainer>( CancelStatesEnum.CANCEL_SEQUENCE_STATE,
96                  CancelStatesEnum.CANCEL_ID_STATE,
97                  UniversalTag.INTEGER.getValue(),
98                  new GrammarAction<CancelRequestContainer>( "Stores CancelId" )
99                  {
100                     public void action( CancelRequestContainer cancelContainer ) throws DecoderException
101                     {
102                         BerValue value = cancelContainer.getCurrentTLV().getValue();
103 
104                         try
105                         {
106                             int cancelId = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE );
107 
108                             if ( LOG.isDebugEnabled() )
109                             {
110                                 LOG.debug( I18n.msg( I18n.MSG_08200_CANCEL_ID, cancelId ) );
111                             }
112 
113                             cancelContainer.getCancelRequest().setCancelId( cancelId );
114                             
115                             cancelContainer.setGrammarEndAllowed( true );
116                         }
117                         catch ( IntegerDecoderException ide )
118                         {
119                             String msg = I18n.err( I18n.ERR_08200_CANCELID_DECODING_FAILED, Strings.dumpBytes( value.getData() ) );
120                             LOG.error( msg );
121                             throw new DecoderException( msg, ide );
122                         }
123                     }
124                 } );
125     }
126 
127 
128     /**
129      * This class is a singleton.
130      * 
131      * @return An instance on this grammar
132      */
133     public static Grammar<CancelRequestContainer> getInstance()
134     {
135         return instance;
136     }
137 }