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   *     https://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 static org.apache.directory.api.ldap.codec.controls.sort.SortResponseFactory.ATTRIBUTE_TYPE_TAG;
24  
25  import org.apache.directory.api.asn1.DecoderException;
26  import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
27  import org.apache.directory.api.asn1.ber.grammar.Grammar;
28  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
29  import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
30  import org.apache.directory.api.asn1.ber.tlv.BerValue;
31  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32  import org.apache.directory.api.i18n.I18n;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * Grammar for decoding SortResponseControl.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public final class SortResponseGrammar extends AbstractGrammar<SortResponseContainer>
44  {
45      /** The logger */
46      static final Logger LOG = LoggerFactory.getLogger( SortRequestGrammar.class );
47  
48      /** The instance of grammar. SortResponseGrammar is a singleton */
49      private static Grammar<SortResponseContainer> instance = new SortResponseGrammar();
50  
51  
52      @SuppressWarnings("unchecked")
53      private SortResponseGrammar()
54      {
55          setName( SortResponseGrammar.class.getName() );
56  
57          // Create the transitions table
58          super.transitions = new GrammarTransition[SortResponseStates.END_STATE.ordinal()][256];
59  
60          super.transitions[SortResponseStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
61              new GrammarTransition<SortResponseContainer>( SortResponseStates.START_STATE,
62                  SortResponseStates.SEQUENCE_STATE,
63                  UniversalTag.SEQUENCE.getValue(), null );
64  
65          super.transitions[SortResponseStates.SEQUENCE_STATE.ordinal()][UniversalTag.ENUMERATED.getValue()] =
66              new GrammarTransition<SortResponseContainer>( SortResponseStates.SEQUENCE_STATE,
67                  SortResponseStates.RESULT_CODE_STATE,
68                  UniversalTag.ENUMERATED.getValue(), new StoreSortResponseResultCode<SortResponseContainer>() );
69  
70          super.transitions[SortResponseStates.RESULT_CODE_STATE.ordinal()][ATTRIBUTE_TYPE_TAG] =
71              new GrammarTransition<SortResponseContainer>( SortResponseStates.RESULT_CODE_STATE,
72                  SortResponseStates.AT_DESC_STATE,
73                  ATTRIBUTE_TYPE_TAG, new GrammarAction<SortResponseContainer>()
74                  {
75  
76                      @Override
77                      public void action( SortResponseContainer container ) throws DecoderException
78                      {
79                          BerValue value = container.getCurrentTLV().getValue();
80  
81                          String atType = Strings.utf8ToString( value.getData() );
82  
83                          if ( LOG.isDebugEnabled() )
84                          {
85                              LOG.debug( I18n.msg( I18n.MSG_05310_ATTRIBUTE_TYPE, atType ) );
86                          }
87  
88                          container.getControl().setAttributeName( atType );
89                          container.setGrammarEndAllowed( true );
90                      }
91                  } );
92  
93      }
94  
95  
96      /**
97       * This class is a singleton.
98       *
99       * @return An instance on this grammar
100      */
101     public static Grammar<?> getInstance()
102     {
103         return instance;
104     }
105 }