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