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.sort;
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.util.Strings;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * Grammar for decoding SortResponseControl. It's defined in https://tools.ietf.org/html/rfc2891
037 * 
038 * <pre>
039 *       SortResult ::= SEQUENCE {
040 *       sortResult  ENUMERATED {
041 *           success                   (0), -- results are sorted
042 *           operationsError           (1), -- server internal failure
043 *           timeLimitExceeded         (3), -- timelimit reached before
044 *                                          -- sorting was completed
045 *           strongAuthRequired        (8), -- refused to return sorted
046 *                                          -- results via insecure
047 *                                          -- protocol
048 *           adminLimitExceeded       (11), -- too many matching entries
049 $                                          -- for the server to sort
050 *           noSuchAttribute          (16), -- unrecognized attribute
051 *                                          -- type in sort key
052 *           inappropriateMatching    (18), -- unrecognized or
053 *                                          -- inappropriate matching
054 *                                          -- rule in sort key
055 *           insufficientAccessRights (50), -- refused to return sorted
056 *                                          -- results to this client
057 *           busy                     (51), -- too busy to process
058 *           unwillingToPerform       (53), -- unable to sort
059 *           other                    (80)
060 *           },
061 *     attributeType [0] AttributeDescription OPTIONAL }
062 * </pre>
063 *
064 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
065 */
066public final class SortResponseGrammar extends AbstractGrammar<SortResponseContainer>
067{
068    /** The logger */
069    static final Logger LOG = LoggerFactory.getLogger( SortRequestGrammar.class );
070
071    /** Speedup for logs */
072    static final boolean IS_DEBUG = LOG.isDebugEnabled();
073
074    /** The instance of grammar. SortResponseGrammar is a singleton */
075    private static Grammar<SortResponseContainer> instance = new SortResponseGrammar();
076
077
078    @SuppressWarnings("unchecked")
079    private SortResponseGrammar()
080    {
081        setName( SortResponseGrammar.class.getName() );
082
083        // Create the transitions table
084        super.transitions = new GrammarTransition[SortResponseStates.END_STATE.ordinal()][256];
085
086        super.transitions[SortResponseStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
087            new GrammarTransition<SortResponseContainer>( SortResponseStates.START_STATE,
088                SortResponseStates.SEQUENCE_STATE,
089                UniversalTag.SEQUENCE.getValue(), null );
090        
091        super.transitions[SortResponseStates.SEQUENCE_STATE.ordinal()][UniversalTag.ENUMERATED.getValue()] =
092            new GrammarTransition<SortResponseContainer>( SortResponseStates.SEQUENCE_STATE,
093                SortResponseStates.RESULT_CODE_STATE,
094                UniversalTag.ENUMERATED.getValue(), new StoreSortResponseResultCode<SortResponseContainer>() );
095
096        super.transitions[SortResponseStates.RESULT_CODE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
097            new GrammarTransition<SortResponseContainer>( SortResponseStates.RESULT_CODE_STATE,
098                SortResponseStates.AT_DESC_STATE,
099                UniversalTag.OCTET_STRING.getValue(), new GrammarAction<SortResponseContainer>()
100                {
101
102                    @Override
103                    public void action( SortResponseContainer container ) throws DecoderException
104                    {
105                        BerValue value = container.getCurrentTLV().getValue();
106
107                        String atType = Strings.utf8ToString( value.getData() );
108                        if ( IS_DEBUG )
109                        {
110                            LOG.debug( "AttributeType = " + atType );
111                        }
112                        
113                        container.getControl().setAttributeName( atType );
114                        container.setGrammarEndAllowed( true );
115                    }
116                } );
117
118    }
119
120
121    /**
122     * This class is a singleton.
123     * 
124     * @return An instance on this grammar
125     */
126    public static Grammar<?> getInstance()
127    {
128        return instance;
129    }
130}