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.compareRequest;
21  
22  
23  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
24  import org.apache.directory.api.asn1.ber.tlv.TLV;
25  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
26  import org.apache.directory.api.ldap.codec.decorators.CompareRequestDecorator;
27  import org.apache.directory.api.ldap.model.message.CompareRequest;
28  import org.apache.directory.api.util.Strings;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * The action used to store the AssertionValue in a Compare Request
35   * <pre>
36   * CompareRequest ::= [APPLICATION 14] SEQUENCE {
37   *     ...
38   *     ava AttributeValueAssertion }
39   *
40   * AttributeValueAssertion ::= SEQUENCE {
41   *     ...
42   *     assertionValue AssertionValue }
43   *
44   * AssertionValue OCTET STRING
45   * </pre>
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  public class StoreCompareRequestAssertionValue extends GrammarAction<LdapMessageContainer<CompareRequestDecorator>>
49  {
50      /** The logger */
51      private static final Logger LOG = LoggerFactory.getLogger( StoreCompareRequestAssertionValue.class );
52  
53      /** Speedup for logs */
54      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
55  
56  
57      /**
58       * Instantiates a new action.
59       */
60      public StoreCompareRequestAssertionValue()
61      {
62          super( "Store CompareRequest assertion value" );
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      public void action( LdapMessageContainer<CompareRequestDecorator> container )
70      {
71          // Get the CompareRequest Object
72          CompareRequest compareRequest = container.getMessage();
73  
74          // Get the Value and store it in the CompareRequest
75          TLV tlv = container.getCurrentTLV();
76  
77          // We have to handle the special case of a 0 length value
78          if ( tlv.getLength() == 0 )
79          {
80              compareRequest.setAssertionValue( "" );
81          }
82          else
83          {
84              if ( container.isBinary( compareRequest.getAttributeId() ) )
85              {
86                  compareRequest.setAssertionValue( tlv.getValue().getData() );
87  
88                  if ( IS_DEBUG )
89                  {
90                      LOG.debug( "Comparing attribute value {}", Strings.dumpBytes( compareRequest
91                          .getAssertionValue().getBytes() ) );
92                  }
93              }
94              else
95              {
96                  compareRequest.setAssertionValue( Strings.utf8ToString( tlv.getValue().getData() ) );
97  
98                  if ( LOG.isDebugEnabled() )
99                  {
100                     LOG.debug( "Comparing attribute value {}", compareRequest.getAssertionValue() );
101                 }
102             }
103         }
104 
105         // We can have an END transition
106         container.setGrammarEndAllowed( true );
107     }
108 }