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.AliasDerefMode;
34  import org.apache.directory.api.ldap.model.message.SearchRequest;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * The action used to store the SearchRequest derefAlias flag
41   * <pre>
42   * SearchRequest ::= [APPLICATION 3] SEQUENCE {
43   *     ...
44   *     derefAliases ENUMERATED {
45   *         neverDerefAliases   (0),
46   *         derefInSearching    (1),
47   *         derefFindingBaseObj (2),
48   *         derefAlways         (3) },
49   *     ...
50   * </pre>
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   */
53  public class StoreSearchRequestDerefAlias extends GrammarAction<LdapMessageContainer<SearchRequestDecorator>>
54  {
55      /** The logger */
56      private static final Logger LOG = LoggerFactory.getLogger( StoreSearchRequestDerefAlias.class );
57  
58      /** Speedup for logs */
59      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
60  
61  
62      /**
63       * Instantiates a new action.
64       */
65      public StoreSearchRequestDerefAlias()
66      {
67          super( "Store SearchRequest derefAlias flag" );
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      public void action( LdapMessageContainer<SearchRequestDecorator> container ) throws DecoderException
75      {
76          SearchRequest searchRequest = container.getMessage().getDecorated();
77  
78          TLV tlv = container.getCurrentTLV();
79  
80          // We have to check that this is a correct derefAliases
81          BerValue value = tlv.getValue();
82          int derefAliases = 0;
83  
84          try
85          {
86              derefAliases = IntegerDecoder.parse( value, LdapCodecConstants.NEVER_DEREF_ALIASES,
87                  LdapCodecConstants.DEREF_ALWAYS );
88          }
89          catch ( IntegerDecoderException ide )
90          {
91              String msg = I18n.err( I18n.ERR_04102, value.toString() );
92              LOG.error( msg );
93              throw new DecoderException( msg, ide );
94          }
95  
96          searchRequest.setDerefAliases( AliasDerefMode.getDerefMode( derefAliases ) );
97  
98          if ( IS_DEBUG )
99          {
100             switch ( derefAliases )
101             {
102                 case LdapCodecConstants.NEVER_DEREF_ALIASES:
103                     LOG.debug( "Handling object strategy : NEVER_DEREF_ALIASES" );
104                     break;
105 
106                 case LdapCodecConstants.DEREF_IN_SEARCHING:
107                     LOG.debug( "Handling object strategy : DEREF_IN_SEARCHING" );
108                     break;
109 
110                 case LdapCodecConstants.DEREF_FINDING_BASE_OBJ:
111                     LOG.debug( "Handling object strategy : DEREF_FINDING_BASE_OBJ" );
112                     break;
113 
114                 case LdapCodecConstants.DEREF_ALWAYS:
115                     LOG.debug( "Handling object strategy : DEREF_ALWAYS" );
116                     break;
117 
118                 default:
119                     LOG.debug( "Handling object strategy : UNKNOWN" );
120             }
121         }
122     }
123 }