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.decorators;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.TLV;
28  import org.apache.directory.api.i18n.I18n;
29  import org.apache.directory.api.ldap.codec.api.LdapApiService;
30  import org.apache.directory.api.ldap.codec.api.LdapConstants;
31  import org.apache.directory.api.ldap.model.message.CompareResponse;
32  
33  
34  /**
35   * A decorator for the CompareResponse message
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class CompareResponseDecorator extends ResponseDecorator<CompareResponse>
40      implements CompareResponse
41  {
42      /** The encoded compareResponse length */
43      private int compareResponseLength;
44  
45  
46      /**
47       * Makes a CompareResponse a MessageDecorator.
48       *
49       * @param decoratedMessage the decorated CompareResponse
50       */
51      public CompareResponseDecorator( LdapApiService codec, CompareResponse decoratedMessage )
52      {
53          super( codec, decoratedMessage );
54      }
55  
56  
57      /**
58       * Stores the encoded length for the CompareResponse
59       * @param compareResponseLength The encoded length
60       */
61      public void setCompareResponseLength( int compareResponseLength )
62      {
63          this.compareResponseLength = compareResponseLength;
64      }
65  
66  
67      /**
68       * @return The encoded CompareResponse's length
69       */
70      public int getCompareResponseLength()
71      {
72          return compareResponseLength;
73      }
74  
75  
76      //-------------------------------------------------------------------------
77      // The CompareResponse methods
78      //-------------------------------------------------------------------------
79  
80      /**
81       * {@inheritDoc}
82       */
83      public boolean isTrue()
84      {
85          return getDecorated().isTrue();
86      }
87  
88  
89      //-------------------------------------------------------------------------
90      // The Decorator methods
91      //-------------------------------------------------------------------------
92  
93      /**
94       * Compute the CompareResponse length 
95       * 
96       * CompareResponse :
97       * 
98       * 0x6F L1
99       *  |
100      *  +--> LdapResult
101      * 
102      * L1 = Length(LdapResult)
103      * 
104      * Length(CompareResponse) = Length(0x6F) + Length(L1) + L1
105      */
106     public int computeLength()
107     {
108         int compareResponseLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
109 
110         setCompareResponseLength( compareResponseLength );
111 
112         return 1 + TLV.getNbBytes( compareResponseLength ) + compareResponseLength;
113     }
114 
115 
116     /**
117      * Encode the CompareResponse message to a PDU.
118      * 
119      * @param buffer The buffer where to put the PDU
120      */
121     public ByteBuffer encode( ByteBuffer buffer )
122         throws EncoderException
123     {
124         try
125         {
126             // The CompareResponse Tag
127             buffer.put( LdapConstants.COMPARE_RESPONSE_TAG );
128             buffer.put( TLV.getBytes( getCompareResponseLength() ) );
129 
130             // The LdapResult
131             ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
132         }
133         catch ( BufferOverflowException boe )
134         {
135             throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
136         }
137 
138         return buffer;
139     }
140 }