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.pwdModify;
21  
22  
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.UniversalTag;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.util.Strings;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * This class implements the PasswordModifyResponse extended operation's ASN.1 grammer. 
37   * All the actions are declared in this class. As it is a singleton, 
38   * these declaration are only done once. The grammar is :
39   * 
40   * <pre>
41   *  PasswdModifyResponseValue ::= SEQUENCE {
42   *      genPasswd       [0]     OCTET STRING OPTIONAL }
43   * </pre>
44   * 
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  
48  public class PasswordModifyResponseGrammar extends AbstractGrammar<PasswordModifyResponseContainer>
49  {
50  
51      /** logger */
52      private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyResponseGrammar.class );
53  
54      /** The instance of grammar. PasswdModifyResponseGrammar is a singleton */
55      private static Grammar<PasswordModifyResponseContainer> instance = new PasswordModifyResponseGrammar();
56  
57  
58      /**
59       * Creates a new PasswordModifyResponseGrammar object.
60       */
61      @SuppressWarnings("unchecked")
62      public PasswordModifyResponseGrammar()
63      {
64          setName( PasswordModifyResponseGrammar.class.getName() );
65  
66          // Create the transitions table
67          super.transitions = new GrammarTransition[PasswordModifyResponseStatesEnum.LAST_PASSWORD_MODIFY_RESPONSE_STATE
68              .ordinal()][256];
69  
70          /**
71           * Transition from init state to PasswordModify Response Value
72           * 
73           * PasswdModifyResponseValue ::= SEQUENCE {
74           *     ...
75           *     
76           * Creates the PasswdModifyResponse object
77           */
78          super.transitions[PasswordModifyResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
79              new GrammarTransition<PasswordModifyResponseContainer>(
80                  PasswordModifyResponseStatesEnum.START_STATE,
81                  PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
82                  UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyResponseContainer>(
83                      "Init PasswordModifyResponse" )
84                  {
85                      public void action( PasswordModifyResponseContainer container )
86                      {
87                          if ( container.getCurrentTLV().getLength() == 0 )
88                          { 
89                              // We may have nothing left
90                              container.setGrammarEndAllowed( true );
91                          }
92                      }
93                  } );
94  
95          /**
96           * Transition from PasswordModify Response Value to genPassword
97           *
98           * PasswdModifyResponseValue ::= SEQUENCE {
99           *     genPassword    [0]  OCTET STRING OPTIONAL
100          *     ...
101          *     
102          * Set the userIdentity into the PasswdModifyResponset instance.
103          */
104         super.transitions[PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE.ordinal()][PasswordModifyResponseConstants.GEN_PASSWORD_TAG] =
105             new GrammarTransition<PasswordModifyResponseContainer>(
106                 PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
107                 PasswordModifyResponseStatesEnum.GEN_PASSWORD_STATE,
108                 PasswordModifyResponseConstants.GEN_PASSWORD_TAG,
109                 new GrammarAction<PasswordModifyResponseContainer>( "Set PasswordModifyResponse user identity" )
110                 {
111                     public void action( PasswordModifyResponseContainer container )
112                     {
113                         BerValue value = container.getCurrentTLV().getValue();
114 
115                         byte[] genPassword = value.getData();
116 
117                         if ( LOG.isDebugEnabled() )
118                         {
119                             LOG.debug( I18n.msg( I18n.MSG_08205_GEN_PASSWORD ) );
120                         }
121 
122                         if ( genPassword == null )
123                         {
124                             genPassword = Strings.EMPTY_BYTES;
125                         }
126 
127                         container.getPwdModifyResponse().setGenPassword( genPassword );
128 
129                         // We may have nothing left
130                         container.setGrammarEndAllowed( true );
131                     }
132                 } );
133     }
134 
135 
136     /**
137      * This class is a singleton.
138      * 
139      * @return An instance on this grammar
140      */
141     public static Grammar<PasswordModifyResponseContainer> getInstance()
142     {
143         return instance;
144     }
145 }