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.pagedSearch;
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.IntegerDecoder;
030import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
031import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
032import org.apache.directory.api.i18n.I18n;
033import org.apache.directory.api.util.Strings;
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    /** The instance of grammar. PagedSearchControlGrammar is a singleton */
057    private static Grammar<?> instance = new PagedResultsGrammar();
058
059
060    /**
061     * Creates a new PagedSearchControlGrammar object.
062     */
063    @SuppressWarnings("unchecked")
064    private PagedResultsGrammar()
065    {
066        setName( PagedResultsGrammar.class.getName() );
067
068        // Create the transitions table
069        super.transitions = new GrammarTransition[PagedResultsStates.LAST_PAGED_SEARCH_STATE.ordinal()][256];
070
071        /** 
072         * Transition from initial state to PagedSearch sequence
073         * realSearchControlValue ::= SEQUENCE OF {
074         *     ...
075         *     
076         * Nothing to do
077         */
078        super.transitions[PagedResultsStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
079            new GrammarTransition<PagedResultsContainer>( PagedResultsStates.START_STATE,
080                PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE,
081                UniversalTag.SEQUENCE.getValue(), null );
082
083        /** 
084         * Transition from PagedSearch sequence to size
085         * 
086         * realSearchControlValue ::= SEQUENCE OF {
087         *     size  INTEGER,  -- INTEGER (0..maxInt),
088         *     ...
089         *     
090         * Stores the size value
091         */
092        super.transitions[PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
093            new GrammarTransition<PagedResultsContainer>( PagedResultsStates.PAGED_SEARCH_SEQUENCE_STATE,
094                PagedResultsStates.SIZE_STATE,
095                UniversalTag.INTEGER.getValue(),
096                new GrammarAction<PagedResultsContainer>( "Set PagedSearchControl size" )
097                {
098                    public void action( PagedResultsContainer container ) throws DecoderException
099                    {
100                        BerValue value = container.getCurrentTLV().getValue();
101
102                        try
103                        {
104                            // Check that the value is into the allowed interval
105                            int size = IntegerDecoder.parse( value, Integer.MIN_VALUE, Integer.MAX_VALUE );
106
107                            // We allow negative value to absorb a bug in some M$ client.
108                            // Those negative values will be transformed to Integer.MAX_VALUE.
109                            if ( size < 0 )
110                            {
111                                size = Integer.MAX_VALUE;
112                            }
113
114                            if ( LOG.isDebugEnabled() )
115                            {
116                                LOG.debug( I18n.msg( I18n.MSG_05303_SIZE, size ) );
117                            }
118
119                            container.getPagedResults().setSize( size );
120                        }
121                        catch ( IntegerDecoderException ide )
122                        {
123                            String msg = I18n.err( I18n.ERR_05306_PAGED_SEARCH_SIZE_DECODING_ERROR );
124                            LOG.error( msg, ide );
125                            throw new DecoderException( msg, ide );
126                        }
127                    }
128                } );
129
130        /** 
131         * Transition from size to cookie
132         * realSearchControlValue ::= SEQUENCE OF {
133         *     ...
134         *     cookie   OCTET STRING
135         * }
136         *     
137         * Stores the cookie flag
138         */
139        super.transitions[PagedResultsStates.SIZE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
140            new GrammarTransition<PagedResultsContainer>( PagedResultsStates.SIZE_STATE,
141                PagedResultsStates.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(),
142                new GrammarAction<PagedResultsContainer>( "Set PagedSearchControl cookie" )
143                {
144                    public void action( PagedResultsContainer container )
145                    {
146                        BerValue value = container.getCurrentTLV().getValue();
147
148                        if ( container.getCurrentTLV().getLength() == 0 )
149                        {
150                            container.getPagedResults().setCookie( Strings.EMPTY_BYTES );
151                        }
152                        else
153                        {
154                            container.getPagedResults().setCookie( value.getData() );
155                        }
156
157                        // We can have an END transition
158                        container.setGrammarEndAllowed( true );
159                    }
160                } );
161    }
162
163
164    /**
165     * This class is a singleton.
166     * 
167     * @return An instance on this grammar
168     */
169    public static Grammar<?> getInstance()
170    {
171        return instance;
172    }
173}