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.actions.searchRequest;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
25  import org.apache.directory.api.asn1.ber.tlv.BerValue;
26  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
27  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
31  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
32  import org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator;
33  import org.apache.directory.api.ldap.model.message.SearchRequest;
34  import org.apache.directory.api.ldap.model.message.SearchScope;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * The action used to store the SearchRequest scope
41   * <pre>
42   * SearchRequest ::= [APPLICATION 3] SEQUENCE {
43   *     ...
44   *     scope ENUMERATED {
45   *         baseObject   (0),
46   *         singleLevel  (1),
47   *         wholeSubtree (2) },
48   *     ...
49   * </pre>
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   */
52  public class StoreSearchRequestScope extends GrammarAction<LdapMessageContainer<SearchRequestDecorator>>
53  {
54      /** The logger */
55      private static final Logger LOG = LoggerFactory.getLogger( StoreSearchRequestScope.class );
56  
57      /** Speedup for logs */
58      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
59  
60  
61      /**
62       * Instantiates a new action.
63       */
64      public StoreSearchRequestScope()
65      {
66          super( "Store SearchRequest scope" );
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      public void action( LdapMessageContainer<SearchRequestDecorator> container ) throws DecoderException
74      {
75          SearchRequest searchRequest = container.getMessage().getDecorated();
76  
77          TLV tlv = container.getCurrentTLV();
78  
79          // We have to check that this is a correct scope
80          BerValue value = tlv.getValue();
81          int scope = 0;
82  
83          try
84          {
85              scope = IntegerDecoder.parse( value, LdapCodecConstants.SCOPE_BASE_OBJECT,
86                  LdapCodecConstants.SCOPE_WHOLE_SUBTREE );
87          }
88          catch ( IntegerDecoderException ide )
89          {
90              String msg = I18n.err( I18n.ERR_04101, value.toString() );
91              LOG.error( msg );
92              throw new DecoderException( msg );
93          }
94  
95          searchRequest.setScope( SearchScope.getSearchScope( scope ) );
96  
97          if ( IS_DEBUG )
98          {
99              switch ( scope )
100             {
101                 case LdapCodecConstants.SCOPE_BASE_OBJECT:
102                     LOG.debug( "Searching within BASE_OBJECT scope " );
103                     break;
104 
105                 case LdapCodecConstants.SCOPE_SINGLE_LEVEL:
106                     LOG.debug( "Searching within SINGLE_LEVEL scope " );
107                     break;
108 
109                 case LdapCodecConstants.SCOPE_WHOLE_SUBTREE:
110                     LOG.debug( "Searching within WHOLE_SUBTREE scope " );
111                     break;
112             }
113         }
114     }
115 }