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.BooleanDecoder;
30  import org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException;
31  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32  import org.apache.directory.api.ldap.model.message.controls.SortKey;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * Grammar used for decoding a SortRequestControl.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class SortRequestGrammar extends AbstractGrammar<SortRequestContainer>
44  {
45      /** The logger */
46      static final Logger LOG = LoggerFactory.getLogger( SortRequestGrammar.class );
47  
48      /** Speedup for logs */
49      static final boolean IS_DEBUG = LOG.isDebugEnabled();
50  
51      /** The instance of grammar. SortRequestGrammar is a singleton */
52      private static Grammar<SortRequestContainer> instance = new SortRequestGrammar();
53  
54  
55      @SuppressWarnings("unchecked")
56      private SortRequestGrammar()
57      {
58          setName( SortRequestGrammar.class.getName() );
59  
60          GrammarAction<SortRequestContainer> addSortKey = new GrammarAction<SortRequestContainer>()
61          {
62  
63              @Override
64              public void action( SortRequestContainer container ) throws DecoderException
65              {
66                  BerValue value = container.getCurrentTLV().getValue();
67  
68                  String atDesc = Strings.utf8ToString( value.getData() );
69                  if ( IS_DEBUG )
70                  {
71                      LOG.debug( "AttributeTypeDesc = " + atDesc );
72                  }
73                  
74                  SortKey sk = new SortKey( atDesc );
75                  container.setCurrentKey( sk );
76                  container.getControl().addSortKey( sk );
77              }
78  
79          };
80  
81          GrammarAction<SortRequestContainer> storeReverseOrder = new GrammarAction<SortRequestContainer>()
82          {
83  
84              @Override
85              public void action( SortRequestContainer container ) throws DecoderException
86              {
87                  BerValue value = container.getCurrentTLV().getValue();
88  
89                  try
90                  {
91                      boolean reverseOrder = BooleanDecoder.parse( value );
92                      if ( IS_DEBUG )
93                      {
94                          LOG.debug( "ReverseOrder = " + reverseOrder );
95                      }
96                      
97                      container.getCurrentKey().setReverseOrder( reverseOrder );
98                      
99                      container.setGrammarEndAllowed( true );
100                 }
101                 catch ( BooleanDecoderException e )
102                 {
103                     //String msg = I18n.err( I18n.ERR_04050 );
104                     //LOG.error( msg, e );
105                     throw new DecoderException( e.getMessage() );
106                 }
107             }
108 
109         };
110         
111         // Create the transitions table
112         super.transitions = new GrammarTransition[SortRequestStates.END_STATE.ordinal()][256];
113 
114         super.transitions[SortRequestStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
115             new GrammarTransition<SortRequestContainer>( SortRequestStates.START_STATE,
116                 SortRequestStates.SEQUENCE_OF_SEQUENCE_STATE,
117                 UniversalTag.SEQUENCE.getValue(), null );
118 
119         super.transitions[SortRequestStates.SEQUENCE_OF_SEQUENCE_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
120             new GrammarTransition<SortRequestContainer>( SortRequestStates.SEQUENCE_OF_SEQUENCE_STATE,
121                 SortRequestStates.SORT_KEY_SEQUENCE_STATE,
122                 UniversalTag.SEQUENCE.getValue(), null );
123 
124         super.transitions[SortRequestStates.SORT_KEY_SEQUENCE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
125             new GrammarTransition<SortRequestContainer>( SortRequestStates.SORT_KEY_SEQUENCE_STATE,
126                 SortRequestStates.AT_DESC_STATE,
127                 UniversalTag.OCTET_STRING.getValue(), addSortKey );
128         
129         super.transitions[SortRequestStates.AT_DESC_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
130             new GrammarTransition<SortRequestContainer>( SortRequestStates.AT_DESC_STATE,
131                 SortRequestStates.ORDER_RULE_STATE,
132                 UniversalTag.OCTET_STRING.getValue(), new GrammarAction<SortRequestContainer>()
133                 {
134 
135                     @Override
136                     public void action( SortRequestContainer container ) throws DecoderException
137                     {
138                         BerValue value = container.getCurrentTLV().getValue();
139 
140                         String matchingRuleOid = Strings.utf8ToString( value.getData() );
141                         if ( IS_DEBUG )
142                         {
143                             LOG.debug( "MatchingRuleOid = " + matchingRuleOid );
144                         }
145                         
146                         container.getCurrentKey().setMatchingRuleId( matchingRuleOid );
147                     }
148 
149                 } );
150         
151         super.transitions[SortRequestStates.ORDER_RULE_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] =
152             new GrammarTransition<SortRequestContainer>( SortRequestStates.ORDER_RULE_STATE,
153                 SortRequestStates.REVERSE_ORDER_STATE,
154                 UniversalTag.BOOLEAN.getValue(), storeReverseOrder );
155         
156         super.transitions[SortRequestStates.AT_DESC_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] =
157             new GrammarTransition<SortRequestContainer>( SortRequestStates.AT_DESC_STATE,
158                 SortRequestStates.REVERSE_ORDER_STATE,
159                 UniversalTag.BOOLEAN.getValue(), storeReverseOrder );
160 
161         super.transitions[SortRequestStates.REVERSE_ORDER_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
162             new GrammarTransition<SortRequestContainer>( SortRequestStates.REVERSE_ORDER_STATE,
163                 SortRequestStates.SORT_KEY_SEQUENCE_STATE,
164                 UniversalTag.SEQUENCE.getValue(), null );
165 
166     }
167 
168 
169     /**
170      * This class is a singleton.
171      * 
172      * @return An instance on this grammar
173      */
174     public static Grammar<?> getInstance()
175     {
176         return instance;
177     }
178 }