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