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      /**
66       * Creates a new PasswordModifyRequestGrammar object.
67       */
68      @SuppressWarnings("unchecked")
69      public PasswordModifyRequestGrammar()
70      {
71          setName( PasswordModifyRequestGrammar.class.getName() );
72  
73          // Create the transitions table
74          super.transitions = new GrammarTransition[PasswordModifyRequestStatesEnum.LAST_PASSWORD_MODIFY_REQUEST_STATE
75              .ordinal()][256];
76  
77          /**
78           * Transition from init state to PasswordModify Request Value
79           * 
80           * PasswdModifyRequestValue ::= SEQUENCE {
81           *     ...
82           *     
83           * Creates the PasswdModifyRequest object
84           */
85          super.transitions[PasswordModifyRequestStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
86              new GrammarTransition<PasswordModifyRequestContainer>(
87                  PasswordModifyRequestStatesEnum.START_STATE,
88                  PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
89                  UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyRequestContainer>(
90                      "Init PasswordModifyRequest" )
91                  {
92                      public void action( PasswordModifyRequestContainer container )
93                      {
94                          PasswordModifyRequestDecorator passwordModifyRequestDecorator = new PasswordModifyRequestDecorator(
95                              LdapApiServiceFactory.getSingleton(), new PasswordModifyRequestImpl() );
96                          container.setPasswordModifyRequest( passwordModifyRequestDecorator );
97  
98                          // We may have nothing left
99                          container.setGrammarEndAllowed( true );
100                     }
101                 } );
102 
103         /**
104          * Transition from PasswordModify Request Value to userIdentity
105          *
106          * PasswdModifyRequestValue ::= SEQUENCE {
107          *     userIdentity    [0]  OCTET STRING OPTIONAL
108          *     ...
109          *     
110          * Set the userIdentity into the PasswdModifyRequest instance.
111          */
112         super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.USER_IDENTITY_TAG] =
113             new GrammarTransition<PasswordModifyRequestContainer>(
114                 PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
115                 PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
116                 PasswordModifyRequestConstants.USER_IDENTITY_TAG,
117                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest user identity" )
118                 {
119                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
120                     {
121                         BerValue value = container.getCurrentTLV().getValue();
122 
123                         byte[] userIdentity = value.getData();
124 
125                         if ( IS_DEBUG )
126                         {
127                             LOG.debug( "UserIdentity = " + Strings.dumpBytes( userIdentity ) );
128                         }
129 
130                         if ( userIdentity == null )
131                         {
132                             userIdentity = Strings.EMPTY_BYTES;
133                         }
134 
135                         container.getPwdModifyRequest().setUserIdentity( userIdentity );
136 
137                         // We may have nothing left
138                         container.setGrammarEndAllowed( true );
139                     }
140                 } );
141 
142         /**
143          * Transition from userIdentity to oldPassword
144          *
145          * PasswdModifyRequestValue ::= SEQUENCE {
146          *     userIdentity    [0]  OCTET STRING OPTIONAL
147          *     oldPassword     [1]  OCTET STRING OPTIONAL
148          *     ...
149          *     
150          * Set the oldPassword into the PasswdModifyRequest instance.
151          */
152         super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.OLD_PASSWORD_TAG] =
153             new GrammarTransition<PasswordModifyRequestContainer>(
154                 PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
155                 PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
156                 PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
157                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest oldPassword" )
158                 {
159                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
160                     {
161                         BerValue value = container.getCurrentTLV().getValue();
162 
163                         byte[] oldPassword = value.getData();
164 
165                         if ( IS_DEBUG )
166                         {
167                             LOG.debug( "oldPassword = " + Strings.dumpBytes( oldPassword ) );
168                         }
169 
170                         if ( oldPassword == null )
171                         {
172                             oldPassword = Strings.EMPTY_BYTES;
173                         }
174 
175                         container.getPwdModifyRequest().setOldPassword( oldPassword );
176 
177                         // We may have nothing left
178                         container.setGrammarEndAllowed( true );
179                     }
180                 } );
181 
182         /**
183          * Transition from userIdentity to newPassword
184          *
185          * PasswdModifyRequestValue ::= SEQUENCE {
186          *     userIdentity    [0]  OCTET STRING OPTIONAL
187          *     ...
188          *     newPassword     [2]  OCTET STRING OPTIONAL
189          * 
190          *     
191          * Set the newPassword into the PasswdModifyRequest instance.
192          */
193         super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
194             new GrammarTransition<PasswordModifyRequestContainer>(
195                 PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
196                 PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
197                 PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
198                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
199                 {
200                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
201                     {
202                         BerValue value = container.getCurrentTLV().getValue();
203 
204                         byte[] newPassword = value.getData();
205 
206                         if ( IS_DEBUG )
207                         {
208                             LOG.debug( "newPassword = " + Strings.dumpBytes( newPassword ) );
209                         }
210 
211                         if ( newPassword == null )
212                         {
213                             newPassword = Strings.EMPTY_BYTES;
214                         }
215 
216                         container.getPwdModifyRequest().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                         container.getPwdModifyRequest().setOldPassword( oldPassword );
257 
258                         // We may have nothing left
259                         container.setGrammarEndAllowed( true );
260                     }
261                 } );
262 
263         /**
264          * Transition from PasswordModify Request Value to newPassword
265          *
266          * PasswdModifyRequestValue ::= SEQUENCE {
267          *     ...
268          *     newPassword    [2]  OCTET STRING OPTIONAL
269          * }
270          *     
271          * Set the newPassword into the PasswdModifyRequest instance.
272          */
273         super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
274             new GrammarTransition<PasswordModifyRequestContainer>(
275                 PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
276                 PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
277                 PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
278                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
279                 {
280                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
281                     {
282                         BerValue value = container.getCurrentTLV().getValue();
283 
284                         byte[] newPassword = value.getData();
285 
286                         if ( IS_DEBUG )
287                         {
288                             LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
289                         }
290 
291                         if ( newPassword == null )
292                         {
293                             newPassword = Strings.EMPTY_BYTES;
294                         }
295 
296                         container.getPwdModifyRequest().setNewPassword( newPassword );
297 
298                         // We may have nothing left
299                         container.setGrammarEndAllowed( true );
300                     }
301                 } );
302 
303         /**
304          * Transition from oldPassword to newPassword
305          *
306          *     ...
307          *     oldPassword    [1]  OCTET STRING OPTIONAL
308          *     newPassword    [2]  OCTET STRING OPTIONAL
309          * }
310          *     
311          * Set the newPassword into the PasswdModifyRequest instance.
312          */
313         super.transitions[PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
314             new GrammarTransition<PasswordModifyRequestContainer>(
315                 PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
316                 PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
317                 PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
318                 new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
319                 {
320                     public void action( PasswordModifyRequestContainer container ) throws DecoderException
321                     {
322                         BerValue value = container.getCurrentTLV().getValue();
323 
324                         byte[] newPassword = value.getData();
325 
326                         if ( IS_DEBUG )
327                         {
328                             LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
329                         }
330 
331                         if ( newPassword == null )
332                         {
333                             newPassword = Strings.EMPTY_BYTES;
334                         }
335 
336                         container.getPwdModifyRequest().setNewPassword( newPassword );
337 
338                         // We may have nothing left
339                         container.setGrammarEndAllowed( true );
340                     }
341                 } );
342     }
343 
344 
345     /**
346      * This class is a singleton.
347      * 
348      * @return An instance on this grammar
349      */
350     public static Grammar<PasswordModifyRequestContainer> getInstance()
351     {
352         return instance;
353     }
354 }