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 *    http://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
025
026import org.apache.directory.api.ldap.codec.api.LdapApiService;
027import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
028import org.apache.directory.api.ldap.codec.api.LdapEncoder;
029import org.apache.directory.api.ldap.model.constants.Loggers;
030import org.apache.directory.api.ldap.model.message.Message;
031import org.apache.directory.api.util.Strings;
032import org.apache.mina.core.buffer.IoBuffer;
033import org.apache.mina.core.session.IoSession;
034import org.apache.mina.filter.codec.ProtocolEncoder;
035import org.apache.mina.filter.codec.ProtocolEncoderOutput;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039
040/**
041 * A LDAP message encoder. It is based on api-ldap encoder.
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class LdapProtocolEncoder implements ProtocolEncoder
046{
047    /** logger for reporting errors that might not be handled properly upstream */
048    private static final Logger CODEC_LOG = LoggerFactory.getLogger( Loggers.CODEC_LOG.getName() );
049
050    /** A speedup for logger */
051    private static final boolean IS_DEBUG = CODEC_LOG.isDebugEnabled();
052
053    /** The stateful encoder */
054    private LdapEncoder encoder;
055
056
057    /**
058     * Creates a new instance of LdapProtocolEncoder.
059     */
060    public LdapProtocolEncoder()
061    {
062        this( LdapApiServiceFactory.getSingleton() );
063    }
064
065    /**
066     * Creates a new instance of LdapProtocolEncoder.
067     *
068     * @param ldapApiService The Service to use
069     */
070    public LdapProtocolEncoder( LdapApiService ldapApiService )
071    {
072        this.encoder = new LdapEncoder( ldapApiService );
073    }
074
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception
081    {
082        ByteBuffer buffer = encoder.encodeMessage( ( Message ) message );
083
084        IoBuffer ioBuffer = IoBuffer.wrap( buffer );
085
086        if ( IS_DEBUG )
087        {
088            byte[] dumpBuffer = new byte[buffer.limit()];
089            buffer.get( dumpBuffer );
090            buffer.flip();
091            CODEC_LOG.debug( "Encoded message \n " + message + "\n : " + Strings.dumpBytes( dumpBuffer ) );
092        }
093
094        out.write( ioBuffer );
095    }
096
097
098    /**
099     * {@inheritDoc}
100     */
101    @Override
102    public void dispose( IoSession session ) throws Exception
103    {
104        // Nothing to do
105    }
106}