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