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