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