You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by sz...@apache.org on 2009/05/21 21:51:37 UTC

svn commit: r777233 - /directory/sandbox/szoerner/apacheds-tomcatrealm/src/main/java/org/apache/directory/tomcatrealm/EmbeddedApacheDsRealm.java

Author: szoerner
Date: Thu May 21 19:51:36 2009
New Revision: 777233

URL: http://svn.apache.org/viewvc?rev=777233&view=rev
Log:
Logging modified to commons logging

Modified:
    directory/sandbox/szoerner/apacheds-tomcatrealm/src/main/java/org/apache/directory/tomcatrealm/EmbeddedApacheDsRealm.java

Modified: directory/sandbox/szoerner/apacheds-tomcatrealm/src/main/java/org/apache/directory/tomcatrealm/EmbeddedApacheDsRealm.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/apacheds-tomcatrealm/src/main/java/org/apache/directory/tomcatrealm/EmbeddedApacheDsRealm.java?rev=777233&r1=777232&r2=777233&view=diff
==============================================================================
--- directory/sandbox/szoerner/apacheds-tomcatrealm/src/main/java/org/apache/directory/tomcatrealm/EmbeddedApacheDsRealm.java (original)
+++ directory/sandbox/szoerner/apacheds-tomcatrealm/src/main/java/org/apache/directory/tomcatrealm/EmbeddedApacheDsRealm.java Thu May 21 19:51:36 2009
@@ -21,6 +21,7 @@
 
 import java.security.Principal;
 import java.text.MessageFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -42,6 +43,9 @@
 import org.apache.directory.shared.ldap.message.AliasDerefMode;
 import org.apache.directory.shared.ldap.name.LdapDN;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * An Apache Tomcat realm whichs embeds Apache Directory Server.
  * 
@@ -52,6 +56,8 @@
 
 	private static final String REALM_NAME = "EmbeddedApacheDsRealm";
 
+	private static Log log = LogFactory.getLog(EmbeddedApacheDsRealm.class);
+
 	private DirectoryService directoryService;
 
 	private SocketAcceptor socketAcceptor;
@@ -66,6 +72,9 @@
 	 */
 	@Override
 	public void start() throws LifecycleException {
+
+		log.info("Starting Embeded ApacheDS");
+
 		try {
 
 			directoryService = new DefaultDirectoryService();
@@ -85,7 +94,7 @@
 			directoryService.startup();
 			ldapService.start();
 		} catch (Exception e) {
-			e.printStackTrace();
+			log.error("Starting failed", e);
 			throw new LifecycleException(e);
 		}
 	}
@@ -95,11 +104,14 @@
 	 */
 	@Override
 	public void stop() throws LifecycleException {
+
+		log.info("Stopping Embeded ApacheDS");
+
 		try {
 			ldapService.stop();
 			directoryService.shutdown();
 		} catch (Exception e) {
-			e.printStackTrace();
+			log.error("Stopping failed", e);
 			throw new LifecycleException(e);
 		}
 	}
@@ -114,38 +126,57 @@
 	}
 
 	/**
+	 * Return a short name for this Realm implementation, for use in log
+	 * messages.
+	 */
+	@Override
+	protected String getName() {
+		return REALM_NAME;
+	}
+
+	/**
 	 * Return the Principal associated with the specified username and
 	 * credentials, if there is one; otherwise return null.
 	 */
 	@Override
 	public Principal authenticate(String username, String credentials) {
-		System.out.println("authenticate " + username);
 
-		LdapDN userDN = this.findDnForUsername(username);
+		Principal principal = null;
+
+		if (log.isDebugEnabled()) {
+			log.debug("try to authenticat user " + username);
+		}
+
+		LdapDN userDN = this.searchEntryByUsername(username);
 		if (userDN != null) {
 
-			System.out.println("upName = " + userDN.getUpName());
-			System.out.println("normName = " + userDN.getNormName());
+			if (log.isDebugEnabled()) {
+				log.debug("Found user with DN " + userDN.getUpName());
+			}
 
 			if (this.authenticate(userDN, credentials)) {
+
+				log.debug("Authentication succesful for user " + username);
+
 				List<String> roles = this.searchRolesForUserDn(userDN);
-				return new GenericPrincipal(this, username, credentials, roles);
+				principal = new GenericPrincipal(this, username, credentials,
+						roles);
+			} else {
+				log.debug("Authentication failed for user " + username);
 			}
 
 		} else {
-			System.out.println("User " + username + " not found in directory");
+			if (log.isDebugEnabled()) {
+				log.debug("User " + username + " not found in directory");
+			}
 		}
 
-		return null;
+		return principal;
 	}
 
