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: *
18: * @author Umut Gokbayrak
19: *
20: */
21: public class MailAuth {
22: private static Log log = LogFactory.getLog(MailAuth.class);
23:
24: public static ConnectionMetaHandler authenticate(
25: ConnectionProfile profile, AuthProfile auth,
26: ConnectionMetaHandler handler) throws SystemException,
27: LoginInvalidException, ServerDownException {
28: try {
29: ProtocolFactory factory = new ProtocolFactory(profile,
30: auth, handler);
31: Protocol protocol = factory.getProtocol(null);
32: handler = protocol.connect(Constants.CONNECTION_READ_WRITE);
33: if (handler == null || !handler.getStore().isConnected()) {
34: throw new ConnectionException();
35: }
36: return handler;
37: } catch (SystemException e) {
38: log.fatal("System Exception while authenticating user: "
39: + ((auth == null) ? null : auth.getUsername()), e);
40: throw e;
41: } catch (ConnectionException e) {
42: log.debug("Login Failed of user: "
43: + ((auth == null) ? null : auth.getUsername()), e);
44: throw new LoginInvalidException(e);
45: }
46: }
47: }
|