1   package com.atlassian.user.util;
2   
3   import com.opensymphony.util.TextUtils;
4   import net.sf.ldaptemplate.support.filter.AndFilter;
5   import net.sf.ldaptemplate.support.filter.Filter;
6   import org.apache.log4j.Category;
7   
8   import javax.naming.directory.SearchControls;
9   
10  public class LDAPUtils
11  {
12      public static final Category log = Category.getInstance(LDAPUtils.class);
13  
14      /**
15       * "ANDS" the two filters passed in. Either filter may be null, in which case the non-null filter is returned.
16       */
17      public static Filter makeAndFilter(Filter filter1, Filter filter2)
18      {
19          if (filter1 == null)
20          {
21              return filter2;
22          }
23          if (filter2 == null)
24          {
25              return filter1;
26          }
27          return new AndFilter().and(filter1).and(filter2);
28      }
29  
30      public static boolean isValidFilter(String filter)
31      {
32          return TextUtils.stringSet(filter) && filter.startsWith("(") && filter.endsWith(")") && filter.indexOf("=") != -1;
33      }
34  
35      public static SearchControls createSearchControls(String[] attributesToReturn, boolean searchAllDepths, int timeLimitMillis)
36      {
37          SearchControls ctls = new SearchControls();
38  
39          int searchScope = searchAllDepths ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE;
40          ctls.setSearchScope(searchScope);
41  
42          if (attributesToReturn != null)
43              ctls.setReturningAttributes(attributesToReturn);
44  
45          ctls.setTimeLimit(timeLimitMillis);
46  
47          return ctls;
48      }
49  }