1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.ldap;
21
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.apache.directory.server.core.CoreSession;
29 import org.apache.directory.shared.ldap.message.AbandonableRequest;
30 import org.apache.directory.shared.ldap.message.BindStatus;
31 import org.apache.mina.common.IoSession;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40
41
42
43 public class LdapSession
44 {
45
46 private static final Logger LOG = LoggerFactory.getLogger( LdapSession.class );
47
48
49 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
50
51
52 private static final AbandonableRequest[] EMPTY_ABANDONABLES = new AbandonableRequest[0];
53
54 private final String outstandingLock;
55
56
57
58
59
60 private final IoSession ioSession;
61
62
63 private CoreSession coreSession;
64
65
66 private LdapService ldapService;
67
68
69 private Map<Integer, AbandonableRequest> outstandingRequests;
70
71
72
73 private BindStatus bindStatus;
74
75
76 private String currentMechanism;
77
78
79
80
81
82 private Map<String, Object> saslProperties;
83
84
85
86
87
88
89
90
91 public LdapSession( IoSession ioSession )
92 {
93 this.ioSession = ioSession;
94 outstandingLock = "OutstandingRequestLock: " + ioSession.toString();
95 outstandingRequests = new ConcurrentHashMap<Integer, AbandonableRequest>();
96 bindStatus = BindStatus.ANONYMOUS;
97 saslProperties = new HashMap<String, Object>();
98 }
99
100
101
102
103
104
105
106
107
108
109 public boolean isAuthenticated()
110 {
111 return ( coreSession != null ) && bindStatus == BindStatus.AUTHENTICATED;
112 }
113
114
115
116
117
118
119
120
121
122
123 public boolean isAnonymous()
124 {
125 return bindStatus == BindStatus.ANONYMOUS;
126 }
127
128
129
130
131
132
133
134 public boolean isAuthPending()
135 {
136 return bindStatus == BindStatus.AUTH_PENDING;
137 }
138
139
140
141
142
143
144
145 public IoSession getIoSession()
146 {
147 return ioSession;
148 }
149
150
151
152
153
154
155
156
157 public CoreSession getCoreSession()
158 {
159 return coreSession;
160 }
161
162
163
164
165
166
167
168 public void setCoreSession( CoreSession coreSession )
169 {
170 this.coreSession = coreSession;
171 }
172
173
174
175
176
177 public void abandonAllOutstandingRequests()
178 {
179 synchronized ( outstandingLock )
180 {
181 AbandonableRequest[] abandonables = outstandingRequests.values().toArray( EMPTY_ABANDONABLES );
182
183 for ( AbandonableRequest abandonable : abandonables )
184 {
185 abandonOutstandingRequest( abandonable.getMessageId() );
186 }
187 }
188 }
189
190
191
192
193
194 public AbandonableRequest abandonOutstandingRequest( Integer messageId )
195 {
196 AbandonableRequest request = null;
197
198 synchronized ( outstandingLock )
199 {
200 request = outstandingRequests.remove( messageId );
201 }
202
203 if ( request == null )
204 {
205 LOG.warn( "AbandonableRequest with messageId {} not found in outstandingRequests.", messageId );
206 return null;
207 }
208
209 if ( request.isAbandoned() )
210 {
211 LOG.warn( "AbandonableRequest with messageId {} has already been abandoned", messageId );
212 return request;
213 }
214
215 request.abandon();
216
217 if ( IS_DEBUG )
218 {
219 LOG.debug( "AbandonRequest on AbandonableRequest wth messageId {} was successful.", messageId );
220 }
221
222 return request;
223 }
224
225
226
227
228
229
230
231 public void registerOutstandingRequest( AbandonableRequest request )
232 {
233 synchronized( outstandingLock )
234 {
235 outstandingRequests.put( request.getMessageId(), request );
236 }
237 }
238
239
240
241
242
243
244
245 public void unregisterOutstandingRequest( AbandonableRequest request )
246 {
247 synchronized( outstandingLock )
248 {
249 outstandingRequests.remove( request.getMessageId() );
250 }
251 }
252
253
254 public Map<Integer, AbandonableRequest> getOutstandingRequests()
255 {
256 synchronized( outstandingLock )
257 {
258 return Collections.unmodifiableMap( outstandingRequests );
259 }
260 }
261
262
263
264
265
266 public BindStatus getBindStatus()
267 {
268 return bindStatus;
269 }
270
271
272
273
274
275 public void setAuthPending()
276 {
277 bindStatus = BindStatus.AUTH_PENDING;
278 }
279
280
281
282
283
284 public void setAnonymous()
285 {
286 bindStatus = BindStatus.ANONYMOUS;
287 }
288
289
290
291
292
293 public void setAuthenticated()
294 {
295 bindStatus = BindStatus.AUTHENTICATED;
296 }
297
298
299
300
301
302
303
304 public String getCurrentMechanism()
305 {
306 return currentMechanism;
307 }
308
309
310
311
312
313
314
315
316 public void putSaslProperty( String property, Object value )
317 {
318 saslProperties.put( property, value );
319 }
320
321
322
323
324
325
326
327
328 public Object getSaslProperty( String property )
329 {
330 return saslProperties.get( property );
331 }
332
333
334
335
336
337 public void clearSaslProperties()
338 {
339 saslProperties.clear();
340 }
341
342
343
344
345
346
347
348 public void removeSaslProperty( String property )
349 {
350 saslProperties.remove( property );
351 }
352
353
354
355
356
357 public LdapService getLdapServer()
358 {
359 return ldapService;
360 }
361
362
363
364
365
366
367
368 public void setLdapServer( LdapService ldapService )
369 {
370 this.ldapService = ldapService;
371 }
372 }