01: package org.claros.commons.auth;
02:
03: import org.apache.commons.logging.Log;
04: import org.apache.commons.logging.LogFactory;
05: import org.claros.commons.auth.exception.LoginInvalidException;
06: import org.claros.commons.auth.models.AuthProfile;
07: import org.claros.commons.exception.SystemException;
08: import org.claros.commons.mail.exception.ConnectionException;
09: import org.claros.commons.mail.exception.ServerDownException;
10: import org.claros.commons.mail.models.ConnectionMetaHandler;
11: import org.claros.commons.mail.models.ConnectionProfile;
12: import org.claros.commons.mail.protocols.Protocol;
13: import org.claros.commons.mail.protocols.ProtocolFactory;
14: import org.claros.commons.mail.utility.Constants;
15:
16: /**
17: * @author Umut Gokbayrak
18: */
19: public class MailAuth {
20: private static Log log = LogFactory.getLog(MailAuth.class);
21:
22: public static ConnectionMetaHandler authenticate(
23: ConnectionProfile profile, AuthProfile auth,
24: ConnectionMetaHandler handler) throws SystemException,
25: LoginInvalidException, ServerDownException {
26: try {
27: ProtocolFactory factory = new ProtocolFactory(profile,
28: auth, handler);
29: Protocol protocol = factory.getProtocol(null);
30: handler = protocol.connect(Constants.CONNECTION_READ_WRITE);
31: if (handler == null || !handler.getStore().isConnected()) {
32: throw new ConnectionException();
33: }
34: // protocol.disconnect();
35: return handler;
36: } catch (SystemException e) {
37: log.fatal("System Exception while authenticating user: "
38: + ((auth == null) ? null : auth.getUsername()), e);
39: throw e;
40: } catch (ConnectionException e) {
41: log.debug("Login Failed of user: "
42: + ((auth == null) ? null : auth.getUsername()), e);
43: throw new LoginInvalidException(e);
44: }
45: }
46: }
|