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    /**
066     * Creates a new PasswordModifyRequestGrammar object.
067     */
068    @SuppressWarnings("unchecked")
069    public PasswordModifyRequestGrammar()
070    {
071        setName( PasswordModifyRequestGrammar.class.getName() );
072
073        // Create the transitions table
074        super.transitions = new GrammarTransition[PasswordModifyRequestStatesEnum.LAST_PASSWORD_MODIFY_REQUEST_STATE
075            .ordinal()][256];
076
077        /**
078         * Transition from init state to PasswordModify Request Value
079         * 
080         * PasswdModifyRequestValue ::= SEQUENCE {
081         *     ...
082         *     
083         * Creates the PasswdModifyRequest object
084         */
085        super.transitions[PasswordModifyRequestStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
086            new GrammarTransition<PasswordModifyRequestContainer>(
087                PasswordModifyRequestStatesEnum.START_STATE,
088                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
089                UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyRequestContainer>(
090                    "Init PasswordModifyRequest" )
091                {
092                    public void action( PasswordModifyRequestContainer container )
093                    {
094                        PasswordModifyRequestDecorator passwordModifyRequestDecorator = new PasswordModifyRequestDecorator(
095                            LdapApiServiceFactory.getSingleton(), new PasswordModifyRequestImpl() );
096                        container.setPasswordModifyRequest( passwordModifyRequestDecorator );
097
098                        // We may have nothing left
099                        container.setGrammarEndAllowed( true );
100                    }
101                } );
102
103        /**
104         * Transition from PasswordModify Request Value to userIdentity
105         *
106         * PasswdModifyRequestValue ::= SEQUENCE {
107         *     userIdentity    [0]  OCTET STRING OPTIONAL
108         *     ...
109         *     
110         * Set the userIdentity into the PasswdModifyRequest instance.
111         */
112        super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.USER_IDENTITY_TAG] =
113            new GrammarTransition<PasswordModifyRequestContainer>(
114                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
115                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
116                PasswordModifyRequestConstants.USER_IDENTITY_TAG,
117                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest user identity" )
118                {
119                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
120                    {
121                        BerValue value = container.getCurrentTLV().getValue();
122
123                        byte[] userIdentity = value.getData();
124
125                        if ( IS_DEBUG )
126                        {
127                            LOG.debug( "UserIdentity = " + Strings.dumpBytes( userIdentity ) );
128                        }
129
130                        if ( userIdentity == null )
131                        {
132                            userIdentity = Strings.EMPTY_BYTES;
133                        }
134
135                        container.getPwdModifyRequest().setUserIdentity( userIdentity );
136
137                        // We may have nothing left
138                        container.setGrammarEndAllowed( true );
139                    }
140                } );
141
142        /**
143         * Transition from userIdentity to oldPassword
144         *
145         * PasswdModifyRequestValue ::= SEQUENCE {
146         *     userIdentity    [0]  OCTET STRING OPTIONAL
147         *     oldPassword     [1]  OCTET STRING OPTIONAL
148         *     ...
149         *     
150         * Set the oldPassword into the PasswdModifyRequest instance.
151         */
152        super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.OLD_PASSWORD_TAG] =
153            new GrammarTransition<PasswordModifyRequestContainer>(
154                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
155                PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
156                PasswordModifyRequestConstants.OLD_PASSWORD_TAG,
157                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest oldPassword" )
158                {
159                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
160                    {
161                        BerValue value = container.getCurrentTLV().getValue();
162
163                        byte[] oldPassword = value.getData();
164
165                        if ( IS_DEBUG )
166                        {
167                            LOG.debug( "oldPassword = " + Strings.dumpBytes( oldPassword ) );
168                        }
169
170                        if ( oldPassword == null )
171                        {
172                            oldPassword = Strings.EMPTY_BYTES;
173                        }
174
175                        container.getPwdModifyRequest().setOldPassword( oldPassword );
176
177                        // We may have nothing left
178                        container.setGrammarEndAllowed( true );
179                    }
180                } );
181
182        /**
183         * Transition from userIdentity to newPassword
184         *
185         * PasswdModifyRequestValue ::= SEQUENCE {
186         *     userIdentity    [0]  OCTET STRING OPTIONAL
187         *     ...
188         *     newPassword     [2]  OCTET STRING OPTIONAL
189         * 
190         *     
191         * Set the newPassword into the PasswdModifyRequest instance.
192         */
193        super.transitions[PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
194            new GrammarTransition<PasswordModifyRequestContainer>(
195                PasswordModifyRequestStatesEnum.USER_IDENTITY_STATE,
196                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
197                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
198                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
199                {
200                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
201                    {
202                        BerValue value = container.getCurrentTLV().getValue();
203
204                        byte[] newPassword = value.getData();
205
206                        if ( IS_DEBUG )
207                        {
208                            LOG.debug( "newPassword = " + Strings.dumpBytes( newPassword ) );
209                        }
210
211                        if ( newPassword == null )
212                        {
213                            newPassword = Strings.EMPTY_BYTES;
214                        }
215
216                        container.getPwdModifyRequest().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                        container.getPwdModifyRequest().setOldPassword( oldPassword );
257
258                        // We may have nothing left
259                        container.setGrammarEndAllowed( true );
260                    }
261                } );
262
263        /**
264         * Transition from PasswordModify Request Value to newPassword
265         *
266         * PasswdModifyRequestValue ::= SEQUENCE {
267         *     ...
268         *     newPassword    [2]  OCTET STRING OPTIONAL
269         * }
270         *     
271         * Set the newPassword into the PasswdModifyRequest instance.
272         */
273        super.transitions[PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
274            new GrammarTransition<PasswordModifyRequestContainer>(
275                PasswordModifyRequestStatesEnum.PASSWORD_MODIFY_REQUEST_SEQUENCE_STATE,
276                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
277                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
278                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
279                {
280                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
281                    {
282                        BerValue value = container.getCurrentTLV().getValue();
283
284                        byte[] newPassword = value.getData();
285
286                        if ( IS_DEBUG )
287                        {
288                            LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
289                        }
290
291                        if ( newPassword == null )
292                        {
293                            newPassword = Strings.EMPTY_BYTES;
294                        }
295
296                        container.getPwdModifyRequest().setNewPassword( newPassword );
297
298                        // We may have nothing left
299                        container.setGrammarEndAllowed( true );
300                    }
301                } );
302
303        /**
304         * Transition from oldPassword to newPassword
305         *
306         *     ...
307         *     oldPassword    [1]  OCTET STRING OPTIONAL
308         *     newPassword    [2]  OCTET STRING OPTIONAL
309         * }
310         *     
311         * Set the newPassword into the PasswdModifyRequest instance.
312         */
313        super.transitions[PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE.ordinal()][PasswordModifyRequestConstants.NEW_PASSWORD_TAG] =
314            new GrammarTransition<PasswordModifyRequestContainer>(
315                PasswordModifyRequestStatesEnum.OLD_PASSWORD_STATE,
316                PasswordModifyRequestStatesEnum.NEW_PASSWORD_STATE,
317                PasswordModifyRequestConstants.NEW_PASSWORD_TAG,
318                new GrammarAction<PasswordModifyRequestContainer>( "Set PasswordModifyRequest newPassword" )
319                {
320                    public void action( PasswordModifyRequestContainer container ) throws DecoderException
321                    {
322                        BerValue value = container.getCurrentTLV().getValue();
323
324                        byte[] newPassword = value.getData();
325
326                        if ( IS_DEBUG )
327                        {
328                            LOG.debug( "NewPassword = " + Strings.dumpBytes( newPassword ) );
329                        }
330
331                        if ( newPassword == null )
332                        {
333                            newPassword = Strings.EMPTY_BYTES;
334                        }
335
336                        container.getPwdModifyRequest().setNewPassword( newPassword );
337
338                        // We may have nothing left
339                        container.setGrammarEndAllowed( true );
340                    }
341                } );
342    }
343
344
345    /**
346     * This class is a singleton.
347     * 
348     * @return An instance on this grammar
349     */
350    public static Grammar<PasswordModifyRequestContainer> getInstance()
351    {
352        return instance;
353    }
354}