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