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