001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     matic
016 */
017package org.nuxeo.ecm.platform.ec.notification.email;
018
019import java.util.Properties;
020
021import javax.mail.PasswordAuthentication;
022
023public class EmailAuthenticator extends javax.mail.Authenticator {
024
025    protected final Properties properties;
026
027    public EmailAuthenticator(Properties properties) {
028        this.properties = properties;
029    }
030
031    @Override
032    protected PasswordAuthentication getPasswordAuthentication() {
033        String user = value("user");
034        if (user == null) {
035            return null;
036        }
037        String password = value("password");
038        if (password == null) {
039            return null;
040        }
041        return new PasswordAuthentication(user, password);
042    }
043
044    protected String value(String name) {
045        String value = protocolValue(name);
046        if (value != null) {
047            return value;
048        }
049        return defaultValue(name);
050    }
051
052    protected String protocolValue(String name) {
053        String key = "mail." + getRequestingProtocol() + "." + name;
054        return properties.getProperty(key);
055    }
056
057    protected String defaultValue(String name) {
058        String key = "mail." + getRequestingProtocol() + "." + name;
059        return properties.getProperty(key);
060    }
061}