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 abstract class implements a generic server class, inherited
008 * by DatabaseServer and LDAPServer.
009 *
010 * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
011 */
012
013 public abstract class Server extends Debug {
014
015 /**
016 * The error set by the connect() method on success.
017 */
018 protected static final int CONNECT_SUCCESS = 0;
019 /**
020 * The error code set by the connect() method when the connection
021 * with the server could not be achieved because of the bind
022 * information provided (the server responded but refused the
023 * connection). Returned by getConnectError().
024 */
025 protected static final int CONNECT_NOAUTH = 1;
026 /**
027 * The error code set by the connect() method when the connection
028 * with the server could not be achieved because of a service failure.
029 * Returned by getConnectError().
030 */
031 protected static final int CONNECT_FAILURE = 2;
032 /**
033 * an integer to store the error code of the connect() calls, set by
034 * setConnectError(), retrieved by getConnectError().
035 */
036 private int connectError = CONNECT_SUCCESS;
037 /**
038 * The result of the authenticate() method on success (when the
039 * user has been authenticated).
040 */
041 protected static final int AUTHENTICATE_SUCCESS = 0;
042 /**
043 * The result of the authenticate() method when the user could not
044 * be authenticated (but the server was accessed without any problem).
045 * When testing many servers, the next servers will not be called.
046 */
047 protected static final int AUTHENTICATE_NOAUTH = 1;
048 /**
049 * The result of the authenticate() method on failure (when the server
050 * could not be accessed for instance). When testing many servers, the
051 * next server will be called.
052 */
053 protected static final int AUTHENTICATE_FAILURE = 2;
054
055 /**
056 * The handler the server will be used for.
057 */
058 private RedundantHandler handler;
059
060
061 /**
062 * Constructor.
063 *
064 * @param handlerDebug debugging mode
065 * @param parentHandler the handler the server will be used by
066 * @param serverElement an XML element corresponding to the server configuration
067 */
068 public Server(
069 final Boolean handlerDebug,
070 final RedundantHandler parentHandler,
071 final Element serverElement) {
072 super(elementDebugValue(serverElement, handlerDebug.booleanValue()));
073 handler = parentHandler;
074 }
075
076 /**
077 * Get the handler the server will be used by.
078 *
079 * @return a RedundantHandler object.
080 */
081 protected final RedundantHandler getHandler() {
082 return handler;
083 }
084
085 /**
086 * Try to authenticate a user.
087 *
088 * @param username the user's name
089 * @param password the user's password
090 *
091 * @return Server.AUTHENTICATE_SUCCESS, Server.AUTHENTICATE_NOAUTH
092 * or Server.AUTHENTICATE_FAILURE.
093 */
094 protected abstract int authenticate(final String username,
095 final String password);
096
097 /**
098 * Set the error code of the last connect() call.
099 *
100 * @param errCode an integer equal to Server.CONNECT_SUCCESS,
101 * Server.CONNECT_NOAUTH or Server.CONNECT_FAILURE.
102 */
103 protected final void setConnectError(final int errCode) {
104 connectError = errCode;
105 }
106
107 /**
108 * Retrieve the error code of the last connect() call.
109 *
110 * @return Server.CONNECT_SUCCESS, Server.CONNECT_NOAUTH
111 * or Server.CONNECT_FAILURE.
112 */
113 protected final int getConnectError() {
114 int error = connectError;
115 connectError = CONNECT_SUCCESS;
116 return error;
117 }
118
119 /**
120 * Retrieve the content of a server sub-element.
121 *
122 * @param serverElement the XML element representing the server in the configuration
123 * @param elementName the name of the XML element to look for
124 * @param needed false if the element can be absent or empty, true otherwise
125 * @return a String
126 * @throws Exception Exception
127 */
128 protected final String getServerSubElementContent(
129 final Element serverElement,
130 final String elementName,
131 final boolean needed) throws Exception {
132 Element element = serverElement.element(elementName);
133 String str;
134 if (element == null) {
135 str = "";
136 } else {
137 str = element.getTextTrim();
138 }
139 if (needed && str.equals("")) {
140 traceThrow(new MisconfiguredHandlerException(
141 "A non empty nested \"" + elementName + "\" element is needed to configure \""
142 + getClass().getName()
143 + "\" servers."
144 ));
145 }
146 return str;
147 }
148
149
150 }