001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: SearchControl.java,v 1.5 2009/01/28 05:34:51 ww203982 Exp $ 026 * 027 */ 028 029package com.iplanet.ums; 030 031import java.util.Hashtable; 032 033import com.sun.identity.shared.ldap.LDAPv2; 034 035/** 036 * This class provides a way to customize Search behaviors. Common behaviors are 037 * time limit, result limit and Virtual list view. In future, we will provide 038 * ways for client to define different hierarchical tree through SearchControl. 039 * 040 * @supported.api 041 */ 042public class SearchControl implements java.io.Serializable { 043 044 private static final long serialVersionUID = -8755868973524858945L; 045 046 static final String KeyVlvRange = "vlvRange"; 047 048 static final String KeyVlvJumpTo = "vlvJumpTo"; 049 050 static final String KeyTimeOut = "timeOut"; 051 052 static final String KeySortKeys = "sortKeys"; 053 054 static final String KeyMaxResults = "maxResults"; 055 056 static final String KeySearchScope = "searchScope"; 057 058 static final int DEFAULT_MAX_RESULTS = 0; 059 060 static final int DEFAULT_TIMEOUT = 0; 061 062 // Disabled by default 063 private static boolean getAllAttributesEnabled = false; 064 065 /** 066 * Search scope for one level. You use this search scope in getting 067 * immediate children of a container. This is the default search scope in 068 * getChildren method in search API. One can use 069 * SearchControl.setSearchScope to override the default search scope in 070 * getChildren. 071 * 072 * @supported.api 073 */ 074 public static final int SCOPE_ONE = LDAPv2.SCOPE_ONE; 075 076 /** 077 * Search scope for subtree level. This scope is used as the default search 078 * scope in the search API. One can use SearchControl.setSearchScope to 079 * override the default search scope in search methods. 080 * 081 * @supported.api 082 */ 083 public static final int SCOPE_SUB = LDAPv2.SCOPE_SUB; 084 085 /** 086 * Search scope for just this object. 087 * 088 * @supported.api 089 */ 090 public static final int SCOPE_BASE = LDAPv2.SCOPE_BASE; 091 092 /** 093 * Set sort order based on attribute names. 094 * 095 * @param attributeNames 096 * array of attribute names to sort on 097 * @supported.api 098 */ 099 public void setSortKeys(String[] attributeNames) { 100 SortKey[] sortKeys; 101 if (attributeNames == null) 102 return; 103 104 sortKeys = new SortKey[attributeNames.length]; 105 for (int i = 0; i < sortKeys.length; i++) { 106 sortKeys[i] = new SortKey(); 107 sortKeys[i].attributeName = attributeNames[i]; 108 sortKeys[i].reverse = false; 109 } 110 111 set(KeySortKeys, sortKeys); 112 } 113 114 /** 115 * Set sort order based on SortKey 116 * 117 * @param sortKeys 118 * array of SortKey. 119 * @supported.api 120 */ 121 public void setSortKeys(SortKey[] sortKeys) { 122 set(KeySortKeys, sortKeys); 123 } 124 125 /** 126 * Get existing attribute names for sorting. 127 * @supported.api 128 */ 129 public SortKey[] getSortKeys() { 130 return (SortKey[]) get(KeySortKeys); 131 } 132 133 /** 134 * Set range for retrieving VLV data. 135 * 136 * @param startIndex 137 * starting position 138 * @param beforeCount 139 * Number of entries before the startIndex 140 * @param afterCount 141 * Number of entries after the startIndex. 142 * @supported.api 143 */ 144 public void setVLVRange(int startIndex, int beforeCount, int afterCount) { 145 int[] range = new int[3]; 146 range[0] = startIndex; 147 range[1] = beforeCount; 148 range[2] = afterCount; 149 150 set(KeyVlvRange, range); 151 } 152 153 /** 154 * Set range for retrieving VLV data. 155 * 156 * @param jumpTo 157 * Search expression defining the result set return 158 * @param beforeCount 159 * Number of entries before the startIndex 160 * @param afterCount 161 * Number of entries after the startIndex. 162 * @supported.api 163 */ 164 public void setVLVRange(String jumpTo, int beforeCount, int afterCount) { 165 int[] range = new int[3]; 166 167 range[0] = 0; 168 range[1] = beforeCount; 169 range[2] = afterCount; 170 171 set(KeyVlvJumpTo, jumpTo); 172 set(KeyVlvRange, range); 173 } 174 175 /** 176 * Get range for current VLV setting. 177 * 178 * @return array of int which contain startIndex, beforeCount and 179 * afterCount. 180 * @supported.api 181 */ 182 public int[] getVLVRange() { 183 return (int[]) get(KeyVlvRange); 184 } 185 186 /** 187 * Get jumpTo value for VLV range. 188 * 189 * @return jumpTo value. 190 * @supported.api 191 */ 192 public String getVLVJumpTo() { 193 return (String) get(KeyVlvJumpTo); 194 } 195 196 /** 197 * Sets the maximum number of milliseconds to wait for any operation for the 198 * search. 199 * 200 * @param timeOut 201 * Max number of milliseconds. 202 * @supported.api 203 */ 204 public void setTimeOut(int timeOut) { 205 set(KeyTimeOut, new Integer(timeOut)); 206 } 207 208 /** 209 * Get current time out setting. 210 * @supported.api 211 */ 212 public int getTimeOut() { 213 Integer i = (Integer) get(KeyTimeOut); 214 if (i == null) { 215 return DEFAULT_TIMEOUT; 216 } else { 217 return i.intValue(); 218 } 219 } 220 221 /** 222 * Sets the maximum number of search results to return; 0 means there is no 223 * limit. 224 * @supported.api 225 */ 226 public void setMaxResults(int maxNumber) { 227 set(KeyMaxResults, new Integer(maxNumber)); 228 } 229 230 /** 231 * Gets the maximum number of search results to return. return 0 means there 232 * is no limit. 233 * @supported.api 234 */ 235 public int getMaxResults() { 236 Integer i = (Integer) get(KeyMaxResults); 237 if (i == null) { 238 return DEFAULT_MAX_RESULTS; 239 } else { 240 return i.intValue(); 241 } 242 } 243 244 /** 245 * Sets the search scope in SearchControl. 246 * 247 * @param scope 248 * Search scope defined in the SearchControl to be used with the 249 * search API. 250 * @supported.api 251 */ 252 public void setSearchScope(int scope) { 253 set(KeySearchScope, new Integer(scope)); 254 } 255 256 /** 257 * Gets the search scope defined in the SearchControl. 258 * 259 * @return search scope defined in the SearchControl. If search scope is 260 * never defined in the SearchControl SCOPE_SUB for subtree type of 261 * search is assumed. 262 * @supported.api 263 */ 264 public int getSearchScope() { 265 Integer scope = (Integer) get(KeySearchScope); 266 if (scope != null) { 267 return scope.intValue(); 268 } else { 269 return SearchControl.SCOPE_SUB; 270 } 271 } 272 273 /** 274 * Gets the search scope defined in the SearchControl. Allow user to specify 275 * default search scope if nothing has been defined in the SearchControl 276 * yet. 277 * 278 * @param defaultScope 279 * Scope value to be used in case the SearchControl is not set up 280 * with a search scope 281 * 282 * @return Search scope defined in the SearchControl. Return defaultScope if 283 * scope is not defined in the control. 284 * @supported.api 285 */ 286 public int getSearchScope(int defaultScope) { 287 Integer scope = (Integer) get(KeySearchScope); 288 if (scope != null) { 289 return scope.intValue(); 290 } else { 291 return defaultScope; 292 } 293 } 294 295 /** 296 * Sets internal attribute value in SearchControl 297 */ 298 protected void set(String name, Object o) { 299 m_hashTable.put(name, o); 300 } 301 302 /** 303 * Gets internal attribute defined in SearchControl 304 * 305 * @param name 306 * Name of attribute to get 307 * @return Object representing the value of the attribute. Return null 308 * object if the given attribute name is not found 309 */ 310 protected Object get(String name) { 311 return m_hashTable.get(name); 312 } 313 314 /** 315 * Checks if an internal attribute is defined for the control 316 * 317 * @param name 318 * Name of internal attribute to check against 319 * @return <code>true</code> if internal attribute is defined in the 320 * control and <code>false</code> otherwise 321 */ 322 protected boolean contains(String name) { 323 return m_hashTable.containsKey(name); 324 } 325 326 /** 327 * Sets the specified boolean value to the variable. Boolean value is set to 328 * true, if all attributes of the entries need to be obtained as part of the 329 * search. 330 * 331 * NOTE: If this getAllReturnAttributes boolean is set to true as part of 332 * AMSearchControl, it overrides any other setReturnAttributes set as part 333 * of the AMSearchControl. This is similar to using a wildcard '*' in 334 * search. 335 * 336 * When all the return attributes are set, the return attributes can be 337 * obtained as a map with DN as map-key and set of attribute values as 338 * map-value from AMSearchResults object. 339 * 340 * @param getAllAttributes 341 * Boolean value set to true as part of the AMSearchControl to 342 * obtain all attributes as part of the search. 343 * 344 * 345 */ 346 public void setAllReturnAttributes(boolean getAllAttributes) { 347 getAllAttributesEnabled = getAllAttributes; 348 } 349 350 /** 351 * Method to check if the boolean getAllAttributesEnabled is enabled or 352 * disabled. 353 * 354 * @return Returns the value of the boolean getAllAttributesEnabled. Returns 355 * true or false. 356 */ 357 public boolean isGetAllReturnAttributesEnabled() { 358 return getAllAttributesEnabled; 359 } 360 361 private Hashtable m_hashTable = new Hashtable(); 362}
Copyright © 2010-2017, ForgeRock All Rights Reserved.