| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 2001 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | /** |
| 41 | * Represents a set of search preferences. |
| 42 | * You can set these preferences for a particular search |
| 43 | * by creating an <CODE>LDAPSearchConstraints</CODE> object, |
| 44 | * specifying your preferences, and passing the object to |
| 45 | * the <CODE>LDAPConnection.search</CODE> method. |
| 46 | * <P> |
| 47 | * |
| 48 | * @version 1.0 |
| 49 | */ |
| 50 | public class LDAPSearchConstraints extends LDAPConstraints { |
| 51 | |
| 52 | private int deref; |
| 53 | private int maxRes; |
| 54 | private int batch; |
| 55 | private int serverTimeLimit; |
| 56 | private int maxBacklog = 100; |
| 57 | |
| 58 | /** |
| 59 | * Constructs an object with the default set of search constraints |
| 60 | */ |
| 61 | public LDAPSearchConstraints() { |
| 62 | super(); |
| 63 | deref = 0; |
| 64 | maxRes = 1000; |
| 65 | batch = 1; |
| 66 | serverTimeLimit = 0; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Constructs an object with the supplied constraints as template |
| 71 | */ |
| 72 | public LDAPSearchConstraints( LDAPConstraints cons ) { |
| 73 | this(); |
| 74 | |
| 75 | setHopLimit( cons.getHopLimit() ); |
| 76 | setReferralFollowing( cons.getReferralFollowing() ); |
| 77 | setTimeLimit( cons.getTimeLimit() ); |
| 78 | |
| 79 | setReferralHandler( cons.getReferralHandler()); |
| 80 | |
| 81 | LDAPControl[] tServerControls = cons.getControls(); |
| 82 | if ( (tServerControls != null) && |
| 83 | (tServerControls.length > 0) ) { |
| 84 | LDAPControl[] oServerControls = |
| 85 | new LDAPControl[tServerControls.length]; |
| 86 | for( int i = 0; i < tServerControls.length; i++ ) { |
| 87 | oServerControls[i] = (LDAPControl)tServerControls[i].clone(); |
| 88 | } |
| 89 | setControls(oServerControls); |
| 90 | } |
| 91 | |
| 92 | if ( cons instanceof LDAPSearchConstraints ) { |
| 93 | LDAPSearchConstraints scons = (LDAPSearchConstraints)cons; |
| 94 | setServerTimeLimit( scons.getServerTimeLimit() ); |
| 95 | setDereference( scons.getDereference() ); |
| 96 | setMaxResults( scons.getMaxResults() ); |
| 97 | setBatchSize( scons.getBatchSize() ); |
| 98 | setMaxBacklog( scons.getMaxBacklog() ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Constructs a new <CODE>LDAPSearchConstraints</CODE> object and allows you |
| 104 | * to specify the search constraints in that object. |
| 105 | * <P> |
| 106 | * @param msLimit maximum time in milliseconds to wait for results (0 |
| 107 | * by default, which means that there is no maximum time limit) |
| 108 | * @param timeLimit maximum time in seconds for the server to spend |
| 109 | * processing a search request (the default value is 0, indicating that there |
| 110 | * is no limit) |
| 111 | * @param dereference either <CODE>LDAPConnection.DEREF_NEVER</CODE>, |
| 112 | * <CODE>LDAPConnection.DEREF_FINDING</CODE>, |
| 113 | * <CODE>LDAPConnection.DEREF_SEARCHING</CODE>, or |
| 114 | * <CODE>LDAPConnection.DEREF_ALWAYS</CODE> (see LDAPConnection.setOption). |
| 115 | * <CODE>LDAPConnection.DEREF_NEVER</CODE> is the default. |
| 116 | * @param maxResults maximum number of search results to return |
| 117 | * (1000 by default) |
| 118 | * @param doReferrals specify <CODE>true</CODE> to follow referrals |
| 119 | * automatically, or <CODE>false</CODE> to throw an |
| 120 | * <CODE>LDAPReferralException</CODE> error if the server sends back |
| 121 | * a referral (<CODE>false</CODE> by default) |
| 122 | * @param batchSize specify the number of results to return at a time |
| 123 | * (1 by default) |
| 124 | * @param bind_proc specifies the object that |
| 125 | * implements the <CODE>LDAPBind</CODE> interface (you need to |
| 126 | * define this class). The object will be used to authenticate |
| 127 | * to the server on referrals. |
| 128 | * (This field is <CODE>null</CODE> by default.) |
| 129 | * @param hop_limit maximum number of referrals to follow in a |
| 130 | * sequence when attempting to resolve a request |
| 131 | * @see org.ietf.ldap.LDAPConnection#setOption(int, java.lang.Object) |
| 132 | * @see org.ietf.ldap.LDAPConnection#search(org.ietf.ldap.LDAPUrl, org.ietf.ldap.LDAPSearchConstraints) |
| 133 | * @see org.ietf.ldap.LDAPConnection#search(java.lang.String, int, java.lang.String, java.lang.String[], boolean, org.ietf.ldap.LDAPSearchConstraints) |
| 134 | */ |
| 135 | public LDAPSearchConstraints( int msLimit, |
| 136 | int timeLimit, |
| 137 | int dereference, |
| 138 | int maxResults, |
| 139 | boolean doReferrals, |
| 140 | int batchSize, |
| 141 | LDAPReferralHandler handler, |
| 142 | int hop_limit) { |
| 143 | super( msLimit, doReferrals, handler, hop_limit ); |
| 144 | serverTimeLimit = timeLimit; |
| 145 | deref = dereference; |
| 146 | maxRes = maxResults; |
| 147 | batch = batchSize; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the suggested number of results to return at a time during |
| 152 | * search. This should be 0 if intermediate results are not needed, and |
| 153 | * 1 if results are to be processed as they come in. |
| 154 | * @return number of results to return at a time. |
| 155 | */ |
| 156 | public int getBatchSize() { |
| 157 | return batch; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Specifies how aliases should be dereferenced. |
| 162 | * @return <CODE>LDAPConnection.DEREF_NEVER</CODE> to |
| 163 | * never follow ("dereference") aliases, |
| 164 | * <CODE>LDAPConnection.DEREF_FINDING</CODE> to dereference when finding |
| 165 | * the starting point for the search (but not when searching |
| 166 | * under that starting entry), <CODE>LDAPConnection.DEREF_SEARCHING</CODE> |
| 167 | * to dereference when searching the entries beneath the |
| 168 | * starting point of the search (but not when finding the starting |
| 169 | * entry), or <CODE>LDAPConnection.DEREF_ALWAYS</CODE> to always |
| 170 | * dereference aliases. |
| 171 | */ |
| 172 | public int getDereference() { |
| 173 | return deref; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get the maximum number of unread entries any search listener can |
| 178 | * have before we stop reading from the server. |
| 179 | * @return the maximum number of unread entries per listener. |
| 180 | * @deprecated Use <CODE>LDAPConnection.getOption()</CODE> |
| 181 | */ |
| 182 | public int getMaxBacklog() { |
| 183 | return maxBacklog; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns the maximum number of search results that are to be returned; 0 means |
| 188 | * there is no limit. |
| 189 | * @return maximum number of search results to be returned. |
| 190 | */ |
| 191 | public int getMaxResults() { |
| 192 | return maxRes; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the maximum number of seconds to wait for the server to |
| 197 | * spend on a search operation.If 0, there is no time limit. |
| 198 | * @return maximum number of seconds for the server to spend. |
| 199 | */ |
| 200 | public int getServerTimeLimit() { |
| 201 | return serverTimeLimit; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets the suggested number of results to return at a time during search. |
| 206 | * This should be 0 if intermediate results are not needed, and 1 if |
| 207 | * results are to be processed as they come in. (By default, this is 1.) |
| 208 | * @param batchSize number of results to return at a time |
| 209 | */ |
| 210 | public void setBatchSize( int batchSize ) { |
| 211 | batch = batchSize; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Sets a preference indicating how aliases should be dereferenced. |
| 216 | * @param dereference <CODE>LDAPConnection.DEREF_NEVER</CODE> to |
| 217 | * never follow ("dereference") aliases, |
| 218 | * <CODE>LDAPConnection.DEREF_FINDING</CODE> to dereference when finding |
| 219 | * the starting point for the search (but not when searching |
| 220 | * under that starting entry), <CODE>LDAPConnection.DEREF_SEARCHING</CODE> |
| 221 | * to dereference when searching the entries beneath the |
| 222 | * starting point of the search (but not when finding the starting |
| 223 | * entry), or <CODE>LDAPConnection.DEREF_ALWAYS</CODE> to always |
| 224 | * dereference aliases |
| 225 | */ |
| 226 | public void setDereference( int dereference ) { |
| 227 | deref = dereference; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Set the maximum number of unread entries any search listener can |
| 232 | * have before we stop reading from the server. |
| 233 | * @param backlog the maximum number of unread entries per listener |
| 234 | * @deprecated Use <CODE>LDAPConnection.setOption()</CODE> |
| 235 | */ |
| 236 | public void setMaxBacklog( int backlog ) { |
| 237 | maxBacklog = backlog; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Sets the maximum number of search results to return; 0 means |
| 242 | * there is no limit. (By default, this is set to 1000.) |
| 243 | * @param maxResults maximum number of search results to return |
| 244 | */ |
| 245 | public void setMaxResults( int maxResults ) { |
| 246 | maxRes = maxResults; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Sets the maximum number of seconds for the server to spend |
| 251 | * returning search results. If 0, there is no time limit. |
| 252 | * @param limit maximum number of seconds for the server to spend. |
| 253 | * (0 by default, which means that there is no maximum time limit.) |
| 254 | */ |
| 255 | public void setServerTimeLimit( int limit ) { |
| 256 | serverTimeLimit = limit; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Makes a copy of an existing set of search constraints. |
| 261 | * @return a copy of an existing set of search constraints. |
| 262 | */ |
| 263 | public Object clone() { |
| 264 | return new LDAPSearchConstraints( this ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Return a string representation of the object for debugging |
| 269 | * |
| 270 | * @return A string representation of the object |
| 271 | */ |
| 272 | public String toString() { |
| 273 | StringBuffer sb = new StringBuffer("LDAPSearchConstraints {"); |
| 274 | sb.append( super.toString() + ' ' ); |
| 275 | sb.append("size limit " + maxRes + ", "); |
| 276 | sb.append("server time limit " + serverTimeLimit + ", "); |
| 277 | sb.append("aliases " + deref + ", "); |
| 278 | sb.append("batch size " + batch + ", "); |
| 279 | sb.append("max backlog " + maxBacklog); |
| 280 | sb.append('}'); |
| 281 | |
| 282 | return sb.toString(); |
| 283 | } |
| 284 | } |