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.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 codec The LDAP service instance
49       * @param decoratedMessage the decorated BindResponse
50       */
51      public BindResponseDecorator( LdapApiService codec, BindResponse decoratedMessage )
52      {
53          super( codec, decoratedMessage );
54      }
55  
56  
57      //-------------------------------------------------------------------------
58      // The BindResponse methods
59      //-------------------------------------------------------------------------
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public byte[] getServerSaslCreds()
66      {
67          return getDecorated().getServerSaslCreds();
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public void setServerSaslCreds( byte[] serverSaslCreds )
76      {
77          getDecorated().setServerSaslCreds( serverSaslCreds );
78      }
79  
80  
81      //-------------------------------------------------------------------------
82      // The Decorator methods
83      //-------------------------------------------------------------------------
84      /**
85       * Compute the BindResponse length 
86       * <br>
87       * BindResponse : 
88       * <pre>
89       * 0x61 L1 
90       *   | 
91       *   +--&gt; LdapResult
92       *   +--&gt; [serverSaslCreds] 
93       *   
94       * L1 = Length(LdapResult) [ + Length(serverSaslCreds) ] 
95       * Length(BindResponse) = Length(0x61) + Length(L1) + L1
96       * </pre>
97       */
98      @Override
99      public int computeLength()
100     {
101         BindResponse bindResponse = getDecorated();
102         int ldapResultLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
103 
104         bindResponseLength = ldapResultLength;
105 
106         byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
107 
108         if ( serverSaslCreds != null )
109         {
110             bindResponseLength += 1 + TLV.getNbBytes( serverSaslCreds.length ) + serverSaslCreds.length;
111         }
112 
113         return 1 + TLV.getNbBytes( bindResponseLength ) + bindResponseLength;
114     }
115 
116 
117     /**
118      * Encode the BindResponse message to a PDU.
119      * <br>
120      * BindResponse :
121      * <pre>
122      * LdapResult.encode 
123      * [0x87 LL serverSaslCreds]
124      * </pre>
125      * 
126      * @param buffer The buffer where to put the PDU
127      * @return The encoded response
128      * @throws EncoderException when encoding operations fail
129      */
130     @Override
131     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
132     {
133         BindResponse bindResponse = getDecorated();
134 
135         try
136         {
137             // The BindResponse Tag
138             buffer.put( LdapCodecConstants.BIND_RESPONSE_TAG );
139             buffer.put( TLV.getBytes( bindResponseLength ) );
140 
141             // The LdapResult
142             ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
143 
144             // The serverSaslCredential, if any
145             byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
146 
147             if ( serverSaslCreds != null )
148             {
149                 buffer.put( ( byte ) LdapCodecConstants.SERVER_SASL_CREDENTIAL_TAG );
150 
151                 buffer.put( TLV.getBytes( serverSaslCreds.length ) );
152 
153                 if ( serverSaslCreds.length != 0 )
154                 {
155                     buffer.put( serverSaslCreds );
156                 }
157             }
158         }
159         catch ( BufferOverflowException boe )
160         {
161             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
162         }
163 
164         return buffer;
165     }
166 }