Skip to main content

Java: Active directory paging code


Forgive some of the bad code this was a quick script and a reminder to self on how to do this: (note this code changes all the AD users passwords so be careful)

package common.jndi;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.PagedResultsControl;
import javax.naming.ldap.PagedResultsResponseControl;

public class ModifyLdapUserScript
    extends LdapBase {
    public static final int PAGE_SIZE = 999;
    private static final String DEFAULT_PASSWORD = "Password1";
    private static final String AD_BASE = "dc=dev,dc=system,dc=uni,dc=edu";

    public ModifyLdapUserScript() {
        super();
    }

    public static void main(String[] args)
        throws Exception {
        String adServer = "adserver";
        String adUser = "cn=Admin,cn=Users," + AD_BASE;
        String adPwd = "secretpassword";

        System.out.println("Starting user modification");
        InitialLdapContext ldapContext = null;
        byte[] cookie = null;
        int runningTotal = 0;
        try {
            long startTime = System.currentTimeMillis();
            ldapContext = connectAD(adServer, adUser, adPwd);

            // Create the search controls
            SearchControls searchCtls = new SearchControls();
            //Specify the attributes to return
            String returnedAtts[] = { "cn" };
            searchCtls.setReturningAttributes(returnedAtts);
            ldapContext.setRequestControls(new Control[] { new PagedResultsControl(PAGE_SIZE, Control.CRITICAL) });
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            //specify the LDAP search filter
            String searchFilter = "(&(cn=*)(objectclass=user))";

            //initialize counter to total the results
            PagedResult totalResults = new PagedResult();

            long innerStartTime = System.currentTimeMillis();
            NamingEnumeration answer = ldapContext.search(AD_BASE, searchFilter, searchCtls);
            totalResults = processPage(ldapContext, answer);
            System.out.println("Processed page 1 [" + totalResults.getResultSize() + "] in time:" +
                               (System.currentTimeMillis() - innerStartTime));
            runningTotal += totalResults.getResultSize();
            int i = 2;
            while (totalResults != null && totalResults.getCookie() != null) {
                innerStartTime = System.currentTimeMillis();
                ldapContext.setRequestControls(new Control[] { new PagedResultsControl(PAGE_SIZE, totalResults.getCookie(), Control.CRITICAL) });
                answer = ldapContext.search(AD_BASE, searchFilter, searchCtls);
                totalResults = processPage(ldapContext, answer);
                System.out.println("Processed page " + i + " [" + totalResults.getResultSize() + "] in time:" +
                                   (System.currentTimeMillis() - innerStartTime));
                runningTotal += totalResults.getResultSize();
                i++;
            }

            System.out.println("Time taken changing users[" + runningTotal + "]:" + (System.currentTimeMillis() - startTime));
        } finally {
            disconnectAD(ldapContext);
        }
        System.out.println("Finished modification");
    }

    private static PagedResult processPage(InitialLdapContext ldapContext, NamingEnumeration answer)
        throws NamingException {
        //Loop through the search results
        String adContent = null;
        PagedResult result = new PagedResult();
        int results = 0;
        while (answer != null && answer.hasMoreElements()) {
            SearchResult sr = answer.next();
            Attributes attrs = sr.getAttributes();
            if (attrs != null) {
                String cn = (String) getAttributeValue(attrs, "cn");
                StringBuffer buffer = new StringBuffer("cn=").append(cn).append(",cn=Users,").append(AD_BASE);
                changePassword(ldapContext, DEFAULT_PASSWORD, buffer.toString());
            }
        }

        Control[] controls = ldapContext.getResponseControls();
        if (controls != null) {
            for (int i = 0; i < controls.length; i++) {
                if (controls[i] instanceof PagedResultsResponseControl) {
                    PagedResultsResponseControl prrc = (PagedResultsResponseControl)controls[i];
                    result.setCookie(prrc.getCookie());
                    result.setResultSize(prrc.getResultSize());
                } else {
                    // Handle other response controls (if any)
                }
            }
        }

        return result;
    }

    public static void changePassword(InitialDirContext context, String password, String username)
        throws NamingException {

        System.out.println("Context Sucessfully Initialized");
        ModificationItem[] mods = new ModificationItem[1];
        Attribute mod0 = new BasicAttribute("userpassword", password);
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
        context.modifyAttributes(username, mods);
    }
}

Comments

Popular posts from this blog

MANIFEST.MF merge JDeveloper for an executable jar

Goto your project > properties. Then click on deployment in the menu. Edit or add a jar deployment profile. Fill in the details under jar options (select Include manifest and give it a main class name) Also remember that the merge functionality only works with a BLANK line at the end of the merge file. REALLY this caught me. My merge file contents: Class-Path: commons-codec-1.3.jar [...empty line here CRLF...]

ADF: LOV Description instead of code

I keep on forgetting how to do this so this is a note to self in terms I understand (ie pictures): Add the related entiy object to yours (in the example we are adding RegionEO(list of values) to CountryEO) Now add the RegionName field from the EO and a transient attribute (I named mine RegionNameLOV) Make the transient attribute updateable and base it on the expression RegionName (the description you wish to display) Add a list of values to RegionNameLOV and map BOTH key to parent fk (region id here) AND RegionName to your transient coulmn (RegionNameLOV).  Map the transient as an Input text with List of Values. Then just drag the lov item onto the page (RegionNameLOV)

OJDeploy: Documentation for the tool

Real DOCS:  http://docs.oracle.com/cd/E26098_01/user.1112/e17455/deploying_apps.htm#OJDUG645 OJDeploy Documentation if you run it from the command line - I keep looking for this so I though I would post it here so I remeber. Oracle JDeveloper Deploy 11.1.2.1.0.6081 Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. Usage:   ojdeploy -profile <name> -workspace <jws> [ -project <name> ] [ <options> ]   ojdeploy -buildfile <ojbuild.xml> [ <options> ]   ojdeploy -buildfileschema Arguments:   -profile               the name of the Profile to deploy   -workspace      full path to the JDeveloper Workspace file(.jws)   -project              name of the JDeveloper Project within the .jws where the Profile can be...