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
025/**
026 * @deprecated since 11.1, use {@link org.nuxeo.mail.MailAuthenticator} instead
027 */
028@Deprecated(since = "11.1")
029public class EmailAuthenticator extends javax.mail.Authenticator {
030
031    protected final Properties properties;
032
033    public EmailAuthenticator(Properties properties) {
034        this.properties = properties;
035    }
036
037    @Override
038    protected PasswordAuthentication getPasswordAuthentication() {
039        String user = protocolValue("user");
040        if (user == null) {
041            return null;
042        }
043        String password = protocolValue("password");
044        if (password == null) {
045            return null;
046        }
047        return new PasswordAuthentication(user, password);
048    }
049
050    /**
051     * @deprecated since 10.2, use {@link #protocolValue(String)} instead
052     */
053    @Deprecated
054    protected String value(String name) {
055        String value = protocolValue(name);
056        if (value != null) {
057            return value;
058        }
059        return defaultValue(name);
060    }
061
062    protected String protocolValue(String name) {
063        String key = "mail." + getRequestingProtocol() + "." + name;
064        return properties.getProperty(key);
065    }
066
067    /**
068     * @deprecated since 10.2, use {@link #protocolValue(String)} instead
069     */
070    @Deprecated
071    protected String defaultValue(String name) {
072        String key = "mail." + getRequestingProtocol() + "." + name;
073        return properties.getProperty(key);
074    }
075}