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