View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.codec.controls.sort;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
25  import org.apache.directory.api.asn1.ber.grammar.Grammar;
26  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
27  import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
28  import org.apache.directory.api.asn1.ber.tlv.BerValue;
29  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30  import org.apache.directory.api.util.Strings;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * Grammar for decoding SortResponseControl. It's defined in https://tools.ietf.org/html/rfc2891
37   * 
38   * <pre>
39   *       SortResult ::= SEQUENCE {
40   *       sortResult  ENUMERATED {
41   *           success                   (0), -- results are sorted
42   *           operationsError           (1), -- server internal failure
43   *           timeLimitExceeded         (3), -- timelimit reached before
44   *                                          -- sorting was completed
45   *           strongAuthRequired        (8), -- refused to return sorted
46   *                                          -- results via insecure
47   *                                          -- protocol
48   *           adminLimitExceeded       (11), -- too many matching entries
49   $                                          -- for the server to sort
50   *           noSuchAttribute          (16), -- unrecognized attribute
51   *                                          -- type in sort key
52   *           inappropriateMatching    (18), -- unrecognized or
53   *                                          -- inappropriate matching
54   *                                          -- rule in sort key
55   *           insufficientAccessRights (50), -- refused to return sorted
56   *                                          -- results to this client
57   *           busy                     (51), -- too busy to process
58   *           unwillingToPerform       (53), -- unable to sort
59   *           other                    (80)
60   *           },
61   *     attributeType [0] AttributeDescription OPTIONAL }
62   * </pre>
63   *
64   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
65   */
66  public final class SortResponseGrammar extends AbstractGrammar<SortResponseContainer>
67  {
68      /** The logger */
69      static final Logger LOG = LoggerFactory.getLogger( SortRequestGrammar.class );
70  
71      /** Speedup for logs */
72      static final boolean IS_DEBUG = LOG.isDebugEnabled();
73  
74      /** The instance of grammar. SortResponseGrammar is a singleton */
75      private static Grammar<SortResponseContainer> instance = new SortResponseGrammar();
76  
77  
78      @SuppressWarnings("unchecked")
79      private SortResponseGrammar()
80      {
81          setName( SortResponseGrammar.class.getName() );
82  
83          // Create the transitions table
84          super.transitions = new GrammarTransition[SortResponseStates.END_STATE.ordinal()][256];
85  
86          super.transitions[SortResponseStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
87              new GrammarTransition<SortResponseContainer>( SortResponseStates.START_STATE,
88                  SortResponseStates.SEQUENCE_STATE,
89                  UniversalTag.SEQUENCE.getValue(), null );
90          
91          super.transitions[SortResponseStates.SEQUENCE_STATE.ordinal()][UniversalTag.ENUMERATED.getValue()] =
92              new GrammarTransition<SortResponseContainer>( SortResponseStates.SEQUENCE_STATE,
93                  SortResponseStates.RESULT_CODE_STATE,
94                  UniversalTag.ENUMERATED.getValue(), new StoreSortResponseResultCode<SortResponseContainer>() );
95  
96          super.transitions[SortResponseStates.RESULT_CODE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
97              new GrammarTransition<SortResponseContainer>( SortResponseStates.RESULT_CODE_STATE,
98                  SortResponseStates.AT_DESC_STATE,
99                  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 }