001/*
002 * JGrapes Event driven Framework
003 * Copyright (C) 2022 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.mail.events;
020
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Optional;
025import org.jgrapes.core.Event;
026import org.jgrapes.mail.MailConnectionManager;
027import org.jgrapes.util.Password;
028
029/**
030 * Common base class for events that open a mail connection for
031 * sending or receiving mail. Note that all configuration information
032 * (such as mail server, protocol, user name) is provided as mail 
033 * properties (see {@link #setMailProperties(Map)).
034 * 
035 * Also note that a component receiving the event may have default
036 * or system wide values configured for the properties.
037 */
038public abstract class OpenMailConnection extends Event<Void> {
039
040    @SuppressWarnings("PMD.UseConcurrentHashMap")
041    private final Map<String, String> mailProps = new HashMap<>();
042    private Password password;
043
044    /**
045     * Specifies mail properties that override defaults set for
046     * the handling {@link MailConnectionManager}. Merges the given
047     * properties with properties already set for the event.
048     *
049     * @param props the props
050     * @return the event
051     */
052    public OpenMailConnection setMailProperties(Map<String, String> props) {
053        mailProps.putAll(props);
054        return this;
055    }
056
057    /**
058     * Sets a single mail property, see {@link #setMailProperties(Map)}.
059     *
060     * @param name the name
061     * @param value the value
062     * @return the open mail connection
063     */
064    public OpenMailConnection setMailProperty(String name, String value) {
065        mailProps.put(name, value);
066        return this;
067    }
068
069    /**
070     * Returns the mail properties.
071     *
072     * @return the map
073     */
074    public Map<String, String> mailProperties() {
075        return Collections.unmodifiableMap(mailProps);
076    }
077
078    /**
079     * Sets the password used for opening the connection.
080     *
081     * @param password the password
082     * @return the open mail connection
083     */
084    public OpenMailConnection setPassword(Password password) {
085        this.password = password;
086        return this;
087    }
088
089    /**
090     * Returns the password.
091     *
092     * @return the password
093     */
094    public Optional<Password> password() {
095        return Optional.ofNullable(password);
096    }
097}