Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 2011 Tirasa. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  */
 package org.connid.ad.util;
 
 
 public class DirSyncUtils {
 
     public static String createDirSyncFilter(final ADConfiguration conf) {
 
         final String[] memberships = conf.getMemberships();
 
         final String isDeleted =
                 String.valueOf(conf.isRetrieveDeletedUser()).toUpperCase();
 
         final StringBuilder filter = new StringBuilder();
         final StringBuilder mfilter = new StringBuilder();
         final StringBuilder ufilter = new StringBuilder();
 
         mfilter.append("(objectClass=group)");
 
         if (memberships != null && memberships.length > 0) {
             ufilter.append(conf.isMembershipsInOr() ? "(|" : "(&");
 
             for (String group : memberships) {
                 ufilter.append("(memberOf=").append(group).append(")");
             }
 
             ufilter.append(")");
         }
 
         ufilter.insert(0, "(&(objectClass=user)").append(")");
 
         filter.append("(|").append(ufilter).append(mfilter).
                 append("(&(isDeleted=").
                 append(isDeleted).
                 append(")(objectClass=user)))");
 
         return filter.toString();
     }
 
     public static String createLdapFilter(final ADConfiguration conf) {
 
         final String[] memberships = conf.getMemberships();
 
         final String isDeleted =
                 String.valueOf(conf.isRetrieveDeletedUser()).toUpperCase();
 
         final StringBuilder filter = new StringBuilder();
         final StringBuilder mfilter = new StringBuilder();
         final StringBuilder ufilter = new StringBuilder();
 
         if (memberships != null && memberships.length > 0) {
             mfilter.append("(&(objectClass=group)(|");
             ufilter.append(conf.isMembershipsInOr() ? "(|" : "(&");
 
             for (String group : memberships) {
                 mfilter.append("(distinguishedName=").append(group).append(")");
                 ufilter.append("(memberOf=").append(group).append(")");
             }
 
             ufilter.append(")");
             mfilter.append("))");
         }
 
         ufilter.insert(0, "(&(objectClass=user)").append(")");
 
         filter.append("(|").append(ufilter).append(mfilter).
                 append("(&(isDeleted=").
                 append(isDeleted).
                 append(")(objectClass=user)))");
 
        return filter.toString();
    }
    private static String AddLeadingZero(int k) {
        return (k <= 0xF)
                ? "0" + Integer.toHexString(k) : Integer.toHexString(k);
    }
    public static String getGuidAsString(byte[] GUID) {
        String strGUID = "";
        String byteGUID = "";
        for (int c = 0; c < GUID.lengthc++) {
            byteGUID = byteGUID + "\\" + AddLeadingZero((intGUID[c] & 0xFF);
        }
        //convert the GUID into string format
        strGUID = strGUID + AddLeadingZero((intGUID[3] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[2] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[1] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[0] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[5] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[4] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[7] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[6] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[8] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[9] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[10] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[11] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[12] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[13] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[14] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[15] & 0xFF);
        return strGUID;
    }

    
Verify custom filter (used to validate any retrieved user).

Parameters:
ctx ldap context.
dn user distinguished name.
conf connector configuration.
Returns:
TRUE if verified; FALSE otherwise.
    public static boolean verifyCustomFilter(
            final LdapContext ctx,
            final String dn,
            final ADConfiguration conf) {
        final String filter = getFilter(conf);
        final SearchControls searchCtls =
                LdapInternalSearch.createDefaultSearchControls();
        searchCtls.setSearchScope(.);
        searchCtls.setReturningAttributes(new String[]{});
        boolean found = true;
        if (StringUtil.isNotBlank(filter)) {
            try {
                final NamingEnumeration res =
                        ctx.search(dnfiltersearchCtls);
                found = res != null && res.hasMoreElements();
            } catch (NamingException ex) {
                found = false;
            }
        }
        return found;
    }

    
Verify complete filter including the custom one. This method is used to validate users 'IN' group.

Parameters:
ctx ldap context.
dn user distinguished name.
conf connector configuration.
Returns:
TRUE if verified; FALSE otherwise.
    public static boolean verifyFilter(
            final LdapContext ctx,
            final String dn,
            final ADConfiguration conf) {
        final StringBuilder filter = new StringBuilder();
        filter.append("(&(").append(createLdapFilter(conf)).append(")");
        filter.append(getFilter(conf) != null ? getFilter(conf) : "").append(")");
        final SearchControls searchCtls =
                LdapInternalSearch.createDefaultSearchControls();
        searchCtls.setSearchScope(.);
        searchCtls.setReturningAttributes(new String[]{});
        boolean found = true;
        if (StringUtil.isNotBlank(filter.toString())) {
            try {
                final NamingEnumeration res =
                        ctx.search(dnfilter.toString(), searchCtls);
                found = res != null && res.hasMoreElements();
            } catch (NamingException ex) {
                found = false;
            }
        }
        return found;
    }
    private static String getFilter(final AbstractConfiguration conf) {
        return ((ADConfigurationconf).getAccountSearchFilter();
    }
New to GrepCode? Check out our FAQ X