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.model.message;
21
22
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import javax.naming.directory.SearchControls;
27
28 import org.apache.directory.api.ldap.model.exception.LdapException;
29 import org.apache.directory.api.ldap.model.schema.AttributeType;
30 import org.apache.directory.api.ldap.model.schema.AttributeTypeOptions;
31 import org.apache.directory.api.ldap.model.schema.SchemaManager;
32 import org.apache.directory.api.ldap.model.schema.SchemaUtils;
33 import org.apache.directory.api.util.Strings;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43 public class SearchParams
44 {
45
46 private static final Logger LOG = LoggerFactory.getLogger( SearchParams.class );
47
48
49 private SearchScope scope = SearchScope.OBJECT;
50
51
52 private int timeLimit = 0;
53
54
55 private long sizeLimit = 0;
56
57
58 private boolean typesOnly = false;
59
60
61 private AliasDerefMode aliasDerefMode = AliasDerefMode.DEREF_ALWAYS;
62
63
64 private Set<String> returningAttributesStr;
65
66
67 private Set<AttributeTypeOptions> returningAttributes;
68
69
70 private Set<Control> controls;
71
72
73
74
75
76
77 public SearchParams()
78 {
79 returningAttributes = new HashSet<AttributeTypeOptions>();
80 returningAttributesStr = new HashSet<String>();
81 controls = new HashSet<Control>();
82 }
83
84
85
86
87
88 public SearchScope getScope()
89 {
90 return scope;
91 }
92
93
94
95
96
97 public void setScope( SearchScope scope )
98 {
99 this.scope = scope;
100 }
101
102
103
104
105
106 public int getTimeLimit()
107 {
108 return timeLimit;
109 }
110
111
112
113
114
115 public void setTimeLimit( int timeLimit )
116 {
117 this.timeLimit = timeLimit;
118 }
119
120
121
122
123
124 public long getSizeLimit()
125 {
126 return sizeLimit;
127 }
128
129
130
131
132
133 public void setSizeLimit( long sizeLimit )
134 {
135 this.sizeLimit = sizeLimit;
136 }
137
138
139
140
141
142 public boolean isTypesOnly()
143 {
144 return typesOnly;
145 }
146
147
148
149
150
151 public void setTypesOnly( boolean typesOnly )
152 {
153 this.typesOnly = typesOnly;
154 }
155
156
157
158
159
160 public AliasDerefMode getAliasDerefMode()
161 {
162 return aliasDerefMode;
163 }
164
165
166
167
168
169 public void setAliasDerefMode( AliasDerefMode aliasDerefMode )
170 {
171 this.aliasDerefMode = aliasDerefMode;
172 }
173
174
175
176
177
178 public Set<AttributeTypeOptions> getReturningAttributes()
179 {
180 return returningAttributes;
181 }
182
183
184
185
186
187 public Set<String> getReturningAttributesStr()
188 {
189 return returningAttributesStr;
190 }
191
192
193
194
195
196
197
198
199
200 public void normalize( SchemaManager schemaManager )
201 {
202 for ( String returnAttribute : returningAttributesStr )
203 {
204 try
205 {
206 String id = SchemaUtils.stripOptions( returnAttribute );
207 Set<String> options = SchemaUtils.getOptions( returnAttribute );
208
209 AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( id );
210 AttributeTypeOptions attrOptions = new AttributeTypeOptions( attributeType, options );
211
212 returningAttributes.add( attrOptions );
213 }
214 catch ( LdapException ne )
215 {
216 LOG.warn( "Requested attribute {} does not exist in the schema, it will be ignored", returnAttribute );
217
218 }
219 }
220 }
221
222
223
224
225
226 public void setReturningAttributes( String... returningAttributes )
227 {
228 if ( returningAttributes != null )
229 {
230 for ( String returnAttribute : returningAttributes )
231 {
232 this.returningAttributesStr.add( returnAttribute );
233 }
234 }
235 }
236
237
238
239
240
241 public void addReturningAttributes( String returningAttribute )
242 {
243 this.returningAttributesStr.add( returningAttribute );
244 }
245
246
247
248
249
250 public Set<Control> getControls()
251 {
252 return controls;
253 }
254
255
256
257
258
259 public void setControls( Set<Control> controls )
260 {
261 this.controls = controls;
262 }
263
264
265
266
267
268 public void addControl( Control control )
269 {
270 this.controls.add( control );
271 }
272
273
274
275
276
277
278
279
280
281 public static SearchParams toSearchParams( SearchControls searchControls, AliasDerefMode aliasDerefMode )
282 {
283 SearchParams searchParams = new SearchParams();
284
285 searchParams.setAliasDerefMode( aliasDerefMode );
286 searchParams.setTimeLimit( searchControls.getTimeLimit() );
287 searchParams.setSizeLimit( searchControls.getCountLimit() );
288 searchParams.setScope( SearchScope.getSearchScope( searchControls.getSearchScope() ) );
289 searchParams.setTypesOnly( searchControls.getReturningObjFlag() );
290
291 if ( searchControls.getReturningAttributes() != null )
292 {
293 for ( String returningAttribute : searchControls.getReturningAttributes() )
294 {
295 searchParams.addReturningAttributes( returningAttribute );
296 }
297 }
298
299 return searchParams;
300 }
301
302
303
304
305
306 public String toString()
307 {
308 StringBuilder sb = new StringBuilder();
309
310 sb.append( "Search parameters :\n" );
311 sb.append( " scope : " ).append( scope ).append( "\n" );
312 sb.append( " Alias dereferencing : " ).append( aliasDerefMode ).append( "\n" );
313 sb.append( " types only : " ).append( typesOnly ).append( "\n" );
314
315 if ( returningAttributesStr.size() != 0 )
316 {
317 sb.append( " returning attributes : " ).append( Strings.setToString( returningAttributesStr ) )
318 .append( "\n" );
319 }
320
321 if ( timeLimit > 0 )
322 {
323 sb.append( " timeLimit : " ).append( timeLimit ).append( "\n" );
324 }
325 else
326 {
327 sb.append( " no timeLimit\n" );
328 }
329
330 if ( timeLimit > 0 )
331 {
332 sb.append( " sizeLimit : " ).append( sizeLimit ).append( "\n" );
333 }
334 else
335 {
336 sb.append( " no sizeLimit\n" );
337 }
338
339 if ( controls.size() != 0 )
340 {
341 for ( Control control : controls )
342 {
343 sb.append( " control : " ).
344 append( control.getOid() ).append( "/" ).
345 append( control.getClass().getName() ).append( "\n" );
346 }
347 }
348
349 return sb.toString();
350 }
351 }