-	/**
-	 * Return a short name for this Realm implementation, for use in log
-	 * messages.
-	 */
 	@Override
-	protected String getName() {
-		return REALM_NAME;
+	protected Principal getPrincipal(String username) {
+		return null;
 	}
 
 	/**
@@ -158,7 +189,7 @@
 				"Operation getPassword not supported by this realm");
 	}
 
-	protected LdapDN findDnForUsername(String username) {
+	protected LdapDN searchEntryByUsername(String username) {
 		LdapDN dn = null;
 
 		try {
@@ -166,17 +197,12 @@
 
 			String base = "ou=system";
 			LdapDN basedn = new LdapDN(base);
-			SearchScope scope = SearchScope.SUBTREE;
+			ExprNode filter = this.createFilter(
+					"(&(objectClass=person)(uid={0}))", username);
 
-			MessageFormat f = new MessageFormat(
-					"(&(objectClass=person)(uid={0}))");
-			String sFilter = f.format(new Object[] { username });
-
-			System.out.println("Filter = " + sFilter);
-
-			ExprNode filter = FilterParser.parse(sFilter);
-			EntryFilteringCursor cursor = adminSession.search(basedn, scope,
-					filter, AliasDerefMode.NEVER_DEREF_ALIASES, null, 0, 0);
+			EntryFilteringCursor cursor = adminSession.search(basedn,
+					SearchScope.SUBTREE, filter,
+					AliasDerefMode.NEVER_DEREF_ALIASES, null, 0, 0);
 
 			CursorIterator iter = new CursorIterator(cursor);
 			if (iter.hasNext()) {
@@ -211,25 +237,18 @@
 
 			String base = "ou=system";
 			LdapDN basedn = new LdapDN(base);
-			SearchScope scope = SearchScope.SUBTREE;
-
-			MessageFormat f = new MessageFormat(
-					"(&(objectClass=groupOfNames)(member={0}))");
-			String sFilter = f.format(new Object[] { userDn.getUpName() });
-
-			System.out.println("Filter = " + sFilter);
-
-			ExprNode filter = FilterParser.parse(sFilter);
-			EntryFilteringCursor cursor = adminSession.search(basedn, scope,
-					filter, AliasDerefMode.NEVER_DEREF_ALIASES, null, 0, 0);
+			ExprNode filter = this.createFilter(
+					"(&(objectClass=groupOfNames)(member={0}))", userDn
+							.getUpName());
+
+			EntryFilteringCursor cursor = adminSession.search(basedn,
+					SearchScope.SUBTREE, filter,
+					AliasDerefMode.NEVER_DEREF_ALIASES, null, 0, 0);
 
 			CursorIterator iter = new CursorIterator(cursor);
 			while (iter.hasNext()) {
 				ServerEntry entry = (ServerEntry) iter.next();
 				EntryAttribute attr = entry.get("cn");
-
-				System.out.println(attr);
-
 				roles.add(attr.getString());
 			}
 			adminSession.unbind();
@@ -241,8 +260,15 @@
 		return roles;
 	}
 
-	@Override
-	protected Principal getPrincipal(String username) {
-		return null;
+	protected ExprNode createFilter(String filterPattern, Object... filterArgs)
+			throws ParseException {
+
+		MessageFormat f = new MessageFormat(filterPattern);
+		String sFilter = f.format(filterArgs);
+		ExprNode filter = FilterParser.parse(sFilter);
+
+		return filter;
+
 	}
+
 }