001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2010 Sun Microsystems, Inc.
025 *      Portions copyright 2012-2015 ForgeRock AS.
026 */
027package org.opends.server.protocols.internal;
028
029import java.util.Collection;
030import java.util.LinkedHashSet;
031import java.util.Set;
032
033import org.forgerock.opendj.ldap.DereferenceAliasesPolicy;
034import org.forgerock.opendj.ldap.SearchScope;
035import org.forgerock.util.Reject;
036import org.opends.server.types.Control;
037import org.opends.server.types.DN;
038import org.opends.server.types.DirectoryException;
039import org.opends.server.types.SearchFilter;
040
041/**
042 * Search request implementation.
043 *
044 * @see org.forgerock.opendj.ldap.requests.SearchRequest
045 */
046public final class SearchRequest extends AbstractRequestImpl {
047    /** Use a LinkedHashSet to return the attributes in the same order as requested by the user. */
048    private final Set<String> attributes = new LinkedHashSet<>();
049    private DereferenceAliasesPolicy dereferenceAliasesPolicy = DereferenceAliasesPolicy.NEVER;
050    private SearchFilter filter;
051    private DN name;
052    private SearchScope scope;
053    private int sizeLimit;
054    private int timeLimit;
055    private boolean typesOnly;
056
057    /**
058     * To be removed.
059     *
060     * @param name
061     *          the dn
062     * @param scope
063     *          the search scope
064     * @param filter
065     *          the search filter
066     */
067    SearchRequest(final DN name, final SearchScope scope, final SearchFilter filter) {
068        this.name = name;
069        this.scope = scope;
070        this.filter = filter;
071    }
072
073    /**
074     * To be removed.
075     *
076     * @param attributeDescriptions
077     *          the attribute descriptions
078     * @return the current object
079     * @see org.forgerock.opendj.ldap.requests.SearchRequest#addAttribute(String...)
080     */
081    public SearchRequest addAttribute(final String... attributeDescriptions) {
082        for (final String attributeDescription : attributeDescriptions) {
083            attributes.add(Reject.checkNotNull(attributeDescription));
084        }
085        return this;
086    }
087
088    /**
089     * To be added to {@link org.forgerock.opendj.ldap.requests.SearchRequest}?
090     *
091     * @param attributeDescriptions
092     *          the attribute descriptions
093     * @return the current object
094     */
095    public SearchRequest addAttribute(final Collection<String> attributeDescriptions) {
096        for (final String attributeDescription : attributeDescriptions) {
097            attributes.add(Reject.checkNotNull(attributeDescription));
098        }
099        return this;
100    }
101
102    /**
103     * To be removed.
104     *
105     * @return the attributes
106     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getAttributes()
107     */
108    public Set<String> getAttributes() {
109        return attributes;
110    }
111
112    /**
113     * To be removed.
114     *
115     * @return the dereference aliases policy
116     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getDereferenceAliasesPolicy()
117     */
118    public DereferenceAliasesPolicy getDereferenceAliasesPolicy() {
119        return dereferenceAliasesPolicy;
120    }
121
122    /**
123     * To be removed.
124     *
125     * @return the search filter
126     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getFilter()
127     */
128    public SearchFilter getFilter() {
129        return filter;
130    }
131
132    /**
133     * To be removed.
134     *
135     * @return the DN
136     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getName()
137     */
138    public DN getName() {
139        return name;
140    }
141
142    /**
143     * To be removed.
144     *
145     * @return the search scope
146     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getScope()
147     */
148    public SearchScope getScope() {
149        return scope;
150    }
151
152    /**
153     * To be removed.
154     *
155     * @return the size limit
156     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getSizeLimit()
157     */
158    public int getSizeLimit() {
159        return sizeLimit;
160    }
161
162    /**
163     * To be removed.
164     *
165     * @return is single entry search
166     * @see org.forgerock.opendj.ldap.requests.SearchRequest#isSingleEntrySearch()
167     */
168    public boolean isSingleEntrySearch() {
169        return sizeLimit == 1 || SearchScope.BASE_OBJECT.equals(scope);
170    }
171
172    /**
173     * To be removed.
174     *
175     * @return the time limit
176     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getTimeLimit()
177     */
178    public int getTimeLimit() {
179        return timeLimit;
180    }
181
182    /**
183     * To be removed.
184     *
185     * @return the types only
186     * @see org.forgerock.opendj.ldap.requests.SearchRequest#isTypesOnly()
187     */
188    public boolean isTypesOnly() {
189        return typesOnly;
190    }
191
192    /**
193     * To be removed.
194     *
195     * @param policy the dereference aliases policy
196     * @return the current request
197     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setDereferenceAliasesPolicy(DereferenceAliasesPolicy)
198     */
199    public SearchRequest setDereferenceAliasesPolicy(final DereferenceAliasesPolicy policy) {
200        Reject.ifNull(policy);
201
202        this.dereferenceAliasesPolicy = policy;
203        return this;
204    }
205
206    /**
207     * To be removed.
208     *
209     * @param filter the search filter
210     * @return the current request
211     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setFilter(org.forgerock.opendj.ldap.Filter)
212     */
213    public SearchRequest setFilter(final SearchFilter filter) {
214        Reject.ifNull(filter);
215
216        this.filter = filter;
217        return this;
218    }
219
220    /**
221     * To be removed.
222     *
223     * @param filter the search filter
224     * @return the current request
225     * @throws DirectoryException if problem occurs
226     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setFilter(String)
227     */
228    public SearchRequest setFilter(final String filter) throws DirectoryException {
229        this.filter = SearchFilter.createFilterFromString(filter);
230        return this;
231    }
232
233    /**
234     * To be removed.
235     *
236     * @param dn the dn
237     * @return the current request
238     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setName(org.forgerock.opendj.ldap.DN)
239     */
240    public SearchRequest setName(final DN dn) {
241        Reject.ifNull(dn);
242
243        this.name = dn;
244        return this;
245    }
246
247    /**
248     * To be removed.
249     *
250     * @param dn the dn
251     * @return the current request
252     * @throws DirectoryException if problem occurs
253     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setName(String)
254     */
255    public SearchRequest setName(final String dn) throws DirectoryException {
256        Reject.ifNull(dn);
257
258        this.name = DN.valueOf(dn);
259        return this;
260    }
261
262    /**
263     * To be removed.
264     *
265     * @param scope the search scope
266     * @return the current request
267     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setScope(SearchScope)
268     */
269    public SearchRequest setScope(final SearchScope scope) {
270        Reject.ifNull(scope);
271
272        this.scope = scope;
273        return this;
274    }
275
276    /**
277     * To be removed.
278     *
279     * @param limit the size limit
280     * @return the current request
281     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setSizeLimit(int)
282     */
283    public SearchRequest setSizeLimit(final int limit) {
284        Reject.ifFalse(limit >= 0, "negative size limit");
285
286        this.sizeLimit = limit;
287        return this;
288    }
289
290    /**
291     * To be removed.
292     *
293     * @param limit the time limit
294     * @return the current request
295     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setTimeLimit(int)
296     */
297    public SearchRequest setTimeLimit(final int limit) {
298        Reject.ifFalse(limit >= 0, "negative time limit");
299
300        this.timeLimit = limit;
301        return this;
302    }
303
304    /**
305     * To be removed.
306     *
307     * @param typesOnly the types only
308     * @return the current request
309     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setTypesOnly(boolean)
310     */
311    public SearchRequest setTypesOnly(final boolean typesOnly) {
312        this.typesOnly = typesOnly;
313        return this;
314    }
315
316    @Override
317    public SearchRequest addControl(Control control) {
318        super.addControl(control);
319        return this;
320    }
321
322    @Override
323    public SearchRequest addControl(Collection<Control> controls) {
324        super.addControl(controls);
325        return this;
326    }
327
328    @Override
329    public String toString() {
330        final StringBuilder sb = new StringBuilder();
331        sb.append(getClass().getSimpleName());
332        sb.append("(name=").append(getName());
333        sb.append(", scope=").append(getScope());
334        sb.append(", filter=").append(getFilter());
335        sb.append(", dereferenceAliasesPolicy=").append(getDereferenceAliasesPolicy());
336        if (getSizeLimit()!=0) {
337          sb.append(", sizeLimit=").append(getSizeLimit());
338        }
339        if (getTimeLimit()!=0) {
340          sb.append(", timeLimit=").append(getTimeLimit());
341        }
342        sb.append(", typesOnly=").append(isTypesOnly());
343        if (!getAttributes().isEmpty()) {
344          sb.append(", attributes=").append(getAttributes());
345        }
346        if (!getControls().isEmpty()) {
347          sb.append(", controls=").append(getControls());
348        }
349        sb.append(")");
350        return sb.toString();
351    }
352}