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      @SuppressWarnings("unchecked")
64      public PasswordModifyResponseGrammar()
65      {
66          setName( PasswordModifyResponseGrammar.class.getName() );
67  
68          // Create the transitions table
69          super.transitions = new GrammarTransition[PasswordModifyResponseStatesEnum.LAST_PASSWORD_MODIFY_RESPONSE_STATE
70              .ordinal()][256];
71  
72          /**
73           * Transition from init state to PasswordModify Response Value
74           * 
75           * PasswdModifyResponseValue ::= SEQUENCE {
76           *     ...
77           *     
78           * Creates the PasswdModifyResponse object
79           */
80          super.transitions[PasswordModifyResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
81              new GrammarTransition<PasswordModifyResponseContainer>(
82                  PasswordModifyResponseStatesEnum.START_STATE,
83                  PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
84                  UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyResponseContainer>(
85                      "Init PasswordModifyResponse" )
86                  {
87                      public void action( PasswordModifyResponseContainer container )
88                      {
89                          PasswordModifyResponseDecorator passwordModifyResponse = new PasswordModifyResponseDecorator(
90                              LdapApiServiceFactory.getSingleton(), new PasswordModifyResponseImpl() );
91                          container.setPasswordModifyResponse( passwordModifyResponse );
92  
93                          // We may have nothing left
94                          container.setGrammarEndAllowed( true );
95                      }
96                  } );
97  
98          /**
99           * Transition from PasswordModify Response Value to genPassword
100          *
101          * PasswdModifyResponseValue ::= SEQUENCE {
102          *     genPassword    [0]  OCTET STRING OPTIONAL
103          *     ...
104          *     
105          * Set the userIdentity into the PasswdModifyResponset instance.
106          */
107         super.transitions[PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE.ordinal()][PasswordModifyResponseConstants.GEN_PASSWORD_TAG] =
108             new GrammarTransition<PasswordModifyResponseContainer>(
109                 PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
110                 PasswordModifyResponseStatesEnum.GEN_PASSWORD_STATE,
111                 PasswordModifyResponseConstants.GEN_PASSWORD_TAG,
112                 new GrammarAction<PasswordModifyResponseContainer>( "Set PasswordModifyResponse user identity" )
113                 {
114                     public void action( PasswordModifyResponseContainer container ) throws DecoderException
115                     {
116                         BerValue value = container.getCurrentTLV().getValue();
117 
118                         byte[] genPassword = value.getData();
119 
120                         if ( IS_DEBUG )
121                         {
122                             LOG.debug( "GenPassword = " + Strings.dumpBytes( genPassword ) );
123                         }
124 
125                         if ( genPassword == null )
126                         {
127                             genPassword = Strings.EMPTY_BYTES;
128                         }
129 
130                         ( ( PasswordModifyResponseDecorator ) container.getPwdModifyResponse() )
131                             .setGenPassword( genPassword );
132 
133                         // We may have nothing left
134                         container.setGrammarEndAllowed( true );
135                     }
136                 } );
137     }
138 
139 
140     /**
141      * This class is a singleton.
142      * 
143      * @return An instance on this grammar
144      */
145     public static Grammar<PasswordModifyResponseContainer> getInstance()
146     {
147         return instance;
148     }
149 }