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.api;
021
022
023import java.io.InputStream;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.DecoderException;
027import org.apache.directory.api.asn1.ber.Asn1Decoder;
028import org.apache.directory.api.asn1.ber.tlv.TLVStateEnum;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.model.message.Message;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * The LdapDecoder decodes ASN.1 BER encoded PDUs into LDAP messages
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class LdapDecoder
041{
042    /** The logger */
043    private static final Logger LOG = LoggerFactory.getLogger( LdapDecoder.class );
044
045    /** The name of the LdapSession's attribute for the LDAP container used during the decoding */
046    public static final String MESSAGE_CONTAINER_ATTR = "LDAP-container";
047
048    /** The maximum PDU size, stored into the LDAPSession's attribute */
049    public static final String MAX_PDU_SIZE_ATTR = "LDAP-maxPduSize";
050
051
052    /**
053     * Creates an instance of a Ldap Decoder implementation.
054     */
055    public LdapDecoder()
056    {
057    }
058
059
060    /**
061     * Decodes a PDU from an input stream into a Ldap message container. We can only
062     * decode one complete message.
063     *
064     * @param in The input stream to read and decode PDU bytes from
065     * @param container The LdapMessageContainer containing the message to decode
066     * @return return The decoded message
067     * @throws DecoderException If the decoding failed
068     */
069    public Message decode( InputStream in, LdapMessageContainer<? extends Message> container )
070        throws DecoderException
071    {
072        try
073        {
074            int amount;
075
076            while ( in.available() > 0 )
077            {
078                byte[] buf = new byte[in.available()];
079
080                amount = in.read( buf );
081                
082                if ( amount == -1 )
083                {
084                    break;
085                }
086
087                Asn1Decoder.decode( ByteBuffer.wrap( buf, 0, amount ), container );
088            }
089        }
090        catch ( Exception e )
091        {
092            String message = I18n.err( I18n.ERR_05204_LDAP_DECODER_FAILURE, e.getLocalizedMessage() );
093            LOG.error( message );
094            throw new DecoderException( message, e );
095        }
096
097        if ( container.getState() == TLVStateEnum.PDU_DECODED )
098        {
099            if ( LOG.isDebugEnabled() )
100            {
101                LOG.debug( I18n.msg( I18n.MSG_5200_DECODED_LDAP_MESSAGE, container ) );
102            }
103
104            return container.getMessage();
105        }
106        else
107        {
108            LOG.error( I18n.err( I18n.ERR_05205_PDU_DOES_NOT_CONTAIN_ENOUGH_DATA ) );
109            throw new DecoderException( I18n.err( I18n.ERR_05206_INPUT_STREAM_TOO_SHORT_PDU ) );
110        }
111    }
112}