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.controls.ad_impl;
21  
22  import org.apache.directory.api.asn1.DecoderException;
23  import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
24  import org.apache.directory.api.asn1.ber.grammar.Grammar;
25  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
26  import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
29  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
30  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
31  import org.apache.directory.api.i18n.I18n;
32  import org.apache.directory.api.util.Strings;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   *
39   * Implementation of AdDirSync Request Control. All the actions are declared in
40   * this class. As it is a singleton, these declaration are only done once.
41   *
42   *  The decoded grammar is as follows :
43   *
44   *  <pre>
45   * realReplControlValue ::= SEQUENCE {
46   *     parentsFirst          integer
47   *     maxAttributeCount     integer
48   *     cookie                OCTET STRING
49   * }
50   * </pre>
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   */
54  public final class AdDirSyncRequestGrammar extends AbstractGrammar<AdDirSyncRequestContainer>
55  {
56  
57      /** the logger */
58      private static final Logger LOG = LoggerFactory.getLogger( AdDirSyncRequestGrammar.class );
59  
60      /** AdDirSyncRequestControlGrammar singleton instance */
61      private static final AdDirSyncRequestGrammar INSTANCE = new AdDirSyncRequestGrammar();
62  
63  
64      /**
65       *
66       * Creates a new instance of AdDirSyncRequestControlGrammar.
67       *
68       */
69      @SuppressWarnings("unchecked")
70      private AdDirSyncRequestGrammar()
71      {
72          setName( AdDirSyncRequestGrammar.class.getName() );
73  
74          super.transitions = new GrammarTransition[AdDirSyncRequestStatesEnum.LAST_AD_DIR_SYNC_REQUEST_STATE.ordinal()][256];
75  
76          /**
77           * Transition from initial state to AdDirSyncRequest sequence
78           * AdDirSync ::= SEQUENCE {
79           *     ...
80           *
81           * Initialize the adDirSyncRequest object
82           */
83          super.transitions[AdDirSyncRequestStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
84              new GrammarTransition<AdDirSyncRequestContainer>(
85              AdDirSyncRequestStatesEnum.START_STATE, AdDirSyncRequestStatesEnum.AD_DIR_SYNC_REQUEST_SEQUENCE_STATE,
86              UniversalTag.SEQUENCE.getValue(),
87              new GrammarAction<AdDirSyncRequestContainer>( "Initialization" )
88              {
89                  @Override
90                  public void action( AdDirSyncRequestContainer container ) throws DecoderException
91                  {
92                  }
93              } );
94  
95  
96          /**
97           * transition from start to parentsFirst
98           * realReplControlValue ::= SEQUENCE {
99           *     parentsFirst          integer
100          *    ....
101          * }
102          */
103         super.transitions[AdDirSyncRequestStatesEnum.AD_DIR_SYNC_REQUEST_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER
104             .getValue()] =
105             new GrammarTransition<AdDirSyncRequestContainer>( AdDirSyncRequestStatesEnum.AD_DIR_SYNC_REQUEST_SEQUENCE_STATE,
106                 AdDirSyncRequestStatesEnum.PARENTS_FIRST_STATE, UniversalTag.INTEGER.getValue(),
107                 new GrammarAction<AdDirSyncRequestContainer>( "Set AdDirSyncRequestControl parentsFirst" )
108                 {
109                     @Override
110                     public void action( AdDirSyncRequestContainer container ) throws DecoderException
111                     {
112                         BerValue value = container.getCurrentTLV().getValue();
113 
114                         try
115                         {
116                             int parentsFirst = IntegerDecoder.parse( value );
117 
118                             if ( LOG.isDebugEnabled() )
119                             {
120                                 LOG.debug( I18n.msg( I18n.MSG_08108_PARENTS_FIRST, parentsFirst ) );
121                             }
122 
123                             container.getAdDirSyncRequest().setParentsFirst( parentsFirst );
124                         }
125                         catch ( IntegerDecoderException ide )
126                         {
127                             String msg = I18n.err( I18n.ERR_08107_AD_DIR_SYNC_PARENTS_FIRST_DECODING_ERROR, ide.getMessage() );
128                             LOG.error( msg, ide );
129                             throw new DecoderException( msg, ide );
130                         }
131                     }
132                 } );
133 
134 
135         /**
136          * transition from flag to maxAttributeCount
137          * realReplControlValue ::= SEQUENCE {
138          *     parentsFirst          integer
139          *     maxAttributeCount     integer
140          *    ....
141          * }
142          */
143         super.transitions[AdDirSyncRequestStatesEnum.PARENTS_FIRST_STATE.ordinal()][UniversalTag.INTEGER
144             .getValue()] =
145             new GrammarTransition<AdDirSyncRequestContainer>( AdDirSyncRequestStatesEnum.PARENTS_FIRST_STATE,
146                 AdDirSyncRequestStatesEnum.MAX_ATTRIBUTE_COUNT_STATE, UniversalTag.INTEGER.getValue(),
147                 new GrammarAction<AdDirSyncRequestContainer>( "Set AdDirSyncRequestControl maxAttributeCount" )
148                 {
149                     @Override
150                     public void action( AdDirSyncRequestContainer container ) throws DecoderException
151                     {
152                         BerValue value = container.getCurrentTLV().getValue();
153 
154                         try
155                         {
156                             int maxAttributeCount = IntegerDecoder.parse( value );
157 
158                             if ( LOG.isDebugEnabled() )
159                             {
160                                 LOG.debug( I18n.msg( I18n.MSG_08109_MAX_ATTRIBUTE_COUNT, maxAttributeCount ) );
161                             }
162 
163                             container.getAdDirSyncRequest().setMaxAttributeCount( maxAttributeCount );
164                         }
165                         catch ( IntegerDecoderException ide )
166                         {
167                             String msg = I18n.err( I18n.ERR_08108_AD_DIR_SYNC_MAX_ATTRIBUTE_COUNT_DECODING_ERROR, ide.getMessage() );
168                             LOG.error( msg, ide );
169                             throw new DecoderException( msg, ide );
170                         }
171                     }
172                 } );
173 
174 
175         /**
176          * transition from maxAttributeCount to cookie
177          *     ...
178          *     maxAttributeCount         integer
179          *     cookie                  OCTET STRING
180          * }
181          */
182         super.transitions[AdDirSyncRequestStatesEnum.MAX_ATTRIBUTE_COUNT_STATE.ordinal()][UniversalTag.OCTET_STRING
183             .getValue()] =
184             new GrammarTransition<AdDirSyncRequestContainer>( AdDirSyncRequestStatesEnum.MAX_ATTRIBUTE_COUNT_STATE,
185                 AdDirSyncRequestStatesEnum.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(),
186                 new GrammarAction<AdDirSyncRequestContainer>( "Set AdDirSyncRequestControl cookie" )
187                 {
188                     @Override
189                     public void action( AdDirSyncRequestContainer container )
190                     {
191                         BerValue value = container.getCurrentTLV().getValue();
192 
193                         byte[] cookie = value.getData();
194 
195                         if ( LOG.isDebugEnabled() )
196                         {
197                             LOG.debug( I18n.msg( I18n.MSG_08000_COOKIE, Strings.dumpBytes( cookie ) ) );
198                         }
199 
200                         container.getAdDirSyncRequest().setCookie( cookie );
201 
202                         container.setGrammarEndAllowed( true );
203                     }
204                 } );
205     }
206 
207 
208     /**
209      * @return the singleton instance of the AdDirSyncControlGrammar
210      */
211     public static Grammar<AdDirSyncRequestContainer> getInstance()
212     {
213         return INSTANCE;
214     }
215 }