1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
45 public class LdapProtocolEncoder implements ProtocolEncoder
46 {
47
48 private static final Logger CODEC_LOG = LoggerFactory.getLogger( Loggers.CODEC_LOG.getName() );
49
50
51 private static final boolean IS_DEBUG = CODEC_LOG.isDebugEnabled();
52
53
54 private LdapEncoder encoder;
55
56
57
58
59
60
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
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
96
97 public void dispose( IoSession session ) throws Exception
98 {
99
100 }
101 }