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.codec.controls.search.persistentSearch;
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.BooleanDecoder;
030import org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException;
031import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
032import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
033import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
034import org.apache.directory.api.i18n.I18n;
035import org.apache.directory.api.ldap.model.message.controls.PersistentSearch;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039
040/**
041 * This class implements the PSearchControl. All the actions are declared in
042 * this class. As it is a singleton, these declaration are only done once.
043 * 
044 * The decoded grammar is the following :
045 * 
046 * PersistenceSearch ::= SEQUENCE {
047 *     changeTypes  INTEGER,  -- an OR combinaison of 0, 1, 2 and 4 --
048 *     changeOnly   BOOLEAN,
049 *     returnECs    BOOLEAN
050 * }
051 * 
052 * The changeTypes field is the logical OR of one or more of these values:
053 * add    (1), 
054 * delete (2), 
055 * modify (4), 
056 * modDN  (8).
057 * 
058 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
059 */
060public final class PersistentSearchGrammar extends AbstractGrammar<PersistentSearchContainer>
061{
062    /** The logger */
063    static final Logger LOG = LoggerFactory.getLogger( PersistentSearchGrammar.class );
064
065    /** Speedup for logs */
066    static final boolean IS_DEBUG = LOG.isDebugEnabled();
067
068    /** The instance of grammar. PSearchControlGrammar is a singleton */
069    private static Grammar<?> instance = new PersistentSearchGrammar();
070
071
072    /**
073     * Creates a new PSearchControlGrammar object.
074     */
075    @SuppressWarnings("unchecked")
076    private PersistentSearchGrammar()
077    {
078        setName( PersistentSearchGrammar.class.getName() );
079
080        // Create the transitions table
081        super.transitions = new GrammarTransition[PersistentSearchStates.LAST_PSEARCH_STATE.ordinal()][256];
082
083        /** 
084         * Transition from initial state to Psearch sequence
085         * PSearch ::= SEQUENCE OF {
086         *     ...
087         *     
088         * Initialize the persistence search object
089         */
090        super.transitions[PersistentSearchStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
091            new GrammarTransition<PersistentSearchContainer>( PersistentSearchStates.START_STATE,
092                PersistentSearchStates.PSEARCH_SEQUENCE_STATE,
093                UniversalTag.SEQUENCE.getValue(), null );
094
095        /** 
096         * Transition from Psearch sequence to Change types
097         * PSearch ::= SEQUENCE OF {
098         *     changeTypes  INTEGER,  -- an OR combinaison of 0, 1, 2 and 4 --
099         *     ...
100         *     
101         * Stores the change types value
102         */
103        super.transitions[PersistentSearchStates.PSEARCH_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
104            new GrammarTransition<PersistentSearchContainer>( PersistentSearchStates.PSEARCH_SEQUENCE_STATE,
105                PersistentSearchStates.CHANGE_TYPES_STATE,
106                UniversalTag.INTEGER.getValue(),
107                new GrammarAction<PersistentSearchContainer>( "Set PSearchControl changeTypes" )
108                {
109                    public void action( PersistentSearchContainer container ) throws DecoderException
110                    {
111                        BerValue value = container.getCurrentTLV().getValue();
112
113                        try
114                        {
115                            // Check that the value is into the allowed interval
116                            int changeTypes = IntegerDecoder.parse( value,
117                                PersistentSearch.CHANGE_TYPES_MIN,
118                                PersistentSearch.CHANGE_TYPES_MAX );
119
120                            if ( IS_DEBUG )
121                            {
122                                LOG.debug( "changeTypes = " + changeTypes );
123                            }
124
125                            container.getPersistentSearchDecorator().setChangeTypes( changeTypes );
126                        }
127                        catch ( IntegerDecoderException ide )
128                        {
129                            String msg = I18n.err( I18n.ERR_04051 );
130                            LOG.error( msg, ide );
131                            throw new DecoderException( msg, ide );
132                        }
133                    }
134                } );
135
136        /** 
137         * Transition from Change types to Changes only
138         * PSearch ::= SEQUENCE OF {
139         *     ...
140         *     changeOnly   BOOLEAN,
141         *     ...
142         *     
143         * Stores the change only flag
144         */
145        super.transitions[PersistentSearchStates.CHANGE_TYPES_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] =
146            new GrammarTransition<PersistentSearchContainer>( PersistentSearchStates.CHANGE_TYPES_STATE,
147                PersistentSearchStates.CHANGES_ONLY_STATE, UniversalTag.BOOLEAN.getValue(),
148                new GrammarAction<PersistentSearchContainer>( "Set PSearchControl changesOnly" )
149                {
150                    public void action( PersistentSearchContainer container ) throws DecoderException
151                    {
152                        BerValue value = container.getCurrentTLV().getValue();
153
154                        try
155                        {
156                            boolean changesOnly = BooleanDecoder.parse( value );
157
158                            if ( IS_DEBUG )
159                            {
160                                LOG.debug( "changesOnly = " + changesOnly );
161                            }
162
163                            container.getPersistentSearchDecorator().setChangesOnly( changesOnly );
164                        }
165                        catch ( BooleanDecoderException bde )
166                        {
167                            String msg = I18n.err( I18n.ERR_04052 );
168                            LOG.error( msg, bde );
169                            throw new DecoderException( msg, bde );
170                        }
171                    }
172                } );
173
174        /** 
175         * Transition from Change types to Changes only
176         * PSearch ::= SEQUENCE OF {
177         *     ...
178         *     returnECs    BOOLEAN 
179         * }
180         *     
181         * Stores the return ECs flag 
182         */
183        super.transitions[PersistentSearchStates.CHANGES_ONLY_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] =
184            new GrammarTransition<PersistentSearchContainer>( PersistentSearchStates.CHANGES_ONLY_STATE,
185                PersistentSearchStates.RETURN_ECS_STATE, UniversalTag.BOOLEAN.getValue(),
186                new GrammarAction<PersistentSearchContainer>( "Set PSearchControl returnECs" )
187                {
188                    public void action( PersistentSearchContainer container ) throws DecoderException
189                    {
190                        BerValue value = container.getCurrentTLV().getValue();
191
192                        try
193                        {
194                            boolean returnECs = BooleanDecoder.parse( value );
195
196                            if ( IS_DEBUG )
197                            {
198                                LOG.debug( "returnECs = " + returnECs );
199                            }
200
201                            container.getPersistentSearchDecorator().setReturnECs( returnECs );
202
203                            // We can have an END transition
204                            container.setGrammarEndAllowed( true );
205                        }
206                        catch ( BooleanDecoderException bde )
207                        {
208                            String msg = I18n.err( I18n.ERR_04053 );
209                            LOG.error( msg, bde );
210                            throw new DecoderException( msg, bde );
211                        }
212                    }
213                } );
214    }
215
216
217    /**
218     * This class is a singleton.
219     * 
220     * @return An instance on this grammar
221     */
222    public static Grammar<?> getInstance()
223    {
224        return instance;
225    }
226}