Created
April 4, 2012 03:10
-
-
Save mryoshio/2297461 to your computer and use it in GitHub Desktop.
LDAP Client test (import user)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This program is under LGPL v2.1 as Liferay. | |
*/ | |
package jp.hoge.liferay.experiment.ldap; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Hashtable; | |
import java.util.List; | |
import java.util.Properties; | |
import javax.naming.Context; | |
import javax.naming.NamingEnumeration; | |
import javax.naming.NamingException; | |
import javax.naming.OperationNotSupportedException; | |
import javax.naming.directory.SearchControls; | |
import javax.naming.directory.SearchResult; | |
import javax.naming.ldap.Control; | |
import javax.naming.ldap.InitialLdapContext; | |
import javax.naming.ldap.LdapContext; | |
import javax.naming.ldap.PagedResultsControl; | |
import javax.naming.ldap.PagedResultsResponseControl; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
/** | |
* | |
* @author mryoshio | |
* | |
*/ | |
public class LDAPClientTest { | |
private static Log logger = LogFactory.getLog(LDAPClientTest.class); | |
private static final String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; | |
private static final String SECURITY_CREDENTIALS = "admin_of_ldap@your.domain.jp"; | |
private static final String REFERRAL = "follow"; | |
private static final String LDAP_BASE_DN = "dc=your,dc=domain,dc=jp"; | |
private static final String LDAP_IMPORT_USER_SEARCH_FILTER = "(objectClass=inetOrgPerson)"; | |
private static String SECURITY_PRINCIPAL = "cn=Manager,dc=your,dc=domain,dc=jp"; | |
private static String PROVIDER_URL = "ldap://<youda ldap ip>:<ldap port>"; | |
private static int LDAP_PAGE_SIZE = 1000; | |
private static final String PROP_DIR = "./props"; | |
/** | |
* Main method | |
* | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
try { | |
importFromLDAP(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Initialize LDAP context | |
* | |
* @return | |
* @throws NamingException | |
*/ | |
private static LdapContext buildLdapContext() throws NamingException { | |
LdapContext ldapContext = null; | |
Hashtable<String, Object> env = new Hashtable<String, Object>(11); | |
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY); | |
env.put(Context.PROVIDER_URL, PROVIDER_URL); | |
env.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL); | |
env.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS); | |
env.put(Context.REFERRAL, REFERRAL); | |
ldapContext = new InitialLdapContext(env, null); | |
logger.debug("ldapContext: " + ldapContext.getEnvironment()); | |
return ldapContext; | |
} | |
/** | |
* | |
* @param name | |
* @return | |
* @throws FileNotFoundException | |
* @throws IOException | |
*/ | |
private static Properties buildMappings(String name) | |
throws FileNotFoundException, IOException { | |
String path = PROP_DIR + name; | |
Properties props = new Properties(); | |
props.load(new FileInputStream(new File(path))); | |
return props; | |
} | |
/** | |
* @see PortalLDAPImporterImpl | |
* @param ldapServerId | |
* @param companyId | |
*/ | |
public static void importFromLDAP() throws Exception { | |
LdapContext ldapContext = buildLdapContext(); | |
if (ldapContext == null) | |
throw new Exception("LdapContext must not be null!"); | |
Properties userMappings = buildMappings("/userMapping.txt"); | |
Properties groupMappings = buildMappings("/groupMapping.txt"); | |
importFromLDAPByUser(ldapContext, userMappings, groupMappings); | |
} | |
/** | |
* @see PortalLDAPImporterImpl | |
*/ | |
public static void importFromLDAPByUser(LdapContext ldapContext, | |
Properties userMappings, Properties groupMappings) throws Exception { | |
{ | |
int trial = 0; | |
byte[] cookie = new byte[0]; | |
while (cookie != null) { | |
logger.debug("####### trial: " + ++trial); | |
List<SearchResult> searchResults = new ArrayList<SearchResult>(); | |
String userMappingsScreenName = userMappings | |
.getProperty("screenName"); | |
cookie = getUsers(ldapContext, cookie, 0, | |
new String[] { userMappingsScreenName }, searchResults); | |
int cnt = 0; | |
for (SearchResult searchResult : searchResults) { | |
logger.info("no" + cnt++ + ": " | |
+ searchResult.getAttributes()); | |
// we don't need the code here because import from LDAP is | |
// finshed | |
} | |
} | |
} | |
} | |
/** | |
* @see PortalLDAPUtil.java | |
* @param ldapServerId | |
* @param companyId | |
* @param ldapContext | |
* @param cookie | |
* @param maxResults | |
* @param attributeIds | |
* @param searchResults | |
* @return | |
* @throws Exception | |
*/ | |
public static byte[] getUsers(LdapContext ldapContext, byte[] cookie, | |
int maxResults, String[] attributeIds, | |
List<SearchResult> searchResults) throws Exception { | |
String baseDN = LDAP_BASE_DN; | |
String userFilter = LDAP_IMPORT_USER_SEARCH_FILTER; | |
return getUsers(ldapContext, cookie, maxResults, baseDN, userFilter, | |
attributeIds, searchResults); | |
} | |
/** | |
* | |
* @param companyId | |
* @param ldapContext | |
* @param cookie | |
* @param maxResults | |
* @param baseDN | |
* @param userFilter | |
* @param attributeIds | |
* @param searchResults | |
* @return | |
* @throws Exception | |
*/ | |
public static byte[] getUsers(LdapContext ldapContext, byte[] cookie, | |
int maxResults, String baseDN, String userFilter, | |
String[] attributeIds, List<SearchResult> searchResults) | |
throws Exception { | |
return searchLDAP(ldapContext, cookie, maxResults, baseDN, userFilter, | |
attributeIds, searchResults); | |
} | |
/** | |
* @see PortalLDAPUtil | |
* @throws NamingException | |
* @throws IOException | |
*/ | |
public static byte[] searchLDAP(LdapContext ldapContext, byte[] cookie, | |
int maxResults, String baseDN, String filter, | |
String[] attributeIds, List<SearchResult> searchResults) | |
throws NamingException, IOException { | |
SearchControls searchControls = new SearchControls( | |
SearchControls.SUBTREE_SCOPE, maxResults, 0, attributeIds, | |
false, false); | |
try { | |
if (cookie != null) { | |
if (cookie.length == 0) { | |
ldapContext | |
.setRequestControls(new Control[] { new PagedResultsControl( | |
LDAP_PAGE_SIZE, Control.CRITICAL) }); | |
} else { | |
ldapContext | |
.setRequestControls(new Control[] { new PagedResultsControl( | |
LDAP_PAGE_SIZE, cookie, Control.CRITICAL) }); | |
} | |
NamingEnumeration<SearchResult> enu = ldapContext.search( | |
baseDN, filter, searchControls); | |
while (enu.hasMoreElements()) { | |
searchResults.add(enu.nextElement()); | |
} | |
enu.close(); | |
return _getCookie(ldapContext.getResponseControls()); | |
} | |
} catch (OperationNotSupportedException onse) { | |
ldapContext.setRequestControls(null); | |
NamingEnumeration<SearchResult> enu = ldapContext.search(baseDN, | |
filter, searchControls); | |
while (enu.hasMoreElements()) { | |
searchResults.add(enu.nextElement()); | |
} | |
enu.close(); | |
} finally { | |
ldapContext.setRequestControls(null); | |
} | |
return null; | |
} | |
/** | |
* | |
* @param controls | |
* @return | |
*/ | |
private static byte[] _getCookie(Control[] controls) { | |
if (controls == null) { | |
return null; | |
} | |
for (Control control : controls) { | |
if (control instanceof PagedResultsResponseControl) { | |
PagedResultsResponseControl pagedResultsResponseControl = (PagedResultsResponseControl) control; | |
return pagedResultsResponseControl.getCookie(); | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment