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 * @param codec The LDAP codec service associated with this encoder. 061 */ 062 public LdapProtocolEncoder() 063 { 064 this( LdapApiServiceFactory.getSingleton() ); 065 } 066 067 public LdapProtocolEncoder( LdapApiService ldapApiService ) 068 { 069 this.encoder = new LdapEncoder( ldapApiService ); 070 } 071 072 073 /** 074 * {@inheritDoc} 075 */ 076 public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception 077 { 078 ByteBuffer buffer = encoder.encodeMessage( ( Message ) message ); 079 080 IoBuffer ioBuffer = IoBuffer.wrap( buffer ); 081 082 if ( IS_DEBUG ) 083 { 084 byte[] dumpBuffer = new byte[buffer.limit()]; 085 buffer.get( dumpBuffer ); 086 buffer.flip(); 087 CODEC_LOG.debug( "Encoded message \n " + message + "\n : " + Strings.dumpBytes( dumpBuffer ) ); 088 } 089 090 out.write( ioBuffer ); 091 } 092 093 094 /** 095 * {@inheritDoc} 096 */ 097 public void dispose( IoSession session ) throws Exception 098 { 099 // Nothing to do 100 } 101}