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.api;
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.decorators.LdapResultDecorator;
30  import org.apache.directory.api.ldap.codec.decorators.ResponseDecorator;
31  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
32  import org.apache.directory.api.util.Strings;
33  
34  
35  /**
36   * A decorator for the ExtendedResponse message
37   *
38   * @param <R> The extended response to decorate
39   * 
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class ExtendedResponseDecorator<R extends ExtendedResponse> extends ResponseDecorator<R>
43      implements ExtendedResponse
44  {
45      /** The response name (OID) as a byte[] */
46      private byte[] responseNameBytes;
47  
48      /** The encoded extendedResponse length */
49      private int extendedResponseLength;
50  
51      /** The response value */
52      protected byte[] responseValue;
53  
54  
55      /**
56       * Makes a ExtendedResponse encodable.
57       *
58       * @param codec The LDAP service instance
59       * @param decoratedMessage the decorated ExtendedResponse
60       */
61      public ExtendedResponseDecorator( LdapApiService codec, R decoratedMessage )
62      {
63          super( codec, decoratedMessage );
64      }
65  
66  
67      //-------------------------------------------------------------------------
68      // The ExtendedResponse methods
69      //-------------------------------------------------------------------------
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public String getResponseName()
76      {
77          return getDecorated().getResponseName();
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public void setResponseName( String oid )
86      {
87          getDecorated().setResponseName( oid );
88      }
89  
90  
91      /**
92       * Gets the Extended response payload 
93       * 
94       * @return The extended payload
95       */
96      public byte[] getResponseValue()
97      {
98          return responseValue;
99      }
100 
101 
102     /**
103      * sets the Extended response payload 
104      * 
105      * @param responseValue The extended payload
106      */
107     public void setResponseValue( byte[] responseValue )
108     {
109         this.responseValue = responseValue;
110     }
111 
112 
113     //-------------------------------------------------------------------------
114     // The Decorator methods
115     //-------------------------------------------------------------------------
116     /**
117      * Compute the ExtendedResponse length
118      * <br>
119      * ExtendedResponse :
120      * <pre>
121      * 0x78 L1
122      *  |
123      *  +--&gt; LdapResult
124      * [+--&gt; 0x8A L2 name
125      * [+--&gt; 0x8B L3 response]]
126      * 
127      * L1 = Length(LdapResult)
128      *      [ + Length(0x8A) + Length(L2) + L2
129      *       [ + Length(0x8B) + Length(L3) + L3]]
130      * 
131      * Length(ExtendedResponse) = Length(0x78) + Length(L1) + L1
132      * </pre>
133      * 
134      * @return The ExtendedResponse length
135      */
136     @Override
137     public int computeLength()
138     {
139         int ldapResultLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
140 
141         extendedResponseLength = ldapResultLength;
142 
143         String id = getResponseName();
144 
145         if ( !Strings.isEmpty( id ) )
146         {
147             responseNameBytes = Strings.getBytesUtf8( id );
148             int idLength = responseNameBytes.length;
149             extendedResponseLength += 1 + TLV.getNbBytes( idLength ) + idLength;
150         }
151 
152         byte[] encodedValue = getResponseValue();
153 
154         if ( encodedValue != null )
155         {
156             extendedResponseLength += 1 + TLV.getNbBytes( encodedValue.length ) + encodedValue.length;
157         }
158 
159         return 1 + TLV.getNbBytes( extendedResponseLength ) + extendedResponseLength;
160     }
161 
162 
163     /**
164      * Encode the ExtendedResponse message to a PDU. 
165      * <br>
166      * ExtendedResponse :
167      * <pre>
168      * LdapResult.encode()
169      * [0x8A LL response name]
170      * [0x8B LL response]
171      * </pre>
172      * 
173      * @param buffer The buffer where to put the PDU
174      * @return The PDU.
175      */
176     @Override
177     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
178     {
179         try
180         {
181             // The ExtendedResponse Tag
182             buffer.put( LdapCodecConstants.EXTENDED_RESPONSE_TAG );
183             buffer.put( TLV.getBytes( extendedResponseLength ) );
184 
185             // The LdapResult
186             ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
187 
188             // The ID, if any
189             if ( responseNameBytes != null )
190             {
191                 buffer.put( ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_RESPONSE_NAME_TAG );
192                 buffer.put( TLV.getBytes( responseNameBytes.length ) );
193 
194                 if ( responseNameBytes.length != 0 )
195                 {
196                     buffer.put( responseNameBytes );
197                 }
198             }
199 
200             // The encodedValue, if any
201             byte[] encodedValue = getResponseValue();
202 
203             if ( encodedValue != null )
204             {
205                 buffer.put( ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_RESPONSE_TAG );
206 
207                 buffer.put( TLV.getBytes( encodedValue.length ) );
208 
209                 if ( encodedValue.length != 0 )
210                 {
211                     buffer.put( encodedValue );
212                 }
213             }
214         }
215         catch ( BufferOverflowException boe )
216         {
217             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
218         }
219 
220         return buffer;
221     }
222 }