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.api;
21
22
23 import java.io.InputStream;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.api.asn1.DecoderException;
27 import org.apache.directory.api.asn1.ber.Asn1Decoder;
28 import org.apache.directory.api.asn1.ber.tlv.TLVStateEnum;
29 import org.apache.directory.api.i18n.I18n;
30 import org.apache.directory.api.ldap.model.message.Message;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40 public class LdapDecoder
41 {
42
43 private static final Logger LOG = LoggerFactory.getLogger( LdapDecoder.class );
44
45
46 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
47
48
49 private Asn1Decoder asn1Decoder;
50
51
52 public static final String MESSAGE_CONTAINER_ATTR = "LDAP-container";
53
54
55 public static final String MAX_PDU_SIZE_ATTR = "LDAP-maxPduSize";
56
57
58
59
60
61 public LdapDecoder()
62 {
63 asn1Decoder = new Asn1Decoder();
64 }
65
66
67
68
69
70
71
72
73
74
75
76 public Message decode( InputStream in, LdapMessageContainer<MessageDecorator<? extends Message>> container )
77 throws DecoderException
78 {
79 try
80 {
81 int amount;
82
83 while ( in.available() > 0 )
84 {
85 byte[] buf = new byte[in.available()];
86
87 amount = in.read( buf );
88 if ( amount == -1 )
89 {
90 break;
91 }
92
93 asn1Decoder.decode( ByteBuffer.wrap( buf, 0, amount ), container );
94 }
95 }
96 catch ( Exception e )
97 {
98 String message = I18n.err( I18n.ERR_04060, e.getLocalizedMessage() );
99 LOG.error( message );
100 throw new DecoderException( message, e );
101 }
102
103 if ( container.getState() == TLVStateEnum.PDU_DECODED )
104 {
105 if ( IS_DEBUG )
106 {
107 LOG.debug( "Decoded LdapMessage : " + container );
108 }
109
110 return container.getMessage();
111 }
112 else
113 {
114 LOG.error( I18n.err( I18n.ERR_04062 ) );
115 throw new DecoderException( I18n.err( I18n.ERR_04063 ) );
116 }
117 }
118 }