Java tutorial
/* * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license * distributed with this file and available online at * http://www.uportal.org/license.html */ package org.jasig.cas.authentication.principal; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.inspektr.common.ioc.annotation.NotNull; import org.jasig.services.persondir.IPersonAttributeDao; import org.jasig.services.persondir.support.StubPersonAttributeDao; /** * * @author Scott Battaglia * @version $Revision: 1.1 $ $Date: 2005/08/19 18:27:17 $ * @since 3.1 * */ public abstract class AbstractPersonDirectoryCredentialsToPrincipalResolver implements CredentialsToPrincipalResolver { /** Log instance. */ protected final Log log = LogFactory.getLog(this.getClass()); /** Repository of principal attributes to be retrieved */ @NotNull private IPersonAttributeDao attributeRepository = new StubPersonAttributeDao( new HashMap<String, List<Object>>()); public final Principal resolvePrincipal(final Credentials credentials) { if (log.isDebugEnabled()) { log.debug("Attempting to resolve a principal..."); } final String principalId = extractPrincipalId(credentials); if (principalId == null) { return null; } if (log.isDebugEnabled()) { log.debug("Creating SimplePrincipal for [" + principalId + "]"); } final Map<String, List<Object>> attributes = this.attributeRepository .getMultivaluedUserAttributes(principalId); final Map<String, Object> convertedAttributes = new HashMap<String, Object>(); for (final Map.Entry<String, List<Object>> entry : attributes.entrySet()) { final String key = entry.getKey(); final Object value = entry.getValue().size() == 1 ? entry.getValue().get(0) : entry.getValue(); convertedAttributes.put(key, value); } return new SimplePrincipal(principalId, convertedAttributes); } /** * Extracts the id of the user from the provided credentials. * * @param credentials the credentials provided by the user. * @return the username, or null if it could not be resolved. */ protected abstract String extractPrincipalId(Credentials credentials); public final void setAttributeRepository(final IPersonAttributeDao attributeRepository) { this.attributeRepository = attributeRepository; } }