001: package org.esupportail.cas.server.util;
002:
003: import org.dom4j.Element;
004: import org.esupportail.cas.server.util.log.Debug;
005:
006: /**
007: * This class implements a basic handler, without any property.
008: * This abstract class is inherited by any handler.
009: *
010: * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
011: */
012: public abstract class BasicHandler extends Debug {
013:
014: /**
015: * The value return by authenticate() when the authentication
016: * was successfull.
017: */
018: public static final int SUCCEEDED = 1;
019: /**
020: * The value return by authenticate() when the authentication
021: * failed but the next handler should be tried.
022: */
023: protected static final int FAILED_CONTINUE = 2;
024: /**
025: * The value return by authenticate() when the authentication
026: * failed and the authentication process should stop.
027: */
028: public static final int FAILED_STOP = 3;
029:
030: /**
031: * The XML element that represents the configuration of the handler
032: * in the configuration file.
033: */
034: private Element configElement;
035:
036: /**
037: * Retrieve the XML element the represents the configuration of the handler
038: * in the configuration file.
039: *
040: * @return an XML element.
041: */
042: protected final Element getConfigElement() {
043: return configElement;
044: }
045:
046: /**
047: * Constructor.
048: *
049: * @param handlerElement the XML element that declares the handler
050: * in the configuration file
051: * @param configDebug debugging mode of the global configuration
052: */
053: protected BasicHandler(final Element handlerElement,
054: final Boolean configDebug) {
055: super (elementDebugValue(handlerElement, configDebug
056: .booleanValue()));
057: traceBegin();
058: configElement = handlerElement.element("config");
059: traceEnd();
060: }
061:
062: /**
063: * Check the "config" XML element if needed.
064: *
065: * @param configElementNeeded true if a config element is needed
066: * @throws Exception Exception
067: */
068: protected final void checkConfigElement(
069: final boolean configElementNeeded) throws Exception {
070: traceBegin();
071: if (configElementNeeded && (getConfigElement() == null)) {
072: traceThrow(new MisconfiguredHandlerException(
073: "A \"config\" element is needed by \""
074: + getClass().getName() + "\" handlers."));
075: }
076: traceEnd();
077: }
078:
079: /**
080: * Tries to Authenticate a user by accessing all the servers..
081: *
082: * @param username the username to authenticate
083: * @param password the corresponding password
084: *
085: * @return SUCCEDED on success, FAILED_CONTINUE or FAILED_STOP otherwise.
086: */
087: public abstract int authenticate(final String username,
088: final String password);
089:
090: /**
091: * Retrieve the content of a config sub-element.
092: *
093: * @param elementName the name of the XML element to look for
094: * @param needed false if the element can be absent or empty, true otherwise
095: * @return a String
096: * @throws Exception Exception
097: */
098: protected final String getConfigSubElementContent(
099: final String elementName, final boolean needed)
100: throws Exception {
101: Element element = getConfigElement().element(elementName);
102: String str;
103: if (element == null) {
104: str = "";
105: } else {
106: str = element.getTextTrim();
107: }
108: if (needed && str.equals("")) {
109: traceThrow(new MisconfiguredHandlerException(
110: "A non empty nested \"" + elementName
111: + "\" element is needed to configure \""
112: + getClass().getName() + "\" handlers."));
113: }
114: return str;
115: }
116:
117: /**
118: * Tell if a config sub-element is present.
119: *
120: * @param elementName the name of the XML element to look for
121: * @return true if the element is present, false otherwise
122: * @throws Exception Exception
123: */
124: protected final boolean hasConfigSubElement(final String elementName)
125: throws Exception {
126: Element element = getConfigElement().element(elementName);
127: return (element != null);
128: }
129:
130: }
|