| 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 | import java.io.Serializable; |
| 41 | import java.util.Hashtable; |
| 42 | |
| 43 | /** |
| 44 | * Represents a set of operation preferences. |
| 45 | * You can set these preferences for a particular operation |
| 46 | * by creating an <CODE>LDAPConstraints</CODE> object, |
| 47 | * specifying your preferences, and passing the object to |
| 48 | * the proper <CODE>LDAPConnection</CODE> method. |
| 49 | * <P> |
| 50 | * |
| 51 | * @version 1.0 |
| 52 | */ |
| 53 | public class LDAPConstraints implements Cloneable, Serializable { |
| 54 | |
| 55 | static final long serialVersionUID = 6506767263918312029L; |
| 56 | private int _hop_limit = 5; |
| 57 | private LDAPReferralHandler _referralHandler = null; |
| 58 | private boolean referrals = false; |
| 59 | private int _time_limit = 0; |
| 60 | private LDAPControl[] _serverControls = null; |
| 61 | private Hashtable<String, Object> _properties = null; |
| 62 | |
| 63 | /** |
| 64 | * Constructs an <CODE>LDAPConstraints</CODE> object that specifies |
| 65 | * the default set of constraints. |
| 66 | */ |
| 67 | public LDAPConstraints() { |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Constructs a new <CODE>LDAPConstraints</CODE> object and allows you |
| 72 | * to specify the constraints in that object. |
| 73 | * <P> |
| 74 | * @param msLimit maximum time in milliseconds to wait for results (0 |
| 75 | * by default, which means that there is no maximum time limit) |
| 76 | * @param doReferrals specify <CODE>true</CODE> to follow referrals |
| 77 | * automatically, or <CODE>False</CODE> to throw an |
| 78 | * <CODE>LDAPReferralException</CODE> error if the server sends back |
| 79 | * a referral (<CODE>False</CODE> by default) |
| 80 | * @param handler specifies the object that |
| 81 | * implements the <CODE>LDAPReferralHandler</CODE> interface (you need to |
| 82 | * define this class). The object will be used when the client |
| 83 | * follows referrals automatically. See org.ietf.ldap.LDAPAuthHandler |
| 84 | * and org.ietf.ldap.LDAPReferralHandler. |
| 85 | * (This field is <CODE>null</CODE> by default.) |
| 86 | * @param hop_limit maximum number of referrals to follow in a |
| 87 | * sequence when attempting to resolve a request |
| 88 | * @see org.ietf.ldap.LDAPConnection#setOption(int, java.lang.Object) |
| 89 | */ |
| 90 | public LDAPConstraints( int msLimit, boolean doReferrals, |
| 91 | LDAPReferralHandler handler, |
| 92 | int hop_limit ) { |
| 93 | _time_limit = msLimit; |
| 94 | referrals = doReferrals; |
| 95 | _referralHandler = handler; |
| 96 | _hop_limit = hop_limit; |
| 97 | _serverControls = null; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the maximum number of milliseconds to wait for any operation |
| 102 | * under these constraints. If 0, there is no maximum time limit |
| 103 | * on waiting for the operation results. |
| 104 | * @return maximum number of milliseconds to wait for operation results. |
| 105 | */ |
| 106 | public int getTimeLimit() { |
| 107 | return _time_limit; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Specifies whether nor not referrals are followed automatically. |
| 113 | * Returns <CODE>true</CODE> if referrals are to be followed automatically, |
| 114 | * or <CODE>false</CODE> if referrals throw an |
| 115 | * <CODE>LDAPReferralException</CODE>. |
| 116 | * @return <CODE>true</CODE> if referrals are followed automatically, |
| 117 | * <CODE>false</CODE> if referrals throw an |
| 118 | * <CODE>LDAPReferralException</CODE>. |
| 119 | */ |
| 120 | public boolean getReferralFollowing() { |
| 121 | return referrals; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the object that provides the mechanism for authenticating to the |
| 126 | * server on referrals. This object must implement the |
| 127 | * <CODE>LDAPReferralHandler</CODE> interface. |
| 128 | * @return object to use to authenticate to the server on referrals |
| 129 | * @see org.ietf.ldap.LDAPReferralHandler |
| 130 | */ |
| 131 | public LDAPReferralHandler getReferralHandler() { |
| 132 | return _referralHandler; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the maximum number of hops to follow during a referral. |
| 137 | * @return maximum number of hops to follow during a referral. |
| 138 | */ |
| 139 | public int getHopLimit() { |
| 140 | return _hop_limit; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns any server controls to be applied by the server |
| 145 | * to LDAP operations. |
| 146 | * @return server controls for the server to apply to LDAP operations. |
| 147 | * @see org.ietf.ldap.LDAPControl |
| 148 | * @see org.ietf.ldap.LDAPConnection#getOption |
| 149 | * @see org.ietf.ldap.LDAPConnection#setOption |
| 150 | */ |
| 151 | public LDAPControl[] getControls() { |
| 152 | return _serverControls; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Gets a property of a constraints object which has been assigned with |
| 157 | * setProperty. Null is returned if the property is not defined. |
| 158 | * |
| 159 | * @param name Name of the property to retrieve |
| 160 | */ |
| 161 | public Object getProperty( String name ) { |
| 162 | if ( _properties == null ) { |
| 163 | return null; |
| 164 | } |
| 165 | return _properties.get( name ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Sets the maximum number of milliseconds to wait for any operation |
| 170 | * under these constraints. If 0, there is no maximum time limit |
| 171 | * on waiting for the operation results. If the time limit is exceeded, |
| 172 | * an LDAPException with the result code <CODE>LDAPException.TIME_LIMIT |
| 173 | * </CODE> is thrown. |
| 174 | * @param msLimit Maximum number of milliseconds to wait for operation |
| 175 | * results (0 by default, which means that there is no maximum time |
| 176 | * limit.) |
| 177 | * @see org.ietf.ldap.LDAPException#LDAP_TIMEOUT |
| 178 | */ |
| 179 | public void setTimeLimit( int msLimit ) { |
| 180 | _time_limit = msLimit; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * Specifies whether or not referrals are followed automatically. |
| 186 | * Specify <CODE>true</CODE> if referrals are to be followed automatically, |
| 187 | * or <CODE>false</CODE> if referrals are to throw an |
| 188 | * <CODE>LDAPReferralException</CODE>. |
| 189 | * (By default, this is set to <CODE>false</CODE>.) |
| 190 | * <P> |
| 191 | * If you set this to <CODE>true</CODE>, you need to create an object of |
| 192 | * this class that implements either the <CODE>LDAPAuthHandler</CODE> or |
| 193 | * <CODE>LDAPBind</CODE> interface. The <CODE>LDAPAuthProvider</CODE> object |
| 194 | * identifies the method for retrieving authentication information which |
| 195 | * will be used when connecting to other LDAP servers during referrals. |
| 196 | * This object should be passed to the <CODE>setReferralHandler</CODE> method. |
| 197 | * Alternatively, the <CODE>LDAPBind</CODE> object identifies an |
| 198 | * authentication mechanism to be used instead of the default |
| 199 | * authentication mechanism when following referrals. This |
| 200 | * object should be passed to the <CODE>setBindHandler</CODE> method. |
| 201 | * @param doReferrals set to <CODE>true</CODE> if referrals should be |
| 202 | * followed automatically, or <CODE>False</CODE> if referrals should throw |
| 203 | * an <CODE>LDAPReferralException</CODE> |
| 204 | * @see org.ietf.ldap.LDAPBindHandler |
| 205 | * @see org.ietf.ldap.LDAPAuthHandler |
| 206 | * @see org.ietf.ldap.LDAPAuthProvider |
| 207 | */ |
| 208 | public void setReferralFollowing( boolean doReferrals ) { |
| 209 | referrals = doReferrals; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Specifies the object that provides the method for getting |
| 214 | * authentication information. This object must belong to a class |
| 215 | * that implements the <CODE>LDAPReferralHandler</CODE> interface. |
| 216 | * (By default, this is <CODE>null</CODE>.) This method sets the |
| 217 | * <CODE>LDAPReferralHandler</CODE> object to null for this constraint. |
| 218 | * @param handler object to use to obtain information for |
| 219 | * authenticating to other LDAP servers during referrals |
| 220 | */ |
| 221 | public void setReferralHandler( LDAPReferralHandler handler ) { |
| 222 | _referralHandler = handler; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Sets maximum number of hops to follow in sequence during a referral. |
| 227 | * (By default, this is 5.) |
| 228 | * @param hop_limit maximum number of hops to follow during a referral |
| 229 | */ |
| 230 | public void setHopLimit( int hop_limit ) { |
| 231 | _hop_limit = hop_limit; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Sets a server control for LDAP operations. |
| 236 | * @param control server control for LDAP operations |
| 237 | * @see org.ietf.ldap.LDAPControl |
| 238 | */ |
| 239 | public void setControls( LDAPControl control ) { |
| 240 | _serverControls = new LDAPControl[1]; |
| 241 | _serverControls[0] = control; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Sets an array of server controls for LDAP operations. |
| 246 | * @param controls an array of server controls for LDAP operations |
| 247 | * @see org.ietf.ldap.LDAPControl |
| 248 | */ |
| 249 | public void setControls( LDAPControl[] controls ) { |
| 250 | _serverControls = controls; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Sets a property of the constraints object. |
| 255 | * No property names have been defined at this time, but the mechanism |
| 256 | * is in place in order to support revisional as well as dynamic and |
| 257 | * proprietary extensions to operation modifiers. |
| 258 | * |
| 259 | * @param name Name of the property to set |
| 260 | * @param value Value to assign to the property |
| 261 | */ |
| 262 | public void setProperty( String name, Object value ) throws LDAPException { |
| 263 | if ( _properties == null ) { |
| 264 | _properties = new Hashtable<>(); |
| 265 | } |
| 266 | _properties.put( name, value ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Return a string representation of the object for debugging |
| 271 | * |
| 272 | * @return A string representation of the object |
| 273 | */ |
| 274 | public String toString() { |
| 275 | StringBuffer sb = new StringBuffer("LDAPConstraints {"); |
| 276 | sb.append("time limit " + getTimeLimit() + ", "); |
| 277 | sb.append("referrals " + getReferralFollowing() + ", "); |
| 278 | sb.append("hop limit " + getHopLimit() + ", "); |
| 279 | sb.append("referral handler " + getReferralHandler() + ", "); |
| 280 | LDAPControl[] controls = getControls(); |
| 281 | if ( controls != null ) { |
| 282 | sb.append(", server controls "); |
| 283 | for (int i =0; i < controls.length; i++) { |
| 284 | sb.append(controls[i].toString()); |
| 285 | if ( i < (controls.length - 1) ) { |
| 286 | sb.append(" "); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | sb.append('}'); |
| 291 | |
| 292 | return sb.toString(); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Makes a copy of an existing set of constraints. |
| 297 | * @return a copy of an existing set of constraints |
| 298 | */ |
| 299 | public Object clone() { |
| 300 | LDAPConstraints o = new LDAPConstraints(); |
| 301 | |
| 302 | o._time_limit = this._time_limit; |
| 303 | o.referrals = this.referrals; |
| 304 | o._referralHandler = this._referralHandler; |
| 305 | o._hop_limit = this._hop_limit; |
| 306 | if ( (this._serverControls != null) && |
| 307 | (this._serverControls.length > 0) ) { |
| 308 | o._serverControls = new LDAPControl[this._serverControls.length]; |
| 309 | for( int i = 0; i < this._serverControls.length; i++ ) |
| 310 | o._serverControls[i] = |
| 311 | (LDAPControl)this._serverControls[i].clone(); |
| 312 | } |
| 313 | return o; |
| 314 | } |
| 315 | } |