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.SearchRequestImpl
045 */
046public final class SearchRequest extends AbstractRequestImpl {
047    /**
048     * Use a LinkedHashSet to return the attributes in the same order as requested by the user.
049     */
050    private final Set<String> attributes = new LinkedHashSet<>();
051    private DereferenceAliasesPolicy dereferenceAliasesPolicy = DereferenceAliasesPolicy.NEVER;
052    private SearchFilter filter;
053    private DN name;
054    private SearchScope scope;
055    private int sizeLimit;
056    private int timeLimit;
057    private boolean typesOnly;
058
059    /**
060     * To be removed.
061     *
062     * @param name
063     *          the dn
064     * @param scope
065     *          the search scope
066     * @param filter
067     *          the search filter
068     */
069    SearchRequest(final DN name, final SearchScope scope, final SearchFilter filter) {
070        this.name = name;
071        this.scope = scope;
072        this.filter = filter;
073    }
074
075    /**
076     * To be removed.
077     *
078     * @param attributeDescriptions
079     *          the attribute descriptions
080     * @return the current object
081     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#addAttribute(String...)
082     */
083    public SearchRequest addAttribute(final String... attributeDescriptions) {
084        for (final String attributeDescription : attributeDescriptions) {
085            attributes.add(Reject.checkNotNull(attributeDescription));
086        }
087        return this;
088    }
089
090    /**
091     * To be added to {@link org.forgerock.opendj.ldap.requests.SearchRequestImpl}?
092     *
093     * @param attributeDescriptions
094     *          the attribute descriptions
095     * @return the current object
096     */
097    public SearchRequest addAttribute(final Collection<String> attributeDescriptions) {
098        for (final String attributeDescription : attributeDescriptions) {
099            attributes.add(Reject.checkNotNull(attributeDescription));
100        }
101        return this;
102    }
103
104    /**
105     * To be removed.
106     *
107     * @return the attributes
108     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getAttributes()
109     */
110    public Set<String> getAttributes() {
111        return attributes;
112    }
113
114    /**
115     * To be removed.
116     *
117     * @return the dereference aliases policy
118     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getDereferenceAliasesPolicy()
119     */
120    public DereferenceAliasesPolicy getDereferenceAliasesPolicy() {
121        return dereferenceAliasesPolicy;
122    }
123
124    /**
125     * To be removed.
126     *
127     * @return the search filter
128     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getFilter()
129     */
130    public SearchFilter getFilter() {
131        return filter;
132    }
133
134    /**
135     * To be removed.
136     *
137     * @return the DN
138     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getName()
139     */
140    public DN getName() {
141        return name;
142    }
143
144    /**
145     * To be removed.
146     *
147     * @return the search scope
148     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getScope()
149     */
150    public SearchScope getScope() {
151        return scope;
152    }
153
154    /**
155     * To be removed.
156     *
157     * @return the size limit
158     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getSizeLimit()
159     */
160    public int getSizeLimit() {
161        return sizeLimit;
162    }
163
164    /**
165     * To be removed.
166     *
167     * @return is single entry search
168     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#isSingleEntrySearch()
169     */
170    public boolean isSingleEntrySearch() {
171        return sizeLimit == 1 || SearchScope.BASE_OBJECT.equals(scope);
172    }
173
174    /**
175     * To be removed.
176     *
177     * @return the time limit
178     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getTimeLimit()
179     */
180    public int getTimeLimit() {
181        return timeLimit;
182    }
183
184    /**
185     * To be removed.
186     *
187     * @return the types only
188     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#isTypesOnly()
189     */
190    public boolean isTypesOnly() {
191        return typesOnly;
192    }
193
194    /**
195     * To be removed.
196     *
197     * @param policy the dereference aliases policy
198     * @return the current request
199     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setDereferenceAliasesPolicy(DereferenceAliasesPolicy)
200     */
201    public SearchRequest setDereferenceAliasesPolicy(final DereferenceAliasesPolicy policy) {
202        Reject.ifNull(policy);
203
204        this.dereferenceAliasesPolicy = policy;
205        return this;
206    }
207
208    /**
209     * To be removed.
210     *
211     * @param filter the search filter
212     * @return the current request
213     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setFilter(org.forgerock.opendj.ldap.Filter)
214     */
215    public SearchRequest setFilter(final SearchFilter filter) {
216        Reject.ifNull(filter);
217
218        this.filter = filter;
219        return this;
220    }
221
222    /**
223     * To be removed.
224     *
225     * @param filter the search filter
226     * @return the current request
227     * @throws DirectoryException if problem occurs
228     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setFilter(String)
229     */
230    public SearchRequest setFilter(final String filter) throws DirectoryException {
231        this.filter = SearchFilter.createFilterFromString(filter);
232        return this;
233    }
234
235    /**
236     * To be removed.
237     *
238     * @param dn the dn
239     * @return the current request
240     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setName(org.forgerock.opendj.ldap.DN)
241     */
242    public SearchRequest setName(final DN dn) {
243        Reject.ifNull(dn);
244
245        this.name = dn;
246        return this;
247    }
248
249    /**
250     * To be removed.
251     *
252     * @param dn the dn
253     * @return the current request
254     * @throws DirectoryException if problem occurs
255     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setName(String)
256     */
257    public SearchRequest setName(final String dn) throws DirectoryException {
258        Reject.ifNull(dn);
259
260        this.name = DN.valueOf(dn);
261        return this;
262    }
263
264    /**
265     * To be removed.
266     *
267     * @param scope the search scope
268     * @return the current request
269     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setScope(SearchScope)
270     */
271    public SearchRequest setScope(final SearchScope scope) {
272        Reject.ifNull(scope);
273
274        this.scope = scope;
275        return this;
276    }
277
278    /**
279     * To be removed.
280     *
281     * @param limit the size limit
282     * @return the current request
283     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setSizeLimit(int)
284     */
285    public SearchRequest setSizeLimit(final int limit) {
286        Reject.ifFalse(limit >= 0, "negative size limit");
287
288        this.sizeLimit = limit;
289        return this;
290    }
291
292    /**
293     * To be removed.
294     *
295     * @param limit the time limit
296     * @return the current request
297     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setTimeLimit(int)
298     */
299    public SearchRequest setTimeLimit(final int limit) {
300        Reject.ifFalse(limit >= 0, "negative time limit");
301
302        this.timeLimit = limit;
303        return this;
304    }
305
306    /**
307     * To be removed.
308     *
309     * @param typesOnly the types only
310     * @return the current request
311     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setTypesOnly(boolean)
312     */
313    public SearchRequest setTypesOnly(final boolean typesOnly) {
314        this.typesOnly = typesOnly;
315        return this;
316    }
317
318    /** {@inheritDoc} */
319    @Override
320    public SearchRequest addControl(Control control) {
321        super.addControl(control);
322        return this;
323    }
324
325    /** {@inheritDoc} */
326    @Override
327    public SearchRequest addControl(Collection<Control> controls) {
328        super.addControl(controls);
329        return this;
330    }
331
332    /** {@inheritDoc} */
333    @Override
334    public String toString() {
335        final StringBuilder builder = new StringBuilder();
336        builder.append("SearchRequest(name=");
337        builder.append(getName());
338        builder.append(", scope=");
339        builder.append(getScope());
340        builder.append(", dereferenceAliasesPolicy=");
341        builder.append(getDereferenceAliasesPolicy());
342        builder.append(", sizeLimit=");
343        builder.append(getSizeLimit());
344        builder.append(", timeLimit=");
345        builder.append(getTimeLimit());
346        builder.append(", typesOnly=");
347        builder.append(isTypesOnly());
348        builder.append(", filter=");
349        builder.append(getFilter());
350        builder.append(", attributes=");
351        builder.append(getAttributes());
352        builder.append(", controls=");
353        builder.append(getControls());
354        builder.append(")");
355        return builder.toString();
356    }
357
358}