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.LdapCodecConstants;
31  import org.apache.directory.api.ldap.model.message.SearchResultDone;
32  
33  
34  /**
35   * A decorator for the SearchResultDone message
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class SearchResultDoneDecorator extends ResponseDecorator<SearchResultDone> implements SearchResultDone
40  {
41      /** The encoded searchResultDone length */
42      private int searchResultDoneLength;
43  
44  
45      /**
46       * Makes a SearchResultDone encodable.
47       *
48       * @param codec The LDAP service instance
49       * @param decoratedMessage the decorated SearchResultDone
50       */
51      public SearchResultDoneDecorator( LdapApiService codec, SearchResultDone decoratedMessage )
52      {
53          super( codec, decoratedMessage );
54      }
55  
56  
57      //-------------------------------------------------------------------------
58      // The Decorator methods
59      //-------------------------------------------------------------------------
60  
61      /**
62       * Compute the SearchResultDone length 
63       * <br>
64       * SearchResultDone : 
65       * <pre>
66       * 0x65 L1 
67       *   | 
68       *   +--&gt; LdapResult 
69       *   
70       * L1 = Length(LdapResult) 
71       * Length(SearchResultDone) = Length(0x65) + Length(L1) + L1
72       * </pre>
73       * 
74       */
75      @Override
76      public int computeLength()
77      {
78          searchResultDoneLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
79  
80          return 1 + TLV.getNbBytes( searchResultDoneLength ) + searchResultDoneLength;
81      }
82  
83  
84      /**
85       * Encode the SearchResultDone message to a PDU.
86       * 
87       * @param buffer The buffer where to put the PDU
88       * return The encoded response
89       * @throws EncoderException If the encoding failed
90       */
91      @Override
92      public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
93      {
94          try
95          {
96              // The searchResultDone Tag
97              buffer.put( LdapCodecConstants.SEARCH_RESULT_DONE_TAG );
98              buffer.put( TLV.getBytes( searchResultDoneLength ) );
99  
100             // The LdapResult
101             ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
102         }
103         catch ( BufferOverflowException boe )
104         {
105             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
106         }
107 
108         return buffer;
109     }
110 }