Skip to content

Commit 00abf94

Browse files
committed
Ported validation to using JSR-303 annotations.
Changes in dependencies: - commons-validator and oro was removed (due to hibernate-validator usage) - commons-beanutils was added Fixed #86
1 parent d5f18ce commit 00abf94

40 files changed

+1249
-659
lines changed

pom.xml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@
127127
</dependency>
128128

129129
<dependency>
130-
<groupId>commons-validator</groupId>
131-
<artifactId>commons-validator</artifactId>
132-
<version>1.3.1</version>
130+
<groupId>commons-beanutils</groupId>
131+
<artifactId>commons-beanutils</artifactId>
132+
<version>1.8.3</version>
133133
</dependency>
134134

135135
<dependency>
@@ -233,13 +233,6 @@
233233
<scope>runtime</scope>
234234
</dependency>
235235

236-
<!-- commons-validator use it to validating e-mails -->
237-
<dependency>
238-
<groupId>oro</groupId>
239-
<artifactId>oro</artifactId>
240-
<version>2.0.8</version>
241-
</dependency>
242-
243236
<dependency>
244237
<groupId>com.google.guava</groupId>
245238
<artifactId>guava</artifactId>

src/main/config/checkstyle-suppressions.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@
1818
<suppress checks="AbstractClassName" files="ru.mystamps.web.tests.cases.WhenUserAtAnyPage" />
1919
<suppress checks="AbstractClassName" files="ru.mystamps.web.tests.cases.WhenUserAtAnyPageWithForm" />
2020

21+
<suppress checks="WhitespaceAround" files="ru.mystamps.web.validation.jsr303.Email" />
22+
<suppress checks="WhitespaceAround" files="ru.mystamps.web.validation.jsr303.ExistingActivationKey" />
23+
<suppress checks="WhitespaceAround" files="ru.mystamps.web.validation.jsr303.FieldsMatch" />
24+
<suppress checks="WhitespaceAround" files="ru.mystamps.web.validation.jsr303.FieldsMismatch" />
25+
<suppress checks="WhitespaceAround" files="ru.mystamps.web.validation.jsr303.UniqueCountryName" />
26+
<suppress checks="WhitespaceAround" files="ru.mystamps.web.validation.jsr303.UniqueLogin" />
27+
<suppress checks="WhitespaceAround" files="ru.mystamps.web.validation.jsr303.ValidCredentials" />
28+
2129
</suppressions>

