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 = protocolValue("user");
036        if (user == null) {
037            return null;
038        }
039        String password = protocolValue("password");
040        if (password == null) {
041            return null;
042        }
043        return new PasswordAuthentication(user, password);
044    }
045
046    /**
047     * @deprecated since 10.2, use {@link #protocolValue(String)} instead
048     */
049    @Deprecated
050    protected String value(String name) {
051        String value = protocolValue(name);
052        if (value != null) {
053            return value;
054        }
055        return defaultValue(name);
056    }
057
058    protected String protocolValue(String name) {
059        String key = "mail." + getRequestingProtocol() + "." + name;
060        return properties.getProperty(key);
061    }
062
063    /**
064     * @deprecated since 10.2, use {@link #protocolValue(String)} instead
065     */
066    @Deprecated
067    protected String defaultValue(String name) {
068        String key = "mail." + getRequestingProtocol() + "." + name;
069        return properties.getProperty(key);
070    }
071}