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.pwdModify;
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.UniversalTag;
30  import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
31  import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequestImpl;
32  import org.apache.directory.api.util.Strings;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * This class implements the PasswordModify extended operation's ASN.1 grammer. 
39   * All the actions are declared in this class. As it is a singleton, 
40   * these declaration are only done once. The grammar is :
41   * 
42   * <pre>
43   *  PasswdModifyRequestValue ::= SEQUENCE {
44   *    userIdentity    [0]  OCTET STRING OPTIONAL
45   *    oldPasswd       [1]  OCTET STRING OPTIONAL
46   *    newPasswd       [2]  OCTET STRING OPTIONAL }
47   * </pre>
48   * 
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  
52  public class PasswordModifyRequestGrammar extends AbstractGrammar<PasswordModifyRequestContainer>
53  {
54  
55      /** logger */
56      private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyRequestGrammar.class );
57  
58      /** Speedup for logs */
59      static final boolean IS_DEBUG = LOG.isDebugEnabled();
60  
61      /** The instance of grammar. PasswdModifyRequestGrammar is a singleton */
62      private static Grammar<PasswordModifyRequestContainer> instance = new PasswordModifyRequestGrammar();
63  
64  
65      @SuppressWarnings("unchecked")
66      public PasswordModifyRequestGrammar()
67      {
68          setName( PasswordModifyRequestGrammar.class.getName() );
69  
70          // Create the transitions table
71          super.transitions = new GrammarTransition[PasswordModifyRequestStatesEnum.LAST_PASSWORD_MODIFY_REQUEST_STATE
72              .ordinal()][256];
73  
74          /**
75           * Transition from init state to PasswordModify Request Value
76           * 
77           * PasswdModifyRequestValue ::= SEQUENCE {
78           *     ...
79           *     
80           * Creates the PasswdModifyRequest object
81           */
82          super.transitions[PasswordModifyRequestStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
83              new GrammarTransition<PasswordModifyRequestContainer>(
84                  PasswordModifyRequestStatesEnum.START_STATE,
85                  PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
86                  UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyRequestContainer>(
87                      "Init PasswordModifyRequest" )
88                  {
89                      public void action( PasswordModifyRequestContainer container )
90                      {
91                          PasswordModifyRequestDecorator passwordModifyRequestDecorator = new PasswordModifyRequestDecorator(
92                              LdapApiServiceFactory.getSingleton(), new PasswordModifyRequestImpl() );
93                          container.setPasswordModifyRequest( passwordModifyRequestDecorator );
94  
95                          // We may have nothing left
96                          container.setGrammarEndAllowed( true );
97                      }
98                  } );
99  
100         /**
101          * Transition from PasswordModify Request Value to userIdentity
102          *
103          * PasswdModifyRequestValue ::= SEQUENCE {
104          *     userIdentity    [0]  OCTET STRING OPTIONAL
105          *     ...
106          *     
107          * Set the userIdentity into the PasswdModifyRequest instance.
108          */
109         super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.USER_IDENTITY_TAG] =
110             new GrammarTransition<PasswordModifyRequestContainer>(
111                 PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
112                 PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
113                 PasswordModifyRequestConstants.USER_IDENTITY_TAG,
114                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest user identity" )
115                 {
116                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
117                     {
118                         BerValue value = container.getCurrentTLV().getValue();
119 
120                         byte[] userIdentity = value.getData();
121 
122                         if ( IS_DEBUG )
123                         {
124                             LOG.debug( "UserIdentity = " + Strings.dumpBytes( userIdentity ) );
125                         }
126 
127                         if ( userIdentity == null )
128                         {
129                             userIdentity = Strings.EMPTY_BYTES;
130                         }
131 
132                         ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
133                             .setUserIdentity( userIdentity );
134 
135                         // We may have nothing left
136                         container.setGrammarEndAllowed( true );
137                     }
138                 } );
139 
140         /**
141          * Transition from userIdentity to oldPassword
142          *
143          * PasswdModifyRequestValue ::= SEQUENCE {
144          *     userIdentity    [0]  OCTET STRING OPTIONAL
145          *     oldPassword     [1]  OCTET STRING OPTIONAL
146          *     ...
147          *     
148          * Set the oldPassword into the PasswdModifyRequest instance.
149          */
150         super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.OLD_PASSWORD_TAG] =
151             new GrammarTransition<PasswordModifyRequestContainer>(
152                 PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
153                 PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
154                 PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
155                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest oldPassword" )
156                 {
157                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
158                     {
159                         BerValue value = container.getCurrentTLV().getValue();
160 
161                         byte[] oldPassword = value.getData();
162 
163                         if ( IS_DEBUG )
164                         {
165                             LOG.debug( "oldPassword = " + Strings.dumpBytes( oldPassword ) );
166                         }
167 
168                         if ( oldPassword == null )
169                         {
170                             oldPassword = Strings.EMPTY_BYTES;
171                         }
172 
173                         ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
174                             .setOldPassword( oldPassword );
175 
176                         // We may have nothing left
177                         container.setGrammarEndAllowed( true );
178                     }
179                 } );
180 
181         /**
182          * Transition from userIdentity to newPassword
183          *
184          * PasswdModifyRequestValue ::= SEQUENCE {
185          *     userIdentity    [0]  OCTET STRING OPTIONAL
186          *     ...
187          *     newPassword     [2]  OCTET STRING OPTIONAL
188          * 
189          *     
190          * Set the newPassword into the PasswdModifyRequest instance.
191          */
192         super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
193             new GrammarTransition<PasswordModifyRequestContainer>(
194                 PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
195                 PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
196                 PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
197                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
198                 {
199                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
200                     {
201                         BerValue value = container.getCurrentTLV().getValue();
202 
203                         byte[] newPassword = value.getData();
204 
205                         if ( IS_DEBUG )
206                         {
207                             LOG.debug( "newPassword = " + Strings.dumpBytes( newPassword ) );
208                         }
209 
210                         if ( newPassword == null )
211                         {
212                             newPassword = Strings.EMPTY_BYTES;
213                         }
214 
215                         ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
216                             .setNewPassword( newPassword );
217 
218                         // We may have nothing left
219                         container.setGrammarEndAllowed( true );
220                     }
221                 } );
222 
223         /**
224          * Transition from PasswordModify Request Value to oldPassword
225          *
226          * PasswdModifyRequestValue ::= SEQUENCE {
227          *     ...
228          *     oldPassword    [1]  OCTET STRING OPTIONAL
229          *     ...
230          *     
231          * Set the oldPassword into the PasswdModifyRequest instance.
232          */
233         super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.OLD_PASSWORD_TAG] =
234             new GrammarTransition<PasswordModifyRequestContainer>(
235                 PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
236                 PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
237                 PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
238                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest oldPassword" )
239                 {
240                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
241                     {
242                         BerValue value = container.getCurrentTLV().getValue();
243 
244                         byte[] oldPassword = value.getData();
245 
246                         if ( IS_DEBUG )
247                         {
248                             LOG.debug( "OldPassword = " + Strings.dumpBytes( oldPassword ) );
249                         }
250 
251                         if ( oldPassword == null )
252                         {
253                             oldPassword = Strings.EMPTY_BYTES;
254                         }
255 
256                         ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
257                             .setOldPassword( oldPassword );
258 
259                         // We may have nothing left
260                         container.setGrammarEndAllowed( true );
261                     }
262                 } );
263 
264         /**
265          * Transition from PasswordModify Request Value to newPassword
266          *
267          * PasswdModifyRequestValue ::= SEQUENCE {
268          *     ...
269          *     newPassword    [2]  OCTET STRING OPTIONAL
270          * }
271          *     
272          * Set the newPassword into the PasswdModifyRequest instance.
273          */
274         super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
275             new GrammarTransition<PasswordModifyRequestContainer>(
276                 PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
277                 PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
278                 PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
279                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
280                 {
281                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
282                     {
283                         BerValue value = container.getCurrentTLV().getValue();
284 
285                         byte[] newPassword = value.getData();
286 
287                         if ( IS_DEBUG )
288                         {
289                             LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
290                         }
291 
292                         if ( newPassword == null )
293                         {
294                             newPassword = Strings.EMPTY_BYTES;
295                         }
296 
297                         ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
298                             .setNewPassword( newPassword );
299 
300                         // We may have nothing left
301                         container.setGrammarEndAllowed( true );
302                     }
303                 } );
304 
305         /**
306          * Transition from oldPassword to newPassword
307          *
308          *     ...
309          *     oldPassword    [1]  OCTET STRING OPTIONAL
310          *     newPassword    [2]  OCTET STRING OPTIONAL
311          * }
312          *     
313          * Set the newPassword into the PasswdModifyRequest instance.
314          */
315         super.transitions[PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
316             new GrammarTransition<PasswordModifyRequestContainer>(
317                 PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
318                 PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
319                 PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
320                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
321                 {
322                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
323                     {
324                         BerValue value = container.getCurrentTLV().getValue();
325 
326                         byte[] newPassword = value.getData();
327 
328                         if ( IS_DEBUG )
329                         {
330                             LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
331                         }
332 
333                         if ( newPassword == null )
334                         {
335                             newPassword = Strings.EMPTY_BYTES;
336                         }
337 
338                         ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
339                             .setNewPassword( newPassword );
340 
341                         // We may have nothing left
342                         container.setGrammarEndAllowed( true );
343                     }
344                 } );
345     }
346 
347 
348     /**
349      * This class is a singleton.
350      * 
351      * @return An instance on this grammar
352      */
353     public static Grammar<PasswordModifyRequestContainer> getInstance()
354     {
355         return instance;
356     }
357 }