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.protocol.mina;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.EncoderException;
026import org.apache.directory.api.asn1.util.Asn1Buffer;
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.codec.api.LdapApiService;
029import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
030import org.apache.directory.api.ldap.codec.api.LdapEncoder;
031import org.apache.directory.api.ldap.model.constants.Loggers;
032import org.apache.directory.api.ldap.model.message.Message;
033import org.apache.directory.api.util.Strings;
034import org.apache.mina.core.buffer.IoBuffer;
035import org.apache.mina.core.session.IoSession;
036import org.apache.mina.filter.codec.ProtocolEncoder;
037import org.apache.mina.filter.codec.ProtocolEncoderOutput;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041
042/**
043 * A LDAP message encoder. It is based on api-ldap encoder.
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public class LdapProtocolEncoder implements ProtocolEncoder
048{
049    /** logger for reporting errors that might not be handled properly upstream */
050    private static final Logger CODEC_LOG = LoggerFactory.getLogger( Loggers.CODEC_LOG.getName() );
051
052    /** The LDAP API Service instance */
053    private LdapApiService codec;
054    
055    /** A thread local storage used to store the Asn1Buffer instance */
056    private ThreadLocal<Asn1Buffer> threadLocalStorage = new ThreadLocal<>();
057    
058    /**
059     * Creates a new instance of LdapProtocolEncoder.
060     */
061    public LdapProtocolEncoder()
062    {
063        this( LdapApiServiceFactory.getSingleton() );
064    }
065
066    /**
067     * Creates a new instance of LdapProtocolEncoder.
068     *
069     * @param ldapApiService The Service to use
070     */
071    public LdapProtocolEncoder( LdapApiService ldapApiService )
072    {
073        codec = ldapApiService;
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception
082    {
083        Asn1Buffer asn1Buffer = threadLocalStorage.get();
084        
085        if ( asn1Buffer == null )
086        {
087            asn1Buffer = new Asn1Buffer();
088            threadLocalStorage.set( asn1Buffer );
089        }
090
091        ByteBuffer encoded;
092        
093        try
094        { 
095            LdapEncoder.encodeMessage( asn1Buffer, codec, ( Message ) message );
096            encoded = asn1Buffer.getBytes();
097        }
098        catch ( EncoderException e )
099        {
100            CODEC_LOG.error( I18n.err( I18n.ERR_14000_ERROR_ENCODING_MESSAGE, message, e.getMessage() ) );
101            throw e;
102        }
103        finally 
104        {
105            asn1Buffer.clear();
106        }
107        
108        IoBuffer ioBuffer = IoBuffer.wrap( encoded );
109    
110        if ( CODEC_LOG.isDebugEnabled() )
111        {
112            byte[] dumpBuffer = new byte[encoded.limit()];
113            encoded.get( dumpBuffer );
114            encoded.flip();
115            CODEC_LOG.debug( I18n.msg( I18n.MSG_14003_ENCODED_LDAP_MESSAGE, message, Strings.dumpBytes( dumpBuffer ) ) );
116        }
117
118        out.write( ioBuffer );
119    }
120
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public void dispose( IoSession session ) throws Exception
127    {
128        // Nothing to do
129    }
130}