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.shared.ldap.codec.controls.search.pagedSearch;
021
022
023import org.apache.directory.shared.asn1.DecoderException;
024import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
025import org.apache.directory.shared.asn1.ber.grammar.Grammar;
026import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
027import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
028import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
029import org.apache.directory.shared.asn1.ber.tlv.Value;
030import org.apache.directory.shared.asn1.ber.tlv.IntegerDecoder;
031import org.apache.directory.shared.asn1.ber.tlv.IntegerDecoderException;
032import org.apache.directory.shared.i18n.I18n;
033import org.apache.directory.shared.util.StringConstants;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037
038/**
039 * This class implements the PagedSearchControl. All the actions are declared in
040 * this class. As it is a singleton, these declaration are only done once.
041 * 
042 * The decoded grammar is the following :
043 * 
044 * realSearchControlValue ::= SEQUENCE {
045 *     size   INTEGER,
046 *     cookie OCTET STRING,
047 * }
048 * 
049 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
050 */
051public final class PagedResultsGrammar extends AbstractGrammar<PagedResultsContainer>
052{
053    /** The logger */
054    static final Logger LOG = LoggerFactory.getLogger( PagedResultsGrammar.class );
055
056    /** Speedup for logs */
057    static final boolean IS_DEBUG = LOG.isDebugEnabled();
058
059    /** The instance of grammar. PagedSearchControlGrammar is a singleton */
060    private static Grammar<?> instance = new PagedResultsGrammar();
061
062
063    /**
064     * Creates a new PagedSearchControlGrammar object.
065     */
066    @SuppressWarnings("unchecked")
067    private PagedResultsGrammar()
068    {
069        setName( PagedResultsGrammar.class.getName() );
070
071        // Create the transitions table
072        super.transitions = new GrammarTransition[ PagedResultsStates.LAST_PAGED_SEARCH_STATE.ordinal()][256];
073
074        /** 
075         * Transition from initial state to PagedSearch sequence
076         * realSearchControlValue ::= SEQUENCE OF {
077         *     ...
078         *     
079         * Nothing to do
080         */
081        super.transitions[ PagedResultsStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
082            new GrammarTransition<PagedResultsContainer>( PagedResultsStates.START_STATE,
083                                    PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE,
084                                    UniversalTag.SEQUENCE.getValue(), null );
085
086
087        /** 
088         * Transition from PagedSearch sequence to size
089         * 
090         * realSearchControlValue ::= SEQUENCE OF {
091         *     size  INTEGER,  -- INTEGER (0..maxInt),
092         *     ...
093         *     
094         * Stores the size value
095         */
096        super.transitions[ PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
097            new GrammarTransition<PagedResultsContainer>( PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE,
098                PagedResultsStates.SIZE_STATE,
099                UniversalTag.INTEGER.getValue(),
100                new GrammarAction<PagedResultsContainer>( "Set PagedSearchControl size" )
101            {
102                public void action( PagedResultsContainer container ) throws DecoderException
103                {
104                    Value value = container.getCurrentTLV().getValue();
105
106                    try
107                    {
108                        // Check that the value is into the allowed interval
109                        int size = IntegerDecoder.parse( value, Integer.MIN_VALUE, Integer.MAX_VALUE );
110                        
111                        // We allow negative value to absorb a bug in some M$ client.
112                        // Those negative values will be transformed to Integer.MAX_VALUE.
113                        if ( size < 0 )
114                        {
115                            size = Integer.MAX_VALUE;
116                        }
117                        
118                        if ( IS_DEBUG )
119                        {
120                            LOG.debug( "size = " + size );
121                        }
122
123                        container.getDecorator().setSize( size );
124                    }
125                    catch ( IntegerDecoderException e )
126                    {
127                        String msg = I18n.err( I18n.ERR_04050 );
128                        LOG.error( msg, e );
129                        throw new DecoderException( msg );
130                    }
131                }
132            } );
133
134        /** 
135         * Transition from size to cookie
136         * realSearchControlValue ::= SEQUENCE OF {
137         *     ...
138         *     cookie   OCTET STRING
139         * }
140         *     
141         * Stores the cookie flag
142         */
143        super.transitions[ PagedResultsStates.SIZE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
144            new GrammarTransition<PagedResultsContainer>( PagedResultsStates.SIZE_STATE,
145                                    PagedResultsStates.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(),
146                new GrammarAction<PagedResultsContainer>( "Set PagedSearchControl cookie" )
147            {
148                public void action( PagedResultsContainer container ) throws DecoderException
149                {
150                    Value value = container.getCurrentTLV().getValue();
151
152                    if ( container.getCurrentTLV().getLength() == 0 )
153                    {
154                        container.getDecorator().setCookie( StringConstants.EMPTY_BYTES );
155                    }
156                    else
157                    {
158                        container.getDecorator().setCookie( value.getData() );
159                    }
160
161                    // We can have an END transition
162                    container.setGrammarEndAllowed( true );
163                }
164            } );
165    }
166
167
168    /**
169     * This class is a singleton.
170     * 
171     * @return An instance on this grammar
172     */
173    public static Grammar<?> getInstance()
174    {
175        return instance;
176    }
177}