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.filter;
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.TLV;
26  import org.apache.directory.api.ldap.codec.AttributeValueAssertion;
27  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
28  import org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator;
29  import org.apache.directory.api.ldap.codec.search.AttributeValueAssertionFilter;
30  import org.apache.directory.api.ldap.model.entry.BinaryValue;
31  import org.apache.directory.api.ldap.model.entry.StringValue;
32  import org.apache.directory.api.ldap.model.entry.Value;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * The action used to initialize the Assertion Value filter
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class InitAssertionValueFilter extends GrammarAction<LdapMessageContainer<SearchRequestDecorator>>
44  {
45      /** The logger */
46      private static final Logger LOG = LoggerFactory.getLogger( InitAssertionValueFilter.class );
47  
48      /** Speedup for logs */
49      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
50  
51  
52      /**
53       * Instantiates a new init assertion value filter action.
54       */
55      public InitAssertionValueFilter()
56      {
57          super( "Initialize Assertion Value filter" );
58      }
59  
60  
61      /**
62       * {@inheritDoc}
63       */
64      public void action( LdapMessageContainer<SearchRequestDecorator> container ) throws DecoderException
65      {
66          SearchRequestDecorator searchRequestDecorator = container.getMessage();
67  
68          TLV tlv = container.getCurrentTLV();
69  
70          // The value can be null.
71          Value<?> assertionValue;
72  
73          AttributeValueAssertionFilter terminalFilter = ( AttributeValueAssertionFilter )
74              searchRequestDecorator.getTerminalFilter();
75          AttributeValueAssertion assertion = terminalFilter.getAssertion();
76  
77          if ( container.isBinary( assertion.getAttributeDesc() ) )
78          {
79              if ( tlv.getLength() != 0 )
80              {
81                  assertionValue = new BinaryValue( tlv.getValue().getData() );
82              }
83              else
84              {
85                  assertionValue = new BinaryValue( Strings.EMPTY_BYTES );
86              }
87  
88              assertion.setAssertionValue( assertionValue );
89          }
90          else
91          {
92              if ( tlv.getLength() != 0 )
93              {
94                  assertionValue = new StringValue( Strings.utf8ToString( tlv.getValue().getData() ) );
95              }
96              else
97              {
98                  assertionValue = new StringValue( "" );
99              }
100 
101             assertion.setAssertionValue( assertionValue );
102         }
103 
104         // We now have to get back to the nearest filter which is
105         // not terminal.
106         searchRequestDecorator.unstackFilters( container );
107 
108         if ( IS_DEBUG )
109         {
110             LOG.debug( "Initialize Assertion Value filter" );
111         }
112     }
113 }