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   *    https://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.factory;
21  
22  import org.apache.directory.api.asn1.ber.tlv.BerValue;
23  import org.apache.directory.api.asn1.util.Asn1Buffer;
24  import org.apache.directory.api.ldap.codec.api.LdapApiService;
25  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
26  import org.apache.directory.api.ldap.model.message.BindRequest;
27  import org.apache.directory.api.ldap.model.message.Message;
28  import org.apache.directory.api.ldap.model.name.Dn;
29  import org.apache.directory.api.util.Strings;
30  
31  /**
32   * The BindRequest factory.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public final class BindRequestFactory implements Messagefactory
37  {
38      /** The static instance */
39      public static final BindRequestFactory INSTANCE = new BindRequestFactory();
40  
41      private BindRequestFactory()
42      {
43          // Nothing to do
44      }
45  
46      /**
47       * Encode the BindRequest message to a PDU.
48       * <br>
49       * BindRequest :
50       * <pre>
51       * 0x60 LL
52       *   0x02 LL version         0x80 LL simple
53       *   0x04 LL name           /
54       *   authentication.encode()
55       *                          \ 0x83 LL 0x04 LL mechanism [0x04 LL credential]
56       * </pre>
57       *
58       * @param codec The LdapApiService instance
59       * @param buffer The buffer where to put the PDU
60       * @param message the BindRequest to encode
61       */
62      @Override
63      public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
64      {
65          int start = buffer.getPos();
66          BindRequest bindMessage = ( BindRequest ) message;
67  
68          // The authentication
69          if ( bindMessage.isSimple() )
70          {
71              // Simple authentication
72              BerValue.encodeOctetString( buffer, ( byte ) LdapCodecConstants.BIND_REQUEST_SIMPLE_TAG,
73                  bindMessage.getCredentials() );
74          }
75          else
76          {
77              // SASL Bind
78              // The credentials, if any
79              if ( !Strings.isEmpty( bindMessage.getCredentials() ) )
80              {
81                  BerValue.encodeOctetString( buffer, bindMessage.getCredentials() );
82              }
83  
84              // The mechanism
85              BerValue.encodeOctetString( buffer, bindMessage.getSaslMechanism() );
86  
87              // The SASL tag
88              BerValue.encodeSequence( buffer, ( byte ) LdapCodecConstants.BIND_REQUEST_SASL_TAG, start );
89          }
90  
91          // The name
92          Dn dn = bindMessage.getDn();
93  
94          if ( !Dn.isNullOrEmpty( dn ) )
95          {
96              // A DN has been provided
97              BerValue.encodeOctetString( buffer, dn.getName() );
98          }
99          else
100         {
101             // No DN has been provided, let's use the name as a string instead
102             BerValue.encodeOctetString( buffer, bindMessage.getName() );
103         }
104 
105         // The version (LDAP V3 only)
106         BerValue.encodeInteger( buffer, 3 );
107 
108         // The BindRequest Tag
109         BerValue.encodeSequence( buffer, LdapCodecConstants.BIND_REQUEST_TAG, start );
110     }
111 }