001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     matic
018 */
019package org.nuxeo.ecm.platform.ec.notification.email;
020
021import java.util.Properties;
022
023import javax.mail.PasswordAuthentication;
024
025public class EmailAuthenticator extends javax.mail.Authenticator {
026
027    protected final Properties properties;
028
029    public EmailAuthenticator(Properties properties) {
030        this.properties = properties;
031    }
032
033    @Override
034    protected PasswordAuthentication getPasswordAuthentication() {
035        String user = value("user");
036        if (user == null) {
037            return null;
038        }
039        String password = value("password");
040        if (password == null) {
041            return null;
042        }
043        return new PasswordAuthentication(user, password);
044    }
045
046    protected String value(String name) {
047        String value = protocolValue(name);
048        if (value != null) {
049            return value;
050        }
051        return defaultValue(name);
052    }
053
054    protected String protocolValue(String name) {
055        String key = "mail." + getRequestingProtocol() + "." + name;
056        return properties.getProperty(key);
057    }
058
059    protected String defaultValue(String name) {
060        String key = "mail." + getRequestingProtocol() + "." + name;
061        return properties.getProperty(key);
062    }
063}