1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.server.core.api;
22
23
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import org.apache.directory.api.ldap.model.constants.Loggers;
29 import org.apache.directory.api.ldap.model.cursor.AbstractCursor;
30 import org.apache.directory.api.ldap.model.cursor.ClosureMonitor;
31 import org.apache.directory.api.ldap.model.cursor.Cursor;
32 import org.apache.directory.api.ldap.model.cursor.CursorException;
33 import org.apache.directory.api.ldap.model.cursor.SearchCursor;
34 import org.apache.directory.api.ldap.model.entry.Entry;
35 import org.apache.directory.api.ldap.model.exception.LdapException;
36 import org.apache.directory.api.ldap.model.message.Control;
37 import org.apache.directory.api.ldap.model.message.IntermediateResponse;
38 import org.apache.directory.api.ldap.model.message.LdapResult;
39 import org.apache.directory.api.ldap.model.message.Referral;
40 import org.apache.directory.api.ldap.model.message.Response;
41 import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
42 import org.apache.directory.api.ldap.model.message.ResultResponse;
43 import org.apache.directory.api.ldap.model.message.SearchRequest;
44 import org.apache.directory.api.ldap.model.message.SearchResultDone;
45 import org.apache.directory.api.ldap.model.message.SearchResultDoneImpl;
46 import org.apache.directory.api.ldap.model.message.SearchResultEntry;
47 import org.apache.directory.api.ldap.model.message.SearchResultEntryImpl;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56
57
58 public class EntryToResponseCursor extends AbstractCursor<Response> implements SearchCursor
59 {
60
61 private static final Logger LOG_CURSOR = LoggerFactory.getLogger( Loggers.CURSOR_LOG.getName() );
62
63
64 private static final boolean IS_DEBUG = LOG_CURSOR.isDebugEnabled();
65
66
67 private Cursor<Entry> wrapped;
68
69
70 private SearchResultDone searchDoneResp;
71
72
73 private boolean done;
74
75
76 private int messageId;
77
78
79 private SearchRequest searchRequest;
80
81 public EntryToResponseCursor( SearchRequest searchRequest, int messageId, Cursor<Entry> wrapped )
82 {
83 if ( IS_DEBUG )
84 {
85 LOG_CURSOR.debug( "Creating EntryToResponseCursor {}", this );
86 }
87
88 this.searchRequest = searchRequest;
89 this.wrapped = wrapped;
90 this.messageId = messageId;
91 }
92
93
94 public Iterator<Response> iterator()
95 {
96 throw new UnsupportedOperationException();
97 }
98
99
100
101
102
103 public void after( Response resp ) throws LdapException, CursorException
104 {
105 throw new UnsupportedOperationException();
106 }
107
108
109
110
111
112 public void afterLast() throws LdapException, CursorException
113 {
114 wrapped.afterLast();
115 }
116
117
118
119
120
121 public boolean available()
122 {
123 return wrapped.available();
124 }
125
126
127
128
129
130 public void before( Response resp ) throws LdapException, CursorException
131 {
132 throw new UnsupportedOperationException();
133 }
134
135
136
137
138
139 public void beforeFirst() throws LdapException, CursorException
140 {
141 wrapped.beforeFirst();
142 }
143
144
145
146
147
148 public void close() throws IOException
149 {
150 if ( IS_DEBUG )
151 {
152 LOG_CURSOR.debug( "Closing EntryToResponseCursor {}", this );
153 }
154
155 wrapped.close();
156 }
157
158
159
160
161
162 public void close( Exception e ) throws IOException
163 {
164 if ( IS_DEBUG )
165 {
166 LOG_CURSOR.debug( "Closing EntryToResponseCursor {}", this );
167 }
168
169 wrapped.close( e );
170 }
171
172
173
174
175
176 public boolean first() throws LdapException, CursorException
177 {
178 return wrapped.first();
179 }
180
181
182
183
184
185 public Response get() throws CursorException
186 {
187 Entry entry = wrapped.get();
188 SearchResultEntry se = new SearchResultEntryImpl( messageId );
189 se.setEntry( entry );
190
191 return se;
192 }
193
194
195
196
197
198
199
200 public SearchResultDone getSearchResultDone()
201 {
202 return searchDoneResp;
203 }
204
205
206
207
208
209 public boolean isClosed()
210 {
211 return wrapped.isClosed();
212 }
213
214
215
216
217
218 public boolean last() throws LdapException, CursorException
219 {
220 return wrapped.last();
221 }
222
223
224
225
226
227 public boolean next() throws LdapException, CursorException
228 {
229 boolean next = wrapped.next();
230
231 if ( !next )
232 {
233 searchDoneResp = new SearchResultDoneImpl( messageId );
234
235 ResultCodeEnum re = ResultCodeEnum.SUCCESS;
236
237 ResultResponse processedResp = searchRequest.getResultResponse();
238 LdapResult filledResult = processedResp.getLdapResult();
239
240 if ( filledResult.getResultCode() != re )
241 {
242 re = filledResult.getResultCode();
243 }
244
245 searchDoneResp.getLdapResult().setResultCode( re );
246
247 Map<String, Control> ctrls = processedResp.getControls();
248
249 if ( ctrls != null )
250 {
251 Iterator<Control> itr = ctrls.values().iterator();
252 while ( itr.hasNext() )
253 {
254 searchDoneResp.addControl( itr.next() );
255 }
256 }
257
258 done = true;
259 }
260
261 return next;
262 }
263
264
265
266
267
268 public boolean previous() throws LdapException, CursorException
269 {
270 return wrapped.previous();
271 }
272
273
274
275
276
277 public void setClosureMonitor( ClosureMonitor monitor )
278 {
279 wrapped.setClosureMonitor( monitor );
280 }
281
282
283
284
285
286 public boolean isDone()
287 {
288 return done;
289 }
290
291
292
293
294
295 public boolean isReferral()
296 {
297 return false;
298 }
299
300
301
302
303
304 public Referral getReferral() throws LdapException
305 {
306 throw new LdapException();
307 }
308
309
310
311
312
313 public boolean isEntry()
314 {
315 return true;
316 }
317
318
319
320
321
322 public Entry getEntry() throws LdapException
323 {
324 if ( !done && wrapped.available() )
325 {
326 try
327 {
328 return wrapped.get();
329 }
330 catch ( Exception e )
331 {
332 throw new LdapException( e );
333 }
334 }
335
336 throw new LdapException();
337 }
338
339
340
341
342
343 public boolean isIntermediate()
344 {
345 return false;
346 }
347
348
349
350
351
352 public IntermediateResponse getIntermediate() throws LdapException
353 {
354 throw new LdapException();
355 }
356 }