All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gravitee.rest.api.service.spring.GraviteeJavaMailManager Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.rest.api.service.spring;

import io.gravitee.common.event.Event;
import io.gravitee.common.event.EventListener;
import io.gravitee.common.event.EventManager;
import io.gravitee.repository.management.model.Parameter;
import io.gravitee.rest.api.model.parameters.Key;
import io.gravitee.rest.api.model.parameters.ParameterReferenceType;
import io.gravitee.rest.api.service.ParameterService;
import io.gravitee.rest.api.service.common.GraviteeContext;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

public class GraviteeJavaMailManager implements EventListener {

    private static final String EMAIL_PROPERTIES_PREFIX = "email.properties";
    private static final String MAILAPI_PROPERTIES_PREFIX = "mail.smtp.";

    private final ParameterService parameterService;
    private final Map mailSenderByReference;

    public GraviteeJavaMailManager(ParameterService parameterService, EventManager eventManager) {
        this.parameterService = parameterService;
        this.mailSenderByReference = new ConcurrentHashMap<>();

        eventManager.subscribeForEvents(this, Key.class);
    }

    public JavaMailSender getOrCreateMailSender(String referenceId, ParameterReferenceType type) {
        GraviteeContext.ReferenceContext ref = GraviteeContext.getCurrentContext();
        JavaMailSenderImpl mailSender = this.getMailSenderByReference(ref);
        if (mailSender == null) {
            mailSender = new JavaMailSenderImpl();
            mailSender.setHost(parameterService.find(Key.EMAIL_HOST, referenceId, type));
            String port = parameterService.find(Key.EMAIL_PORT, referenceId, type);
            if (StringUtils.isNumeric(port)) {
                mailSender.setPort(Integer.parseInt(port));
            }
            String username = parameterService.find(Key.EMAIL_USERNAME, referenceId, type);
            if (username != null && !username.isEmpty()) {
                mailSender.setUsername(username);
            }

            String password = parameterService.find(Key.EMAIL_PASSWORD, referenceId, type);
            if (password != null && !password.isEmpty()) {
                mailSender.setPassword(password);
            }

            mailSender.setProtocol(parameterService.find(Key.EMAIL_PROTOCOL, referenceId, type));
            mailSender.setJavaMailProperties(loadProperties(referenceId, type));

            this.mailSenderByReference.put(ref, mailSender);
        }

        return mailSender;
    }

    @Override
    public void onEvent(Event event) {
        JavaMailSenderImpl mailSender =
            this.getMailSenderByReference(
                    event.content().getReferenceId(),
                    ParameterReferenceType.valueOf(event.content().getReferenceType().name())
                );
        if (mailSender != null) {
            switch (event.type()) {
                case EMAIL_HOST:
                    mailSender.setHost(event.content().getValue());
                    break;
                case EMAIL_PORT:
                    if (StringUtils.isNumeric(event.content().getValue())) {
                        mailSender.setPort(Integer.parseInt(event.content().getValue()));
                    }
                    break;
                case EMAIL_USERNAME:
                    if (event.content().getValue() != null && !event.content().getValue().isEmpty()) {
                        mailSender.setUsername(event.content().getValue());
                    } else {
                        mailSender.setUsername(null);
                    }
                    break;
                case EMAIL_PASSWORD:
                    if (event.content().getValue() != null && !event.content().getValue().isEmpty()) {
                        mailSender.setPassword(event.content().getValue());
                    } else {
                        mailSender.setPassword(null);
                    }
                    break;
                case EMAIL_PROTOCOL:
                    mailSender.setProtocol(event.content().getValue());
                    break;
                case EMAIL_PROPERTIES_AUTH_ENABLED:
                case EMAIL_PROPERTIES_SSL_TRUST:
                case EMAIL_PROPERTIES_STARTTLS_ENABLE:
                    mailSender.getJavaMailProperties().setProperty(computeMailProperty(event.type().key()), event.content().getValue());
                    break;
            }
        }
    }

    JavaMailSenderImpl getMailSenderByReference(GraviteeContext.ReferenceContext ref) {
        return this.mailSenderByReference.get(ref);
    }

    JavaMailSenderImpl getMailSenderByReference(String referenceId, ParameterReferenceType referenceType) {
        return this.getMailSenderByReference(
                new GraviteeContext.ReferenceContext(referenceId, GraviteeContext.ReferenceContextType.valueOf(referenceType.name()))
            );
    }

    private String computeMailProperty(String graviteeProperty) {
        return MAILAPI_PROPERTIES_PREFIX + graviteeProperty.substring(EMAIL_PROPERTIES_PREFIX.length() + 1);
    }

    private Properties loadProperties(String referenceId, ParameterReferenceType referenceType) {
        Map> parameters = parameterService.findAll(
            Arrays.asList(Key.EMAIL_PROPERTIES_AUTH_ENABLED, Key.EMAIL_PROPERTIES_STARTTLS_ENABLE, Key.EMAIL_PROPERTIES_SSL_TRUST),
            referenceId,
            referenceType
        );

        Properties properties = new Properties();
        parameters.forEach(
            (key, value) -> {
                if (!value.isEmpty()) {
                    properties.setProperty(computeMailProperty(key), value.get(0));
                }
            }
        );

        return properties;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy