View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.impl.email;
20  
21  import java.util.Properties;
22  
23  import javax.annotation.PostConstruct;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.mail.SimpleMailMessage;
29  import org.springframework.mail.javamail.JavaMailSender;
30  import org.springframework.mail.javamail.JavaMailSenderImpl;
31  import org.springframework.security.access.annotation.Secured;
32  import org.springframework.stereotype.Component;
33  
34  import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
35  import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
36  import com.hack23.cia.service.data.api.ApplicationConfigurationService;
37  
38  /**
39   * The Class EmailServiceImpl.
40   */
41  @Component("MailService")
42  @Secured({"ROLE_ANONYMOUS","ROLE_USER", "ROLE_ADMIN" })
43  public final class EmailServiceImpl implements EmailService {
44  
45  	/** The Constant MAIL_SMTP_STARTTLS_ENABLE. */
46  	private static final String MAIL_SMTP_STARTTLS_ENABLE = "mail.smtp.starttls.enable";
47  
48  	/** The Constant MAIL_SMTP_AUTH. */
49  	private static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
50  
51  	/** The Constant APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE. */
52  	private static final String APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE = "application.email.smtp.starttls.enable";
53  
54  	/** The Constant APPLICATION_EMAIL_SMTP_AUTH. */
55  	private static final String APPLICATION_EMAIL_SMTP_AUTH = "application.email.smtp.auth";
56  
57  	/** The Constant APPLICATION_EMAIL_SMTP_PASSWORD. */
58  	private static final String APPLICATION_EMAIL_SMTP_SECRET = "application.email.smtp.password";
59  
60  	/** The Constant APPLICATION_EMAIL_SMTP_USERNAME. */
61  	private static final String APPLICATION_EMAIL_SMTP_USERNAME = "application.email.smtp.username";
62  
63  	/** The Constant APPLICATION_EMAIL_SMTP_PORT. */
64  	private static final String APPLICATION_EMAIL_SMTP_PORT = "application.email.smtp.port";
65  
66  	/** The Constant APPLICATION_EMAIL_SMTP_HOST. */
67  	private static final String APPLICATION_EMAIL_SMTP_HOST = "application.email.smtp.host";
68  
69  	/** The Constant APPLICATION_EMAIL_FROM_EMAIL. */
70  	private static final String APPLICATION_EMAIL_FROM_EMAIL = "application.email.from.email";
71  
72  	/** The Constant APPLICATION_EMAIL_SEND_EMAIL. */
73  	private static final String APPLICATION_EMAIL_SEND_EMAIL = "application.email.send.email";
74  
75  	/** The Constant RESPONSIBLE_FOR_SENDING_EMAIL. */
76  	private static final String RESPONSIBLE_FOR_SENDING_EMAIL = "Responsible for sending email";
77  
78  	/** The Constant SMTP_STARTTLS_ENABLE. */
79  	private static final String SMTP_STARTTLS_ENABLE = "Smtp starttls enable";
80  
81  	/** The Constant SMTP_AUTH. */
82  	private static final String SMTP_AUTH = "Smtp auth";
83  
84  	/** The Constant SMTP_PASSWORD. */
85  	private static final String SMTP_SECRET = "Smtp password";
86  
87  	/** The Constant SMTP_USERNAME. */
88  	private static final String SMTP_USERNAME = "Smtp username";
89  
90  	/** The Constant SMTP_PORT. */
91  	private static final String SMTP_PORT = "Smtp port";
92  
93  	/** The Constant SMTP_HOST. */
94  	private static final String SMTP_HOST = "Smtp Host";
95  
96  	/** The Constant FROM_EMAIL. */
97  	private static final String FROM_EMAIL = "From email";
98  
99  	/** The Constant SEND_EMAIL. */
100 	private static final String SEND_EMAIL = "Send email";
101 
102 	/** The Constant EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE. */
103 	private static final String EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE = "Email configuration smtp starttls enable";
104 
105 	/** The Constant EMAIL_CONFIGURATION_SMTP_AUTH. */
106 	private static final String EMAIL_CONFIGURATION_SMTP_AUTH = "Email configuration smtp auth";
107 
108 	/** The Constant EMAIL_CONFIGURATION_SMTP_PASSWORD. */
109 	private static final String EMAIL_CONFIGURATION_SMTP_SECRET = "Email configuration smtp password";
110 
111 	/** The Constant EMAIL_CONFIGURATION_SMTP_USERNAME. */
112 	private static final String EMAIL_CONFIGURATION_SMTP_USERNAME = "Email configuration smtp username";
113 
114 	/** The Constant EMAIL_CONFIGURATION_SMTP_PORT. */
115 	private static final String EMAIL_CONFIGURATION_SMTP_PORT = "Email configuration smtp port";
116 
117 	/** The Constant EMAIL_CONFIGURATION_SMTP_HOST. */
118 	private static final String EMAIL_CONFIGURATION_SMTP_HOST = "Email configuration smtp host";
119 
120 	/** The Constant EMAIL_CONFIGURATION_FROM_EMAIL. */
121 	private static final String EMAIL_CONFIGURATION_FROM_EMAIL = "Email configuration from email";
122 
123 	/** The Constant EMAIL_CONFIGURATION_SEND_EMAILS. */
124 	private static final String EMAIL_CONFIGURATION_SEND_EMAILS = "Email configuration send emails";
125 
126 	/** The Constant EMAIL_SETTINGS. */
127 	private static final String EMAIL_SETTINGS = "Email settings:{}";
128 
129 	/** The Constant LOGGER. */
130 	private static final Logger LOGGER = LoggerFactory.getLogger(EmailServiceImpl.class);
131 
132 	/** The application configuration service. */
133 	@Autowired
134 	private ApplicationConfigurationService applicationConfigurationService;
135 
136 
137 	/**
138 	 * Instantiates a new email service impl.
139 	 */
140 	public EmailServiceImpl() {
141 		super();
142 	}
143 
144 
145 	/**
146 	 * Inits the settings.
147 	 */
148 	@PostConstruct
149 	public void initSettings() {
150 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SEND_EMAILS, SEND_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SEND_EMAIL, "false"));
151 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_FROM_EMAIL, FROM_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_FROM_EMAIL, "admin@hack23.com"));
152 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_HOST, SMTP_HOST, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_HOST, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_HOST, "localhost"));
153 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_PORT, SMTP_PORT, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_PORT, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_PORT, "587"));
154 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_USERNAME, SMTP_USERNAME, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_USERNAME, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_USERNAME, "username"));
155 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_SECRET, SMTP_SECRET, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_SECRET, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_SECRET, "password"));
156 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_AUTH, SMTP_AUTH, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_AUTH, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_AUTH, "true"));
157 		LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE, SMTP_STARTTLS_ENABLE, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_STARTTLS_ENABLE, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE, "true"));
158 
159 	}
160 
161 
162 	@Override
163 	public void sendEmail(final String toEmail,final String subject, final String content) {
164 		final ApplicationConfiguration sendEmail = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SEND_EMAILS, SEND_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SEND_EMAIL, "false");
165 		final ApplicationConfiguration fromEmail = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_FROM_EMAIL, FROM_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_FROM_EMAIL, "admin@hack23.com");
166 
167 		final JavaMailSender javaMailSender = getMailSender();
168 
169 	    final SimpleMailMessage emailMessage = new SimpleMailMessage();
170 	    emailMessage.setFrom(fromEmail.getPropertyValue());
171         emailMessage.setTo(toEmail);
172         emailMessage.setText(content);
173 		emailMessage.setSubject(subject);
174 
175 		if("true".equalsIgnoreCase(sendEmail.getPropertyValue())) {
176 			LOGGER.info("Sending email:{}",emailMessage);
177 			javaMailSender.send(emailMessage);
178 		} else {
179 			LOGGER.info("Email sending disabled, do not send email:{}",emailMessage);
180 		}
181 	}
182 
183 	/**
184 	 * Gets the mail sender.
185 	 *
186 	 * @return the mail sender
187 	 */
188 	private JavaMailSender getMailSender() {
189 		final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
190 
191 		final ApplicationConfiguration smtpHostConfig = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_HOST, SMTP_HOST, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_HOST, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_HOST, "localhost");
192 		final ApplicationConfiguration smtpPort = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_PORT, SMTP_PORT, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_PORT, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_PORT, "587");
193 		final ApplicationConfiguration smtpUsername = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_USERNAME, SMTP_USERNAME, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_USERNAME, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_USERNAME, "username");
194 		final ApplicationConfiguration smtpPassword = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_SECRET, SMTP_SECRET, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_SECRET, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_SECRET, "password");
195 		final ApplicationConfiguration smtpAuth = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_AUTH, SMTP_AUTH, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_AUTH, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_AUTH, "true");
196 		final ApplicationConfiguration smtpStartTlsEnable = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE, SMTP_STARTTLS_ENABLE, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_STARTTLS_ENABLE, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE, "true");
197 
198 
199 		javaMailSender.setHost(smtpHostConfig.getPropertyValue());
200 		javaMailSender.setPort(Integer.parseInt(smtpPort.getPropertyValue()));
201 		javaMailSender.setUsername(smtpUsername.getPropertyValue());
202 		javaMailSender.setPassword(smtpPassword.getPropertyValue());
203 
204 		final Properties javaMailProperties = new Properties();
205 
206 		javaMailProperties.setProperty(MAIL_SMTP_AUTH, smtpAuth.getPropertyValue());
207 		javaMailProperties.setProperty(MAIL_SMTP_STARTTLS_ENABLE, smtpStartTlsEnable.getPropertyValue());
208 
209 		javaMailSender.setJavaMailProperties(javaMailProperties);
210 
211 		return javaMailSender;
212 	}
213 
214 }