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       * @param codec The LDAP codec service associated with this encoder.
61       */
62      public LdapProtocolEncoder()
63      {
64          this( LdapApiServiceFactory.getSingleton() );
65      }
66  
67      public LdapProtocolEncoder( LdapApiService ldapApiService )
68      {
69          this.encoder = new LdapEncoder( ldapApiService );
70      }
71  
72  
73      /**
74       * {@inheritDoc}
75       */
76      public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception
77      {
78          ByteBuffer buffer = encoder.encodeMessage( ( Message ) message );
79  
80          IoBuffer ioBuffer = IoBuffer.wrap( buffer );
81  
82          if ( IS_DEBUG )
83          {
84              byte[] dumpBuffer = new byte[buffer.limit()];
85              buffer.get( dumpBuffer );
86              buffer.flip();
87              CODEC_LOG.debug( "Encoded message \n " + message + "\n : " + Strings.dumpBytes( dumpBuffer ) );
88          }
89  
90          out.write( ioBuffer );
91      }
92  
93  
94      /**
95       * {@inheritDoc}
96       */
97      public void dispose( IoSession session ) throws Exception
98      {
99          // Nothing to do
100     }
101 }