View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.codec.protocol.mina;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  
26  import org.apache.directory.api.ldap.codec.api.LdapApiService;
27  import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
28  import org.apache.directory.api.ldap.codec.api.LdapEncoder;
29  import org.apache.directory.api.ldap.model.constants.Loggers;
30  import org.apache.directory.api.ldap.model.message.Message;
31  import org.apache.directory.api.util.Strings;
32  import org.apache.mina.core.buffer.IoBuffer;
33  import org.apache.mina.core.session.IoSession;
34  import org.apache.mina.filter.codec.ProtocolEncoder;
35  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * A LDAP message encoder. It is based on api-ldap encoder.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class LdapProtocolEncoder implements ProtocolEncoder
46  {
47      /** logger for reporting errors that might not be handled properly upstream */
48      private static final Logger CODEC_LOG = LoggerFactory.getLogger( Loggers.CODEC_LOG.getName() );
49  
50      /** A speedup for logger */
51      private static final boolean IS_DEBUG = CODEC_LOG.isDebugEnabled();
52  
53      /** The stateful encoder */
54      private LdapEncoder encoder;
55  
56  
57      /**
58       * Creates a new instance of LdapProtocolEncoder.
59       */
60      public LdapProtocolEncoder()
61      {
62          this( LdapApiServiceFactory.getSingleton() );
63      }
64  
65      /**
66       * Creates a new instance of LdapProtocolEncoder.
67       *
68       * @param ldapApiService The Service to use
69       */
70      public LdapProtocolEncoder( LdapApiService ldapApiService )
71      {
72          this.encoder = new LdapEncoder( ldapApiService );
73      }
74  
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception
81      {
82          ByteBuffer buffer = encoder.encodeMessage( ( Message ) message );
83  
84          IoBuffer ioBuffer = IoBuffer.wrap( buffer );
85  
86          if ( IS_DEBUG )
87          {
88              byte[] dumpBuffer = new byte[buffer.limit()];
89              buffer.get( dumpBuffer );
90              buffer.flip();
91              CODEC_LOG.debug( "Encoded message \n " + message + "\n : " + Strings.dumpBytes( dumpBuffer ) );
92          }
93  
94          out.write( ioBuffer );
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public void dispose( IoSession session ) throws Exception
103     {
104         // Nothing to do
105     }
106 }