001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify;
021
022
023import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
024import org.apache.directory.api.asn1.ber.grammar.Grammar;
025import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
026import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.util.Strings;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * This class implements the PasswordModify extended operation's ASN.1 grammer. 
037 * All the actions are declared in this class. As it is a singleton, 
038 * these declaration are only done once. The grammar is :
039 * 
040 * <pre>
041 *  PasswdModifyRequestValue ::= SEQUENCE {
042 *    userIdentity    [0]  OCTET STRING OPTIONAL
043 *    oldPasswd       [1]  OCTET STRING OPTIONAL
044 *    newPasswd       [2]  OCTET STRING OPTIONAL }
045 * </pre>
046 * 
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049
050public class PasswordModifyRequestGrammar extends AbstractGrammar<PasswordModifyRequestContainer>
051{
052
053    /** logger */
054    private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyRequestGrammar.class );
055
056    /** The instance of grammar. PasswdModifyRequestGrammar is a singleton */
057    private static Grammar<PasswordModifyRequestContainer> instance = new PasswordModifyRequestGrammar();
058
059
060    /**
061     * Creates a new PasswordModifyRequestGrammar object.
062     */
063    @SuppressWarnings("unchecked")
064    public PasswordModifyRequestGrammar()
065    {
066        setName( PasswordModifyRequestGrammar.class.getName() );
067
068        // Create the transitions table
069        super.transitions = new GrammarTransition[PasswordModifyRequestStatesEnum.LAST_PASSWORD_MODIFY_REQUEST_STATE
070            .ordinal()][256];
071
072        /**
073         * Transition from init state to PasswordModify Request Value
074         * 
075         * PasswdModifyRequestValue ::= SEQUENCE {
076         *     ...
077         *     
078         * Creates the PasswdModifyRequest object
079         */
080        super.transitions[PasswordModifyRequestStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
081            new GrammarTransition<PasswordModifyRequestContainer>(
082                PasswordModifyRequestStatesEnum.START_STATE,
083                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
084                UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyRequestContainer>(
085                    "Init PasswordModifyRequest" )
086                {
087                    public void action( PasswordModifyRequestContainer container )
088                    {
089                        // We may have nothing left
090                        if ( container.getCurrentTLV().getLength() == 0 )
091                        {
092                            container.setGrammarEndAllowed( true );
093                        }
094                    }
095                } );
096
097        /**
098         * Transition from PasswordModify Request Value to userIdentity
099         *
100         * PasswdModifyRequestValue ::= SEQUENCE {
101         *     userIdentity    [0]  OCTET STRING OPTIONAL
102         *     ...
103         *     
104         * Set the userIdentity into the PasswdModifyRequest instance.
105         */
106        super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.USER_IDENTITY_TAG] =
107            new GrammarTransition<PasswordModifyRequestContainer>(
108                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
109                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
110                PasswordModifyRequestConstants.USER_IDENTITY_TAG,
111                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest user identity" )
112                {
113                    public void action( PasswordModifyRequestContainer container )
114                    {
115                        BerValue value = container.getCurrentTLV().getValue();
116
117                        byte[] userIdentity = value.getData();
118
119                        if ( LOG.isDebugEnabled() )
120                        {
121                            LOG.debug( I18n.msg( I18n.MSG_08217_USER_IDENTITY, Strings.dumpBytes( userIdentity ) ) );
122                        }
123
124                        if ( userIdentity == null )
125                        {
126                            userIdentity = Strings.EMPTY_BYTES;
127                        }
128
129                        container.getPwdModifyRequest().setUserIdentity( userIdentity );
130
131                        // We may have nothing left
132                        container.setGrammarEndAllowed( true );
133                    }
134                } );
135
136        /**
137         * Transition from userIdentity to oldPassword
138         *
139         * PasswdModifyRequestValue ::= SEQUENCE {
140         *     userIdentity    [0]  OCTET STRING OPTIONAL
141         *     oldPassword     [1]  OCTET STRING OPTIONAL
142         *     ...
143         *     
144         * Set the oldPassword into the PasswdModifyRequest instance.
145         */
146        super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.OLD_PASSWORD_TAG] =
147            new GrammarTransition<PasswordModifyRequestContainer>(
148                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
149                PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
150                PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
151                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest oldPassword" )
152                {
153                    public void action( PasswordModifyRequestContainer container )
154                    {
155                        BerValue value = container.getCurrentTLV().getValue();
156
157                        byte[] oldPassword = value.getData();
158
159                        if ( LOG.isDebugEnabled() )
160                        {
161                            LOG.debug( I18n.msg( I18n.MSG_08209_OLD_PASSWORD ) );
162                        }
163
164                        if ( oldPassword == null )
165                        {
166                            oldPassword = Strings.EMPTY_BYTES;
167                        }
168
169                        container.getPwdModifyRequest().setOldPassword( oldPassword );
170
171                        // We may have nothing left
172                        container.setGrammarEndAllowed( true );
173                    }
174                } );
175
176        /**
177         * Transition from userIdentity to newPassword
178         *
179         * PasswdModifyRequestValue ::= SEQUENCE {
180         *     userIdentity    [0]  OCTET STRING OPTIONAL
181         *     ...
182         *     newPassword     [2]  OCTET STRING OPTIONAL
183         * 
184         *     
185         * Set the newPassword into the PasswdModifyRequest instance.
186         */
187        super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
188            new GrammarTransition<PasswordModifyRequestContainer>(
189                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
190                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
191                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
192                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
193                {
194                    public void action( PasswordModifyRequestContainer container )
195                    {
196                        BerValue value = container.getCurrentTLV().getValue();
197
198                        byte[] newPassword = value.getData();
199
200                        if ( LOG.isDebugEnabled() )
201                        {
202                            LOG.debug( I18n.msg( I18n.MSG_08208_NEW_PASSWORD ) );
203                        }
204
205                        if ( newPassword == null )
206                        {
207                            newPassword = Strings.EMPTY_BYTES;
208                        }
209
210                        container.getPwdModifyRequest().setNewPassword( newPassword );
211
212                        // We may have nothing left
213                        container.setGrammarEndAllowed( true );
214                    }
215                } );
216
217        /**
218         * Transition from PasswordModify Request Value to oldPassword
219         *
220         * PasswdModifyRequestValue ::= SEQUENCE {
221         *     ...
222         *     oldPassword    [1]  OCTET STRING OPTIONAL
223         *     ...
224         *     
225         * Set the oldPassword into the PasswdModifyRequest instance.
226         */
227        super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.OLD_PASSWORD_TAG] =
228            new GrammarTransition<PasswordModifyRequestContainer>(
229                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
230                PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
231                PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
232                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest oldPassword" )
233                {
234                    public void action( PasswordModifyRequestContainer container )
235                    {
236                        BerValue value = container.getCurrentTLV().getValue();
237
238                        byte[] oldPassword = value.getData();
239
240                        if ( LOG.isDebugEnabled() )
241                        {
242                            LOG.debug( I18n.msg( I18n.MSG_08209_OLD_PASSWORD ) );
243                        }
244
245                        if ( oldPassword == null )
246                        {
247                            oldPassword = Strings.EMPTY_BYTES;
248                        }
249
250                        container.getPwdModifyRequest().setOldPassword( oldPassword );
251
252                        // We may have nothing left
253                        container.setGrammarEndAllowed( true );
254                    }
255                } );
256
257        /**
258         * Transition from PasswordModify Request Value to newPassword
259         *
260         * PasswdModifyRequestValue ::= SEQUENCE {
261         *     ...
262         *     newPassword    [2]  OCTET STRING OPTIONAL
263         * }
264         *     
265         * Set the newPassword into the PasswdModifyRequest instance.
266         */
267        super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
268            new GrammarTransition<PasswordModifyRequestContainer>(
269                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
270                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
271                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
272                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
273                {
274                    public void action( PasswordModifyRequestContainer container )
275                    {
276                        BerValue value = container.getCurrentTLV().getValue();
277
278                        byte[] newPassword = value.getData();
279
280                        if ( LOG.isDebugEnabled() )
281                        {
282                            LOG.debug( I18n.msg( I18n.MSG_08208_NEW_PASSWORD ) );
283                        }
284
285                        if ( newPassword == null )
286                        {
287                            newPassword = Strings.EMPTY_BYTES;
288                        }
289
290                        container.getPwdModifyRequest().setNewPassword( newPassword );
291
292                        // We may have nothing left
293                        container.setGrammarEndAllowed( true );
294                    }
295                } );
296
297        /**
298         * Transition from oldPassword to newPassword
299         *
300         *     ...
301         *     oldPassword    [1]  OCTET STRING OPTIONAL
302         *     newPassword    [2]  OCTET STRING OPTIONAL
303         * }
304         *     
305         * Set the newPassword into the PasswdModifyRequest instance.
306         */
307        super.transitions[PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
308            new GrammarTransition<PasswordModifyRequestContainer>(
309                PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
310                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
311                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
312                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
313                {
314                    public void action( PasswordModifyRequestContainer container )
315                    {
316                        BerValue value = container.getCurrentTLV().getValue();
317
318                        byte[] newPassword = value.getData();
319
320                        if ( LOG.isDebugEnabled() )
321                        {
322                            LOG.debug( I18n.msg( I18n.MSG_08208_NEW_PASSWORD ) );
323                        }
324
325                        if ( newPassword == null )
326                        {
327                            newPassword = Strings.EMPTY_BYTES;
328                        }
329
330                        container.getPwdModifyRequest().setNewPassword( newPassword );
331
332                        // We may have nothing left
333                        container.setGrammarEndAllowed( true );
334                    }
335                } );
336    }
337
338
339    /**
340     * This class is a singleton.
341     * 
342     * @return An instance on this grammar
343     */
344    public static Grammar<PasswordModifyRequestContainer> getInstance()
345    {
346        return instance;
347    }
348}