Skip to content

Commit 681913b

Browse files
committed
Implemented sending of mail with activation key.
Also added support for feature flags. You can enable/disable them in runtime by opening /togglz URL under admin account. Fixed #64 (GH #1)
1 parent 255369a commit 681913b

25 files changed

+616
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ target/
99
.idea
1010
mystamps.iml
1111

12+
src/main/resources/prod/spring/mail.properties

NEWS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
0.3 (not released yet)
2+
- implemented sending of mail with activation key
3+
- started using feature flags with Togglz
24
- added localization support for countries
35
- added categories to series
46
- save images to the disk

pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
<lombok.version>1.12.6</lombok.version>
1616
<mysql.connector.version>5.1.30</mysql.connector.version>
1717
<selenium.version>2.41.0</selenium.version>
18+
<subethasmtp.version>3.1.7</subethasmtp.version>
1819
<slf4j.version>1.7.7</slf4j.version>
1920
<spring.version>3.2.5.RELEASE</spring.version>
2021
<spring.data.version>1.5.1.RELEASE</spring.data.version>
2122
<spring.security.version>3.1.6.RELEASE</spring.security.version>
2223
<h2.version>1.4.178</h2.version>
2324
<javax.validation.version>1.1.0.Alpha1</javax.validation.version>
2425
<javax.inject.version>1</javax.inject.version>
26+
<javax.mail.version>1.5.2</javax.mail.version>
2527
<javax.persistence.version>1.0.1.Final</javax.persistence.version>
2628
<servlet.api.version>3.0.20100224</servlet.api.version>
2729
<jstl.version>1.2</jstl.version>
@@ -33,6 +35,7 @@
3335
<tiles.version>2.2.2</tiles.version>
3436
<liquibase.version>3.2.0</liquibase.version>
3537
<liquibase.slf4j.version>1.2.1</liquibase.slf4j.version>
38+
<togglz.version>2.0.1.Final</togglz.version>
3639

3740
<!-- Also don't forget to change version in src/main/java/ru/mystamps/web/config/MvcConfig.java -->
3841
<bootstrap.version>2.3.1</bootstrap.version>
@@ -97,6 +100,13 @@
97100
<version>${javax.inject.version}</version>
98101
</dependency>
99102

103+
<!-- For MimeMessage class -->
104+
<dependency>
105+
<groupId>javax.mail</groupId>
106+
<artifactId>javax.mail-api</artifactId>
107+
<version>${javax.mail.version}</version>
108+
</dependency>
109+
100110
<dependency>
101111
<groupId>javax.validation</groupId>
102112
<artifactId>validation-api</artifactId>
@@ -134,6 +144,13 @@
134144
<version>${spring.version}</version>
135145
</dependency>
136146

147+
<!-- For mail classes -->
148+
<dependency>
149+
<groupId>org.springframework</groupId>
150+
<artifactId>spring-context-support</artifactId>
151+
<version>${spring.version}</version>
152+
</dependency>
153+
137154
<dependency>
138155
<groupId>org.springframework.data</groupId>
139156
<artifactId>spring-data-jpa</artifactId>
@@ -266,6 +283,39 @@
266283
<scope>runtime</scope>
267284
</dependency>
268285

286+
<dependency>
287+
<groupId>org.togglz</groupId>
288+
<artifactId>togglz-core</artifactId>
289+
<version>${togglz.version}</version>
290+
</dependency>
291+
292+
<dependency>
293+
<groupId>org.togglz</groupId>
294+
<artifactId>togglz-spring</artifactId>
295+
<version>${togglz.version}</version>
296+
<scope>runtime</scope>
297+
</dependency>
298+
299+
<dependency>
300+
<groupId>org.togglz</groupId>
301+
<artifactId>togglz-spring-security</artifactId>
302+
<version>${togglz.version}</version>
303+
</dependency>
304+
305+
<dependency>
306+
<groupId>org.togglz</groupId>
307+
<artifactId>togglz-slf4j</artifactId>
308+
<version>${togglz.version}</version>
309+
<scope>runtime</scope>
310+
</dependency>
311+
312+
<dependency>
313+
<groupId>org.togglz</groupId>
314+
<artifactId>togglz-console</artifactId>
315+
<version>${togglz.version}</version>
316+
<scope>runtime</scope>
317+
</dependency>
318+
269319
<dependency>
270320
<groupId>org.webjars</groupId>
271321
<artifactId>bootstrap</artifactId>
@@ -323,6 +373,27 @@
323373
<scope>test</scope>
324374
</dependency>
325375

