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.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.i18n.I18n;
27  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
28  import org.apache.directory.api.ldap.codec.api.ResponseCarryingException;
29  import org.apache.directory.api.ldap.codec.decorators.CompareRequestDecorator;
30  import org.apache.directory.api.ldap.model.message.CompareRequest;
31  import org.apache.directory.api.ldap.model.message.CompareResponseImpl;
32  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
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 store the AssertionValue attributeDescription in a Compare Request
40   * <pre>
41   * CompareRequest ::= [APPLICATION 14] SEQUENCE {
42   *     ...
43   *     ava AttributeValueAssertion }
44   *
45   * AttributeValueAssertion ::= SEQUENCE {
46   *     attributeDesc   AttributeDescription,
47   *     ...
48   * </pre>
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  public class StoreCompareRequestAttributeDesc extends GrammarAction<LdapMessageContainer<CompareRequestDecorator>>
52  {
53      /** The logger */
54      private static final Logger LOG = LoggerFactory.getLogger( StoreCompareRequestAttributeDesc.class );
55  
56      /** Speedup for logs */
57      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
58  
59  
60      /**
61       * Instantiates a new action.
62       */
63      public StoreCompareRequestAttributeDesc()
64      {
65          super( "Store CompareRequest assertion description" );
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void action( LdapMessageContainer<CompareRequestDecorator> container ) throws DecoderException
73      {
74          // Get the CompareRequest Object
75          CompareRequest compareRequest = container.getMessage();
76  
77          // Get the Value and store it in the CompareRequest
78          TLV tlv = container.getCurrentTLV();
79  
80          // We have to handle the special case of a 0 length matched
81          // Dn
82          if ( tlv.getLength() == 0 )
83          {
84              String msg = I18n.err( I18n.ERR_04093 );
85              LOG.error( msg );
86              CompareResponseImpl response = new CompareResponseImpl( compareRequest.getMessageId() );
87  
88              throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX,
89                  compareRequest.getName(), null );
90          }
91  
92          String type = Strings.utf8ToString( tlv.getValue().getData() );
93          compareRequest.setAttributeId( type );
94  
95          if ( IS_DEBUG )
96          {
97              LOG.debug( "Comparing attribute description {}", compareRequest.getAttributeId() );
98          }
99      }
100 }