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 public LdapProtocolEncoder()
61 {
62 this( LdapApiServiceFactory.getSingleton() );
63 }
64
65
66
67
68
69
70 public LdapProtocolEncoder( LdapApiService ldapApiService )
71 {
72 this.encoder = new LdapEncoder( ldapApiService );
73 }
74
75
76
77
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
100
101 @Override
102 public void dispose( IoSession session ) throws Exception
103 {
104
105 }
106 }