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.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 codec The LDAP service instance
49       * @param decoratedMessage the decorated AddResponse
50       */
51      public AddResponseDecorator( LdapApiService codec, AddResponse decoratedMessage )
52      {
53          super( codec, decoratedMessage );
54      }
55  
56  
57      /**
58       * @return The decorated AddResponse
59       */
60      public AddResponse getAddResponse()
61      {
62          return getDecorated();
63      }
64  
65  
66      //-------------------------------------------------------------------------
67      // The Decorator methods
68      //-------------------------------------------------------------------------
69      /**
70       * Compute the AddResponse length 
71       * <br>
72       * AddResponse : 
73       * <pre>
74       * 0x69 L1
75       *  |
76       *  +--&gt; LdapResult
77       * 
78       * L1 = Length(LdapResult)
79       * 
80       * Length(AddResponse) = Length(0x69) + Length(L1) + L1
81       * </pre>
82       */
83      @Override
84      public int computeLength()
85      {
86          AddResponse addResponse = getAddResponse();
87          setLdapResult( new LdapResultDecorator( getCodecService(), addResponse.getLdapResult() ) );
88          addResponseLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
89  
90          return 1 + TLV.getNbBytes( addResponseLength ) + addResponseLength;
91      }
92  
93  
94      /**
95       * Encode the AddResponse message to a PDU.
96       * 
97       * @param buffer The buffer where to put the PDU
98       * @return The encoded response
99       * @throws EncoderException If teh encoding failed
100      */
101     @Override
102     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
103     {
104         try
105         {
106             // The AddResponse Tag
107             buffer.put( LdapCodecConstants.ADD_RESPONSE_TAG );
108             buffer.put( TLV.getBytes( addResponseLength ) );
109 
110             // The LdapResult
111             ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
112 
113             return buffer;
114         }
115         catch ( BufferOverflowException boe )
116         {
117             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
118         }
119     }
120 }