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

br.com.jhonsapp.email.session.configurator.ServerConfigurator Maven / Gradle / Ivy

Go to download

Email Dispatcher is an open source project created by Jhonys Camacho and Jhonathan Camacho to facilitate the sending of emails.

There is a newer version: 1.0.3
Show newest version
package br.com.jhonsapp.email.session.configurator;

import java.io.Serializable;
import java.nio.file.FileSystemNotFoundException;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.outjected.email.impl.SimpleMailConfig;

import br.com.jhonsapp.email.session.properties.ServerProperties;
import br.com.jhonsapp.email.session.properties.UserProperties;
import br.com.jhonsapp.util.loader.PropertiesLoader;

/**
 * This class is used to configure and load information for the mail server.
 * 
 * @author Jhonathan Camacho
 *
 */
@Component
public class ServerConfigurator implements Serializable {

	/**
	 * Generated serial version id created at 08 July 2017.
	 */
	private static final long serialVersionUID = 6009806907368702289L;

	@Autowired
	private PropertiesLoader loader;
	private SimpleMailConfig simpleMailConfig;
	private Properties serverInformation;

	
	public void configure(SimpleMailConfig sessionConfig) {

		this.simpleMailConfig = sessionConfig;

		serverInformation = getServerInformation();
		verifyIfAllServerInformationValuesExist();
		setServerInformationInSimpleMailConfig();
	}

	private void verifyIfAllServerInformationValuesExist() {

		loader.verifyIfValueExists(serverInformation, ServerProperties.hostKey);
		loader.verifyIfValueExists(serverInformation, ServerProperties.portKey);
		loader.verifyIfValueExists(serverInformation, ServerProperties.sslKey);
		loader.verifyIfValueExists(serverInformation, ServerProperties.authKey);
	}

	private void setServerInformationInSimpleMailConfig() {

		simpleMailConfig.setServerHost(loader.getProperty(serverInformation, ServerProperties.hostKey));
		simpleMailConfig
				.setServerPort(Integer.parseInt(loader.getProperty(serverInformation, ServerProperties.portKey)));
		simpleMailConfig
				.setEnableSsl(Boolean.parseBoolean(loader.getProperty(serverInformation, ServerProperties.sslKey)));
		simpleMailConfig.setAuth(Boolean.parseBoolean(loader.getProperty(serverInformation, ServerProperties.authKey)));
	}

	private Properties getServerInformation() {

		Properties userProperties = loader.getPropertiesFile(UserProperties.USER_PROPERTIES);

		if (serverInformation == null) {

			if (typeIsEquals(userProperties, UserProperties.GMAIL)) {

				serverInformation = loader.getPropertiesFile(ServerProperties.SERVER_PROPERTIES_GMAIL);

			} else if (typeIsEquals(userProperties, UserProperties.HOTMAIL)) {

				serverInformation = loader.getPropertiesFile(ServerProperties.SERVER_PROPERTIES_HOTMAIL);

			} else {

				throw new FileSystemNotFoundException("This informed email provider is not supported.");
			}
		}

		return serverInformation;
	}

	private boolean typeIsEquals(Properties userProperties, String value) {

		return loader.getProperty(userProperties, UserProperties.typeKey).equalsIgnoreCase(value);
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy