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 *    http://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.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
025import org.apache.directory.api.asn1.ber.grammar.Grammar;
026import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
027import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
028import org.apache.directory.api.asn1.ber.tlv.BerValue;
029import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
030import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
031import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequestImpl;
032import org.apache.directory.api.util.Strings;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * This class implements the PasswordModify extended operation's ASN.1 grammer. 
039 * All the actions are declared in this class. As it is a singleton, 
040 * these declaration are only done once. The grammar is :
041 * 
042 * <pre>
043 *  PasswdModifyRequestValue ::= SEQUENCE {
044 *    userIdentity    [0]  OCTET STRING OPTIONAL
045 *    oldPasswd       [1]  OCTET STRING OPTIONAL
046 *    newPasswd       [2]  OCTET STRING OPTIONAL }
047 * </pre>
048 * 
049 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
050 */
051
052public class PasswordModifyRequestGrammar extends AbstractGrammar<PasswordModifyRequestContainer>
053{
054
055    /** logger */
056    private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyRequestGrammar.class );
057
058    /** Speedup for logs */
059    static final boolean IS_DEBUG = LOG.isDebugEnabled();
060
061    /** The instance of grammar. PasswdModifyRequestGrammar is a singleton */
062    private static Grammar<PasswordModifyRequestContainer> instance = new PasswordModifyRequestGrammar();
063
064
065    @SuppressWarnings("unchecked")
066    public PasswordModifyRequestGrammar()
067    {
068        setName( PasswordModifyRequestGrammar.class.getName() );
069
070        // Create the transitions table
071        super.transitions = new GrammarTransition[PasswordModifyRequestStatesEnum.LAST_PASSWORD_MODIFY_REQUEST_STATE
072            .ordinal()][256];
073
074        /**
075         * Transition from init state to PasswordModify Request Value
076         * 
077         * PasswdModifyRequestValue ::= SEQUENCE {
078         *     ...
079         *     
080         * Creates the PasswdModifyRequest object
081         */
082        super.transitions[PasswordModifyRequestStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
083            new GrammarTransition<PasswordModifyRequestContainer>(
084                PasswordModifyRequestStatesEnum.START_STATE,
085                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
086                UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyRequestContainer>(
087                    "Init PasswordModifyRequest" )
088                {
089                    public void action( PasswordModifyRequestContainer container )
090                    {
091                        PasswordModifyRequestDecorator passwordModifyRequestDecorator = new PasswordModifyRequestDecorator(
092                            LdapApiServiceFactory.getSingleton(), new PasswordModifyRequestImpl() );
093                        container.setPasswordModifyRequest( passwordModifyRequestDecorator );
094
095                        // We may have nothing left
096                        container.setGrammarEndAllowed( true );
097                    }
098                } );
099
100        /**
101         * Transition from PasswordModify Request Value to userIdentity
102         *
103         * PasswdModifyRequestValue ::= SEQUENCE {
104         *     userIdentity    [0]  OCTET STRING OPTIONAL
105         *     ...
106         *     
107         * Set the userIdentity into the PasswdModifyRequest instance.
108         */
109        super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.USER_IDENTITY_TAG] =
110            new GrammarTransition<PasswordModifyRequestContainer>(
111                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
112                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
113                PasswordModifyRequestConstants.USER_IDENTITY_TAG,
114                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest user identity" )
115                {
116                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
117                    {
118                        BerValue value = container.getCurrentTLV().getValue();
119
120                        byte[] userIdentity = value.getData();
121
122                        if ( IS_DEBUG )
123                        {
124                            LOG.debug( "UserIdentity = " + Strings.dumpBytes( userIdentity ) );
125                        }
126
127                        if ( userIdentity == null )
128                        {
129                            userIdentity = Strings.EMPTY_BYTES;
130                        }
131
132                        ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
133                            .setUserIdentity( userIdentity );
134
135                        // We may have nothing left
136                        container.setGrammarEndAllowed( true );
137                    }
138                } );
139
140        /**
141         * Transition from userIdentity to oldPassword
142         *
143         * PasswdModifyRequestValue ::= SEQUENCE {
144         *     userIdentity    [0]  OCTET STRING OPTIONAL
145         *     oldPassword     [1]  OCTET STRING OPTIONAL
146         *     ...
147         *     
148         * Set the oldPassword into the PasswdModifyRequest instance.
149         */
150        super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.OLD_PASSWORD_TAG] =
151            new GrammarTransition<PasswordModifyRequestContainer>(
152                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
153                PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
154                PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
155                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest oldPassword" )
156                {
157                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
158                    {
159                        BerValue value = container.getCurrentTLV().getValue();
160
161                        byte[] oldPassword = value.getData();
162
163                        if ( IS_DEBUG )
164                        {
165                            LOG.debug( "oldPassword = " + Strings.dumpBytes( oldPassword ) );
166                        }
167
168                        if ( oldPassword == null )
169                        {
170                            oldPassword = Strings.EMPTY_BYTES;
171                        }
172
173                        ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
174                            .setOldPassword( oldPassword );
175
176                        // We may have nothing left
177                        container.setGrammarEndAllowed( true );
178                    }
179                } );
180
181        /**
182         * Transition from userIdentity to newPassword
183         *
184         * PasswdModifyRequestValue ::= SEQUENCE {
185         *     userIdentity    [0]  OCTET STRING OPTIONAL
186         *     ...
187         *     newPassword     [2]  OCTET STRING OPTIONAL
188         * 
189         *     
190         * Set the newPassword into the PasswdModifyRequest instance.
191         */
192        super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
193            new GrammarTransition<PasswordModifyRequestContainer>(
194                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
195                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
196                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
197                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
198                {
199                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
200                    {
201                        BerValue value = container.getCurrentTLV().getValue();
202
203                        byte[] newPassword = value.getData();
204
205                        if ( IS_DEBUG )
206                        {
207                            LOG.debug( "newPassword = " + Strings.dumpBytes( newPassword ) );
208                        }
209
210                        if ( newPassword == null )
211                        {
212                            newPassword = Strings.EMPTY_BYTES;
213                        }
214
215                        ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
216                            .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                        ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
257                            .setOldPassword( oldPassword );
258
259                        // We may have nothing left
260                        container.setGrammarEndAllowed( true );
261                    }
262                } );
263
264        /**
265         * Transition from PasswordModify Request Value to newPassword
266         *
267         * PasswdModifyRequestValue ::= SEQUENCE {
268         *     ...
269         *     newPassword    [2]  OCTET STRING OPTIONAL
270         * }
271         *     
272         * Set the newPassword into the PasswdModifyRequest instance.
273         */
274        super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
275            new GrammarTransition<PasswordModifyRequestContainer>(
276                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
277                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
278                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
279                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
280                {
281                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
282                    {
283                        BerValue value = container.getCurrentTLV().getValue();
284
285                        byte[] newPassword = value.getData();
286
287                        if ( IS_DEBUG )
288                        {
289                            LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
290                        }
291
292                        if ( newPassword == null )
293                        {
294                            newPassword = Strings.EMPTY_BYTES;
295                        }
296
297                        ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
298                            .setNewPassword( newPassword );
299
300                        // We may have nothing left
301                        container.setGrammarEndAllowed( true );
302                    }
303                } );
304
305        /**
306         * Transition from oldPassword to newPassword
307         *
308         *     ...
309         *     oldPassword    [1]  OCTET STRING OPTIONAL
310         *     newPassword    [2]  OCTET STRING OPTIONAL
311         * }
312         *     
313         * Set the newPassword into the PasswdModifyRequest instance.
314         */
315        super.transitions[PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
316            new GrammarTransition<PasswordModifyRequestContainer>(
317                PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
318                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
319                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
320                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
321                {
322                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
323                    {
324                        BerValue value = container.getCurrentTLV().getValue();
325
326                        byte[] newPassword = value.getData();
327
328                        if ( IS_DEBUG )
329                        {
330                            LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
331                        }
332
333                        if ( newPassword == null )
334                        {
335                            newPassword = Strings.EMPTY_BYTES;
336                        }
337
338                        ( ( PasswordModifyRequestDecorator ) container.getPwdModifyRequest() )
339                            .setNewPassword( newPassword );
340
341                        // We may have nothing left
342                        container.setGrammarEndAllowed( true );
343                    }
344                } );
345    }
346
347
348    /**
349     * This class is a singleton.
350     * 
351     * @return An instance on this grammar
352     */
353    public static Grammar<PasswordModifyRequestContainer> getInstance()
354    {
355        return instance;
356    }
357}