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