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.PasswordModifyResponseImpl;
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 PasswordModifyResponse 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   *  PasswdModifyResponseValue ::= SEQUENCE {
44   *      genPasswd       [0]     OCTET STRING OPTIONAL }
45   * </pre>
46   * 
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  
50  public class PasswordModifyResponseGrammar extends AbstractGrammar<PasswordModifyResponseContainer>
51  {
52  
53      /** logger */
54      private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyResponseGrammar.class );
55  
56      /** Speedup for logs */
57      static final boolean IS_DEBUG = LOG.isDebugEnabled();
58  
59      /** The instance of grammar. PasswdModifyResponseGrammar is a singleton */
60      private static Grammar<PasswordModifyResponseContainer> instance = new PasswordModifyResponseGrammar();
61  
62  
63      /**
64       * Creates a new PasswordModifyResponseGrammar object.
65       */
66      @SuppressWarnings("unchecked")
67      public PasswordModifyResponseGrammar()
68      {
69          setName( PasswordModifyResponseGrammar.class.getName() );
70  
71          // Create the transitions table
72          super.transitions = new GrammarTransition[PasswordModifyResponseStatesEnum.LAST_PASSWORD_MODIFY_RESPONSE_STATE
73              .ordinal()][256];
74  
75          /**
76           * Transition from init state to PasswordModify Response Value
77           * 
78           * PasswdModifyResponseValue ::= SEQUENCE {
79           *     ...
80           *     
81           * Creates the PasswdModifyResponse object
82           */
83          super.transitions[PasswordModifyResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
84              new GrammarTransition<PasswordModifyResponseContainer>(
85                  PasswordModifyResponseStatesEnum.START_STATE,
86                  PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
87                  UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyResponseContainer>(
88                      "Init PasswordModifyResponse" )
89                  {
90                      public void action( PasswordModifyResponseContainer container )
91                      {
92                          PasswordModifyResponseDecorator passwordModifyResponse = new PasswordModifyResponseDecorator(
93                              LdapApiServiceFactory.getSingleton(), new PasswordModifyResponseImpl() );
94                          container.setPasswordModifyResponse( passwordModifyResponse );
95  
96                          // We may have nothing left
97                          container.setGrammarEndAllowed( true );
98                      }
99                  } );
100 
101         /**
102          * Transition from PasswordModify Response Value to genPassword
103          *
104          * PasswdModifyResponseValue ::= SEQUENCE {
105          *     genPassword    [0]  OCTET STRING OPTIONAL
106          *     ...
107          *     
108          * Set the userIdentity into the PasswdModifyResponset instance.
109          */
110         super.transitions[PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE.ordinal()][PasswordModifyResponseConstants.GEN_PASSWORD_TAG] =
111             new GrammarTransition<PasswordModifyResponseContainer>(
112                 PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
113                 PasswordModifyResponseStatesEnum.GEN_PASSWORD_STATE,
114                 PasswordModifyResponseConstants.GEN_PASSWORD_TAG,
115                 new GrammarAction<PasswordModifyResponseContainer>( "Set PasswordModifyResponse user identity" )
116                 {
117                     public void action( PasswordModifyResponseContainer container ) throws DecoderException
118                     {
119                         BerValue value = container.getCurrentTLV().getValue();
120 
121                         byte[] genPassword = value.getData();
122 
123                         if ( IS_DEBUG )
124                         {
125                             LOG.debug( "GenPassword = " + Strings.dumpBytes( genPassword ) );
126                         }
127 
128                         if ( genPassword == null )
129                         {
130                             genPassword = Strings.EMPTY_BYTES;
131                         }
132 
133                         container.getPwdModifyResponse().setGenPassword( genPassword );
134 
135                         // We may have nothing left
136                         container.setGrammarEndAllowed( true );
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<PasswordModifyResponseContainer> getInstance()
148     {
149         return instance;
150     }
151 }