001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003: * JR Boyens <gnu-jrb[remove] at gmx dot net>
004: * Distributed under the terms of either:
005: * - the common development and distribution license (CDDL), v1.0; or
006: * - the GNU Lesser General Public License, v2.1 or later
007: * $Id: SessionValidatorRetriever.java 3643 2007-01-12 15:29:45Z gbevin $
008: */
009: package com.uwyn.rife.authentication;
010:
011: import com.uwyn.rife.authentication.SessionValidator;
012: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.AuthenticatedElementNotFoundException;
013: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.NotAuthenticatedElementException;
014: import com.uwyn.rife.authentication.elements.AuthenticatedDeployer;
015: import com.uwyn.rife.engine.ElementDeployer;
016: import com.uwyn.rife.engine.ElementInfo;
017: import com.uwyn.rife.engine.Site;
018:
019: /**
020: * This abstract class provides the functionalities to retrieve a {@link
021: * com.uwyn.rife.authentication.SessionValidator SessionValidator} from a
022: * particular {@link com.uwyn.rife.authentication.elements.Authenticated
023: * Authenticated} element in a site.
024: * <p>Since you can have many authentication schemes and backends being active
025: * in a single web application. it's quite verbose to retrieve a
026: * {@code SessionValidator} when you want to perform some operations on
027: * its stored credentials. This class provides the functionalities to quickly
028: * perform this retrieval.
029: *
030: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
031: * @author JR Boyens (gnu-jrb[remove] at gmx dot net)
032: * @see com.uwyn.rife.authentication.SessionValidator
033: * @see com.uwyn.rife.authentication.elements.Authenticated
034: * @version $Revision: 3643 $
035: * @since 1.0
036: */
037: public abstract class SessionValidatorRetriever {
038: /**
039: * Retrieves a {@code SessionValidator} manager from an
040: * {@code Authenticated} element in a {@code Site}.
041: *
042: * @param site the site in which the authenticated element is declared
043: * @param authElementId the absolute ID of the authenticated element that
044: * provides all the authentication related managers
045: * @param reference a reference element against which to resolve the id;
046: * or {@code null} if the provided id is absolute
047: * @exception AuthenticatedElementNotFoundException when the element ID
048: * couldn't be found in the site
049: * @exception NotAuthenticatedElementException when the element ID doesn't
050: * refer to an {@code Authenticated} element
051: * @since 1.0
052: */
053: public static SessionValidator getSessionValidator(Site site,
054: String authElementId, ElementInfo reference) {
055: if (null == site)
056: throw new IllegalArgumentException("site can't be null");
057: if (null == authElementId)
058: throw new IllegalArgumentException(
059: "authElementId can't be null");
060: if (0 == authElementId.length())
061: throw new IllegalArgumentException(
062: "authElementId can't be empty");
063:
064: ElementInfo auth_elementinfo = site.resolveId(authElementId,
065: reference);
066: if (null == auth_elementinfo) {
067: throw new AuthenticatedElementNotFoundException(
068: authElementId);
069: }
070:
071: return getSessionValidator(auth_elementinfo);
072: }
073:
074: /**
075: * Retrieves a {@code SessionValidator} manager from an
076: * {@code Authenticated} element in a {@code Site}.
077: *
078: * @param authElementInfo the {@code ElementInfo} of the authenticated
079: * element that provides all the authentication related managers
080: * @exception NotAuthenticatedElementException when the provided element info
081: * doesn't refer to an {@code Authenticated} element
082: * @since 1.4
083: */
084: public static SessionValidator getSessionValidator(
085: ElementInfo authElementInfo) {
086: if (null == authElementInfo)
087: throw new IllegalArgumentException(
088: "authElementInfo can't be null");
089:
090: ElementDeployer deployer = authElementInfo.getDeployer();
091: if (null == deployer
092: || !(deployer instanceof AuthenticatedDeployer)) {
093: throw new NotAuthenticatedElementException(authElementInfo
094: .getId());
095: }
096: AuthenticatedDeployer auth_deployer = (AuthenticatedDeployer) deployer;
097:
098: SessionValidator validator = auth_deployer
099: .getSessionValidator();
100:
101: return validator;
102: }
103: }
|