376+
<dependency>
377+
<groupId>org.togglz</groupId>
378+
<artifactId>togglz-testing</artifactId>
379+
<version>${togglz.version}</version>
380+
<scope>test</scope>
381+
</dependency>
382+
383+
<dependency>
384+
<groupId>org.subethamail</groupId>
385+
<artifactId>subethasmtp</artifactId>
386+
<version>${subethasmtp.version}</version>
387+
<scope>test</scope>
388+
</dependency>
389+
390+
<dependency>
391+
<groupId>com.sun.mail</groupId>
392+
<artifactId>javax.mail</artifactId>
393+
<version>${javax.mail.version}</version>
394+
<scope>test</scope>
395+
</dependency>
396+
326397
<dependency>
327398
<groupId>org.seleniumhq.selenium</groupId>
328399
<artifactId>selenium-java</artifactId>
@@ -468,6 +539,9 @@
468539
<groupId>org.apache.maven.plugins</groupId>
469540
<artifactId>maven-war-plugin</artifactId>
470541
<version>${war.plugin.version}</version>
542+
<configuration>
543+
<packagingExcludes>WEB-INF/classes/prod/spring/mail.properties.example</packagingExcludes>
544+
</configuration>
471545
</plugin>
472546

473547
<plugin>

src/main/java/ru/mystamps/web/Url.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @author Slava Semushin <[email protected]>
2828
*/
2929
public final class Url {
30+
public static final String PUBLIC_URL = "http://my-stamps.ru";
3031

3132
// defined at pom.xml (and used by functional tests only)
3233
public static final String SITE = "http://127.0.0.1:8081";

src/main/java/ru/mystamps/web/config/ApplicationContext.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.config;
1919

20+
import javax.inject.Inject;
21+
2022
import org.springframework.context.MessageSource;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.Configuration;
@@ -25,26 +27,42 @@
2527

2628
import ru.mystamps.web.support.spring.security.SecurityConfig;
2729

30+
import org.togglz.core.manager.TogglzConfig;
31+
32+
import ru.mystamps.web.support.togglz.FeatureConfig;
33+
2834
@Configuration
2935
@Import({
3036
DbConfig.class,
3137
LiquibaseConfig.class,
38+
MailConfig.class,
3239
SecurityConfig.class,
3340
ServicesConfig.class,
3441
StrategiesConfig.class
3542
})
3643
public class ApplicationContext {
3744

45+
@Inject
46+
private DataSourceConfig dataSourceConfig;
47+
3848
@Bean(name = "messageSource")
3949
public MessageSource getMessageSource() {
4050
ReloadableResourceBundleMessageSource messageSource =
4151
new ReloadableResourceBundleMessageSource();
4252

43-
messageSource.setBasename("classpath:ru/mystamps/i18n/SpringSecurityMessages");
53+
messageSource.setBasenames(
54+
"classpath:ru/mystamps/i18n/SpringSecurityMessages",
55+
"classpath:ru/mystamps/i18n/MailTemplates"
56+
);
4457
messageSource.setDefaultEncoding("UTF-8");
4558
messageSource.setFallbackToSystemLocale(false);
4659

4760
return messageSource;
4861
}
4962

63+
@Bean
64+
public TogglzConfig getTogglzConfig() {
65+
return new FeatureConfig(dataSourceConfig.getDataSource());
66+
}
67+
5068
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2009-2014 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.config;
19+
20+
import javax.inject.Inject;
21+
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.PropertySource;
25+
import org.springframework.core.env.Environment;
26+
import org.springframework.mail.javamail.JavaMailSender;
27+
import org.springframework.mail.javamail.JavaMailSenderImpl;
28+
29+
@Configuration
30+
@PropertySource("classpath:${spring.profiles.active}/spring/mail.properties")
31+
public class MailConfig {
32+
33+
@Inject
34+
private Environment env;
35+
36+
@Bean
37+
public JavaMailSender getMailSender() {
38+
JavaMailSenderImpl mailer = new JavaMailSenderImpl();
39+
mailer.setHost(env.getRequiredProperty("mail.smtp.host"));
40+
mailer.setPort(env.getRequiredProperty("mail.smtp.port", Integer.class));
41+
mailer.setUsername(env.getRequiredProperty("mail.smtp.login"));
42+
mailer.setPassword(env.getRequiredProperty("mail.smtp.password"));
43+
44+
return mailer;
45+
}
46+
47+
}

src/main/java/ru/mystamps/web/config/ServicesConfig.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import javax.inject.Inject;
2121

22+
import org.springframework.context.MessageSource;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.core.env.Environment;
2426

2527
import ru.mystamps.web.dao.*; // NOCHECKSTYLE: AvoidStarImportCheck, NOPMD: UnusedImports
2628
import ru.mystamps.web.service.*; // NOCHECKSTYLE: AvoidStarImportCheck, NOPMD: UnusedImports
@@ -59,6 +61,15 @@ public class ServicesConfig {
5961
@Inject
6062
private ImageDao imageDao;
6163

64+
@Inject
65+
private MailConfig mailConfig;
66+
67+
@Inject
68+
private Environment env;
69+
70+
@Inject
71+
private MessageSource messageSource;
72+
6273
@Bean
6374
public CountryService getCountryService() {
6475
return new CountryServiceImpl(countryDao);
@@ -79,6 +90,18 @@ public ImageService getImageService() {
7990
return new ImageServiceImpl(strategiesConfig.getImagePersistenceStrategy(), imageDao);
8091
}
8192

93+
@Bean
94+
public MailService getMailService() {
95+
boolean isProductionEnvironment = env.acceptsProfiles("prod");
96+
boolean enableTestMode = !isProductionEnvironment;
97+
98+
return new MailServiceImpl(
99+
mailConfig.getMailSender(),
100+
messageSource,
101+
env.getRequiredProperty("mail.robot.email"),
102+
enableTestMode);
103+
}
104+
82105
@Bean
83106
public SeriesService getSeriesService() {
84107
return new SeriesServiceImpl(seriesDao, getImageService());
@@ -94,6 +117,7 @@ public UserService getUserService() {
94117
return new UserServiceImpl(
95118
userDao,
96119
usersActivationDao,
120+
getMailService(),
97121
securityConfig.getPasswordEncoder()
98122
);
99123
}

src/main/java/ru/mystamps/web/controller/AccountController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.controller;
1919

20+
import java.util.Locale;
21+
2022
import javax.validation.Valid;
2123

2224
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
@@ -69,13 +71,14 @@ public RegisterAccountForm showRegistrationForm() {
6971
public String processRegistrationForm(
7072
@Valid RegisterAccountForm form,
7173
BindingResult result,
72-
RedirectAttributes redirectAttributes) {
74+
RedirectAttributes redirectAttributes,
75+
Locale userLocale) {
7376

7477
if (result.hasErrors()) {
7578
return null;
7679
}
7780

78-
userService.addRegistrationRequest(form);
81+
userService.addRegistrationRequest(form, userLocale);
7982

8083
redirectAttributes.addFlashAttribute("justRegisteredUser", true);
8184

src/main/java/ru/mystamps/web/entity/UsersActivation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.entity;
1919

2020
import java.util.Date;
21+
import java.util.Locale;
2122

2223
import javax.persistence.Column;
2324
import javax.persistence.Entity;
@@ -35,6 +36,7 @@ public class UsersActivation {
3536

3637
public static final int ACTIVATION_KEY_LENGTH = 10;
3738
public static final int EMAIL_LENGTH = 255;
39+
public static final int LANG_LENGTH = 2;
3840

3941
@Id
4042
@Column(name = "act_key", length = ACTIVATION_KEY_LENGTH)
@@ -43,7 +45,14 @@ public class UsersActivation {
4345
@Column(length = EMAIL_LENGTH, nullable = false)
4446
private String email;
4547

48+
@Column(length = LANG_LENGTH, nullable = false)
49+
private String lang;
50+
4651
@Column(name = "created_at", nullable = false)
4752
private Date createdAt;
4853

54+
public Locale getLocale() {
55+
return new Locale(lang);
56+
}
57+
4958
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2009-2014 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service;
19+
20+
import ru.mystamps.web.entity.UsersActivation;
21+
22+
public interface MailService {
23+
void sendActivationKeyToUser(UsersActivation activation);
24+
}

0 commit comments

Comments
 (0)