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.BindResponse;
32  
33  
34  /**
35   * A decorator for the BindResponse message
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class BindResponseDecorator extends ResponseDecorator<BindResponse> implements BindResponse
40  {
41      /** The encoded bindResponse length */
42      private int bindResponseLength;
43  
44  
45      /**
46       * Makes a BindResponse a MessageDecorator.
47       *
48       * @param decoratedMessage the decorated BindResponse
49       */
50      public BindResponseDecorator( LdapApiService codec, BindResponse decoratedMessage )
51      {
52          super( codec, decoratedMessage );
53      }
54  
55  
56      /**
57       * Stores the encoded length for the BindResponse
58       * @param bindResponseLength The encoded length
59       */
60      public void setBindResponseLength( int bindResponseLength )
61      {
62          this.bindResponseLength = bindResponseLength;
63      }
64  
65  
66      /**
67       * @return The encoded BindResponse's length
68       */
69      public int getBindResponseLength()
70      {
71          return bindResponseLength;
72      }
73  
74  
75      //-------------------------------------------------------------------------
76      // The BindResponse methods
77      //-------------------------------------------------------------------------
78  
79      /**
80       * {@inheritDoc}
81       */
82      public byte[] getServerSaslCreds()
83      {
84          return getDecorated().getServerSaslCreds();
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      public void setServerSaslCreds( byte[] serverSaslCreds )
92      {
93          getDecorated().setServerSaslCreds( serverSaslCreds );
94      }
95  
96  
97      //-------------------------------------------------------------------------
98      // The Decorator methods
99      //-------------------------------------------------------------------------
100     /**
101      * Compute the BindResponse length 
102      * 
103      * BindResponse : 
104      * <pre>
105      * 0x61 L1 
106      *   | 
107      *   +--> LdapResult
108      *   +--> [serverSaslCreds] 
109      *   
110      * L1 = Length(LdapResult) [ + Length(serverSaslCreds) ] 
111      * Length(BindResponse) = Length(0x61) + Length(L1) + L1
112      * </pre>
113      */
114     public int computeLength()
115     {
116         BindResponse bindResponse = getDecorated();
117         int ldapResultLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
118 
119         int bindResponseLength = ldapResultLength;
120 
121         byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
122 
123         if ( serverSaslCreds != null )
124         {
125             bindResponseLength += 1 + TLV.getNbBytes( serverSaslCreds.length ) + serverSaslCreds.length;
126         }
127 
128         setBindResponseLength( bindResponseLength );
129 
130         return 1 + TLV.getNbBytes( bindResponseLength ) + bindResponseLength;
131     }
132 
133 
134     /**
135      * Encode the BindResponse message to a PDU.
136      * 
137      * BindResponse :
138      * <pre>
139      * LdapResult.encode 
140      * [0x87 LL serverSaslCreds]
141      * </pre>
142      * 
143      * @param bb The buffer where to put the PDU
144      * @param bindResponseDecorator The decorated BindResponse to encode
145      * @throws EncoderException when encoding operations fail
146      */
147     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
148     {
149         BindResponse bindResponse = getDecorated();
150 
151         try
152         {
153             // The BindResponse Tag
154             buffer.put( LdapConstants.BIND_RESPONSE_TAG );
155             buffer.put( TLV.getBytes( getBindResponseLength() ) );
156 
157             // The LdapResult
158             ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
159 
160             // The serverSaslCredential, if any
161             byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
162 
163             if ( serverSaslCreds != null )
164             {
165                 buffer.put( ( byte ) LdapConstants.SERVER_SASL_CREDENTIAL_TAG );
166 
167                 buffer.put( TLV.getBytes( serverSaslCreds.length ) );
168 
169                 if ( serverSaslCreds.length != 0 )
170                 {
171                     buffer.put( serverSaslCreds );
172                 }
173             }
174         }
175         catch ( BufferOverflowException boe )
176         {
177             throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
178         }
179 
180         return buffer;
181     }
182 }