src/main/config/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
<!-- See http://checkstyle.sf.net/config_import.html -->
123123
<module name="AvoidStarImport"/>
124124
<module name="IllegalImport"> <!-- defaults to sun.* packages -->
125-
<property name="illegalPkgs" value="java.sql, org.hibernate, org.apache.log4j"/>
125+
<property name="illegalPkgs" value="java.sql, org.apache.log4j"/>
126126
</module>
127127
<module name="RedundantImport"/>
128128
<module name="UnusedImports"/>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
@ComponentScan(basePackages = {
4343
"ru.mystamps.web.controller",
4444
"ru.mystamps.web.dao",
45-
"ru.mystamps.web.service",
46-
"ru.mystamps.web.validation"
45+
"ru.mystamps.web.service"
4746
})
4847
public class MvcConfig extends WebMvcConfigurerAdapter {
4948

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Map;
2222

2323
import javax.inject.Inject;
24-
import javax.validation.Valid;
2524

2625
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
2726
import org.springframework.stereotype.Controller;
@@ -31,10 +30,16 @@
3130
import org.springframework.web.bind.annotation.RequestMethod;
3231
import org.springframework.web.bind.WebDataBinder;
3332
import org.springframework.validation.BindingResult;
33+
import org.springframework.validation.annotation.Validated;
3434

3535
import ru.mystamps.web.model.ActivateAccountForm;
36+
import ru.mystamps.web.model.ActivateAccountForm.LoginChecks;
37+
import ru.mystamps.web.model.ActivateAccountForm.NameChecks;
38+
import ru.mystamps.web.model.ActivateAccountForm.PasswordChecks;
39+
import ru.mystamps.web.model.ActivateAccountForm.PasswordConfirmationChecks;
40+
import ru.mystamps.web.model.ActivateAccountForm.ActKeyChecks;
41+
import ru.mystamps.web.model.ActivateAccountForm.FormChecks;
3642
import ru.mystamps.web.service.UserService;
37-
import ru.mystamps.web.validation.ActivateAccountValidator;
3843

3944
import static ru.mystamps.web.SiteMap.ACTIVATE_ACCOUNT_PAGE_URL;
4045
import static ru.mystamps.web.SiteMap.ACTIVATE_ACCOUNT_PAGE_WITH_KEY_URL;
@@ -44,21 +49,15 @@
4449
public class ActivateAccountController {
4550

4651
private final UserService userService;
47-
private final ActivateAccountValidator activateAccountValidator;
4852

4953
@Inject
50-
ActivateAccountController(
51-
final UserService userService,
52-
final ActivateAccountValidator activateAccountValidator) {
53-
54+
ActivateAccountController(final UserService userService) {
5455
this.userService = userService;
55-
this.activateAccountValidator = activateAccountValidator;
5656
}
5757

5858
@InitBinder
5959
protected void initBinder(final WebDataBinder binder) {
60-
binder.setValidator(activateAccountValidator);
61-
binder.registerCustomEditor(String.class, "name", new StringTrimmerEditor(false));
60+
binder.registerCustomEditor(String.class, "name", new StringTrimmerEditor(true));
6261
}
6362

6463
@RequestMapping(value = ACTIVATE_ACCOUNT_PAGE_URL, method = RequestMethod.GET)
@@ -78,8 +77,10 @@ public String showForm(@PathVariable("key") final String activationKey, final Ma
7877

7978
@RequestMapping(value = ACTIVATE_ACCOUNT_PAGE_URL, method = RequestMethod.POST)
8079
public String processInput(
81-
@Valid final ActivateAccountForm form,
82-
final BindingResult result) {
80+
@Validated({
81+
LoginChecks.class, NameChecks.class, PasswordChecks.class,
82+
PasswordConfirmationChecks.class, ActKeyChecks.class, FormChecks.class
83+
}) final ActivateAccountForm form, final BindingResult result) {
8384

8485
if (result.hasErrors()) {
8586
return null;

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import ru.mystamps.web.model.AddCountryForm;
3333
import ru.mystamps.web.service.CountryService;
34-
import ru.mystamps.web.validation.AddCountryValidator;
3534

3635
import static ru.mystamps.web.SiteMap.ADD_COUNTRY_PAGE_URL;
3736
import static ru.mystamps.web.SiteMap.INFO_COUNTRY_PAGE_URL;
@@ -40,20 +39,15 @@
4039
@RequestMapping(ADD_COUNTRY_PAGE_URL)
4140
public class AddCountryController {
4241

43-
private final AddCountryValidator addCountryValidator;
4442
private final CountryService countryService;
4543

4644
@Inject
47-
AddCountryController(
48-
final AddCountryValidator addCountryValidator,
49-
final CountryService countryService) {
50-
this.addCountryValidator = addCountryValidator;
45+
AddCountryController(final CountryService countryService) {
5146
this.countryService = countryService;
5247
}
5348

5449
@InitBinder
5550
protected void initBinder(final WebDataBinder binder) {
56-
binder.setValidator(addCountryValidator);
5751
binder.registerCustomEditor(String.class, "country", new StringTrimmerEditor(false));
5852
}
5953

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@
2121
import javax.inject.Inject;
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpSession;
24-
import javax.validation.Valid;
2524

2625
import org.springframework.stereotype.Controller;
27-
import org.springframework.web.bind.annotation.InitBinder;
2826
import org.springframework.web.bind.annotation.RequestHeader;
2927
import org.springframework.web.bind.annotation.RequestMapping;
3028
import org.springframework.web.bind.annotation.RequestMethod;
31-
import org.springframework.web.bind.WebDataBinder;
3229
import org.springframework.validation.BindingResult;
30+
import org.springframework.validation.annotation.Validated;
3331

3432
import ru.mystamps.web.entity.User;
3533
import ru.mystamps.web.model.AuthAccountForm;
34+
import ru.mystamps.web.model.AuthAccountForm.FormChecks;
35+
import ru.mystamps.web.model.AuthAccountForm.LoginChecks;
36+
import ru.mystamps.web.model.AuthAccountForm.PasswordChecks;
3637
import ru.mystamps.web.service.SiteService;
3738
import ru.mystamps.web.service.UserService;
38-
import ru.mystamps.web.validation.AuthAccountValidator;
3939

4040
import static ru.mystamps.web.SiteMap.INDEX_PAGE_URL;
4141
import static ru.mystamps.web.SiteMap.AUTHENTICATION_PAGE_URL;
@@ -46,22 +46,11 @@ public class AuthAccountController {
4646

4747
private final UserService userService;
4848
private final SiteService siteService;
49-
private final AuthAccountValidator authAccountValidator;
5049

5150
@Inject
52-
AuthAccountController(
53-
final UserService userService,
54-
final SiteService siteService,
55-
final AuthAccountValidator authAccountValidator) {
56-
51+
AuthAccountController(final UserService userService, final SiteService siteService) {
5752
this.userService = userService;
5853
this.siteService = siteService;
59-
this.authAccountValidator = authAccountValidator;
60-
}
61-
62-
@InitBinder
63-
protected void initAuthBinder(final WebDataBinder binder) {
64-
binder.setValidator(authAccountValidator);
6554
}
6655

6756
@RequestMapping(method = RequestMethod.GET)
@@ -75,18 +64,21 @@ public String processInput(
7564
final HttpSession session,
7665
@RequestHeader(value = "referer", required = false) final String referer,
7766
@RequestHeader(value = "user-agent", required = false) final String agent,
78-
@Valid final AuthAccountForm form,
67+
@Validated({
68+
LoginChecks.class,
69+
PasswordChecks.class,
70+
FormChecks.class
71+
}) final AuthAccountForm form,
7972
final BindingResult result) {
8073

8174
if (result.hasErrors()) {
8275

8376
// When user provides wrong login/password pair than
8477
// validation mechanism add error to form. Check this to
8578
// handle situation with wrong credentials.
86-
// @see AuthAccountValidator::validateLoginPasswordPair()
8779
if (result.hasGlobalErrors()
8880
&& result.getGlobalError() != null
89-
&& result.getGlobalError().getCode().equals("login.password.invalid")) {
81+
&& "ValidCredentials".equals(result.getGlobalError().getCode())) {
9082

9183
// TODO: log more info (login for example) (#59)
9284
// TODO: sanitize all user's values (#60)

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import ru.mystamps.web.model.RegisterAccountForm;
3333
import ru.mystamps.web.service.UserService;
34-
import ru.mystamps.web.validation.RegisterAccountValidator;
3534

3635
import static ru.mystamps.web.SiteMap.REGISTRATION_PAGE_URL;
3736
import static ru.mystamps.web.SiteMap.SUCCESSFUL_REGISTRATION_PAGE_URL;
@@ -41,20 +40,14 @@
4140
public class RegisterAccountController {
4241

4342
private final UserService userService;
44-
private final RegisterAccountValidator registerAccountValidator;
4543

4644
@Inject
47-
RegisterAccountController(
48-
final UserService userService,
49-
final RegisterAccountValidator registerAccountValidator) {
50-
45+
RegisterAccountController(final UserService userService) {
5146
this.userService = userService;
52-
this.registerAccountValidator = registerAccountValidator;
5347
}
5448

5549
@InitBinder
5650
protected void initBinder(final WebDataBinder binder) {
57-
binder.setValidator(registerAccountValidator);
5851
binder.registerCustomEditor(String.class, "email", new StringTrimmerEditor(false));
5952
}
6053

0 commit comments

Comments
 (0)