001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.codec.factory;
021
022import org.apache.directory.api.asn1.ber.tlv.BerValue;
023import org.apache.directory.api.asn1.util.Asn1Buffer;
024import org.apache.directory.api.ldap.codec.api.LdapApiService;
025import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
026import org.apache.directory.api.ldap.model.message.BindRequest;
027import org.apache.directory.api.ldap.model.message.Message;
028import org.apache.directory.api.ldap.model.name.Dn;
029import org.apache.directory.api.util.Strings;
030
031/**
032 * The BindRequest factory.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public final class BindRequestFactory implements Messagefactory
037{
038    /** The static instance */
039    public static final BindRequestFactory INSTANCE = new BindRequestFactory();
040
041    private BindRequestFactory()
042    {
043        // Nothing to do
044    }
045
046    /**
047     * Encode the BindRequest message to a PDU.
048     * <br>
049     * BindRequest :
050     * <pre>
051     * 0x60 LL
052     *   0x02 LL version         0x80 LL simple
053     *   0x04 LL name           /
054     *   authentication.encode()
055     *                          \ 0x83 LL 0x04 LL mechanism [0x04 LL credential]
056     * </pre>
057     *
058     * @param codec The LdapApiService instance
059     * @param buffer The buffer where to put the PDU
060     * @param message the BindRequest to encode
061     */
062    @Override
063    public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
064    {
065        int start = buffer.getPos();
066        BindRequest bindMessage = ( BindRequest ) message;
067
068        // The authentication
069        if ( bindMessage.isSimple() )
070        {
071            // Simple authentication
072            BerValue.encodeOctetString( buffer, ( byte ) LdapCodecConstants.BIND_REQUEST_SIMPLE_TAG,
073                bindMessage.getCredentials() );
074        }
075        else
076        {
077            // SASL Bind
078            // The credentials, if any
079            if ( !Strings.isEmpty( bindMessage.getCredentials() ) )
080            {
081                BerValue.encodeOctetString( buffer, bindMessage.getCredentials() );
082            }
083
084            // The mechanism
085            BerValue.encodeOctetString( buffer, bindMessage.getSaslMechanism() );
086
087            // The SASL tag
088            BerValue.encodeSequence( buffer, ( byte ) LdapCodecConstants.BIND_REQUEST_SASL_TAG, start );
089        }
090
091        // The name
092        Dn dn = bindMessage.getDn();
093
094        if ( !Dn.isNullOrEmpty( dn ) )
095        {
096            // A DN has been provided
097            BerValue.encodeOctetString( buffer, dn.getName() );
098        }
099        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}