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.factory;
21
22 import java.util.Collection;
23 import java.util.Iterator;
24
25 import org.apache.directory.api.asn1.ber.tlv.BerValue;
26 import org.apache.directory.api.asn1.util.Asn1Buffer;
27 import org.apache.directory.api.ldap.codec.api.LdapApiService;
28 import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
29 import org.apache.directory.api.ldap.model.message.LdapResult;
30 import org.apache.directory.api.ldap.model.message.Message;
31 import org.apache.directory.api.ldap.model.message.Referral;
32 import org.apache.directory.api.ldap.model.message.ResultResponse;
33
34
35
36
37
38
39 public abstract class ResponseFactory implements Messagefactory
40 {
41
42 private static final byte[] DEFAULT_SUCCESS = new byte[]
43 { 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };
44
45 private static final byte[] EMPTY_MATCHED_DN = new byte[]
46 { 0x04, 0x00 };
47
48
49
50
51 protected ResponseFactory()
52 {
53
54 }
55
56
57
58
59
60
61
62
63 protected void encodeReferralUrls( Asn1Buffer buffer, Iterator<String> urls )
64 {
65 if ( urls.hasNext() )
66 {
67 String url = urls.next();
68
69 encodeReferralUrls( buffer, urls );
70
71 BerValue.encodeOctetString( buffer, url );
72 }
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 protected void encodeLdapResultReverse( Asn1Buffer buffer, LdapResult ldapResult )
98 {
99 if ( ldapResult.isDefaultSuccess() )
100 {
101
102 buffer.put( DEFAULT_SUCCESS );
103
104 return;
105 }
106
107
108 Referral referral = ldapResult.getReferral();
109
110 if ( referral != null )
111 {
112 int start = buffer.getPos();
113 Collection<String> urls = referral.getLdapUrls();
114
115 if ( urls != null )
116 {
117 encodeReferralUrls( buffer, urls.iterator() );
118 }
119
120
121 BerValue.encodeSequence( buffer, ( byte ) LdapCodecConstants.LDAP_RESULT_REFERRAL_SEQUENCE_TAG, start );
122 }
123
124
125 BerValue.encodeOctetString( buffer, ldapResult.getDiagnosticMessage() );
126
127
128 if ( ldapResult.getMatchedDn() != null )
129 {
130 BerValue.encodeOctetString( buffer, ldapResult.getMatchedDn().getName() );
131 }
132 else
133 {
134 buffer.put( EMPTY_MATCHED_DN );
135 }
136
137
138 BerValue.encodeEnumerated( buffer, ldapResult.getResultCode().getValue() );
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 protected void encodeReverse( LdapApiService codec, Asn1Buffer buffer, byte tag, Message message )
158 {
159 int start = buffer.getPos();
160
161
162 encodeLdapResultReverse( buffer, ( ( ResultResponse ) message ).getLdapResult() );
163
164
165 BerValue.encodeSequence( buffer, tag, start );
166 }
167 }