Skip to content

Commit 070aa92

Browse files
committed
Prohibit repetition special characters
2 parents fc3da2c + 252dc70 commit 070aa92

18 files changed

+207
-154
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javax.servlet.http.HttpServletResponse;
2525
import javax.validation.Valid;
2626

27-
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
2827
import org.springframework.http.HttpStatus;
2928
import org.springframework.stereotype.Controller;
3029
import org.springframework.ui.Model;
@@ -43,7 +42,7 @@
4342
import ru.mystamps.web.Url;
4443
import ru.mystamps.web.controller.converter.annotation.Category;
4544
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
46-
import ru.mystamps.web.controller.editor.CustomCategoryNameEditor;
45+
import ru.mystamps.web.controller.editor.ReplaceRepeatingSpacesEditor;
4746
import ru.mystamps.web.dao.dto.LinkEntityDto;
4847
import ru.mystamps.web.dao.dto.SeriesInfoDto;
4948
import ru.mystamps.web.dao.dto.UrlEntityDto;
@@ -63,7 +62,9 @@ public class CategoryController {
6362

6463
@InitBinder("addCategoryForm")
6564
protected void initBinder(WebDataBinder binder) {
66-
CustomCategoryNameEditor nameEditor = new CustomCategoryNameEditor();
65+
// CheckStyle: ignore LineLength for next 1 line
66+
// We can't use StringTrimmerEditor here because "only one single registered custom editor per property path is supported".
67+
ReplaceRepeatingSpacesEditor nameEditor = new ReplaceRepeatingSpacesEditor(true);
6768
binder.registerCustomEditor(String.class, "name", nameEditor);
6869
binder.registerCustomEditor(String.class, "nameRu", nameEditor);
6970
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Objects;
2929

3030
import javax.servlet.http.HttpServletResponse;
31+
import javax.validation.Valid;
3132
import javax.validation.groups.Default;
3233

3334
import org.apache.commons.lang3.StringUtils;
@@ -232,7 +233,7 @@ public String showInfo(
232233

233234
@RequestMapping(value = Url.ADD_IMAGE_SERIES_PAGE, method = RequestMethod.POST)
234235
public String processImage(
235-
@Validated({ Default.class, AddImageForm.ImageChecks.class }) AddImageForm form,
236+
@Valid AddImageForm form,
236237
BindingResult result,
237238
@PathVariable("id") Integer seriesId,
238239
Model model,

src/main/java/ru/mystamps/web/controller/editor/CustomCategoryNameEditor.java renamed to src/main/java/ru/mystamps/web/controller/editor/ReplaceRepeatingSpacesEditor.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,31 @@
1818
package ru.mystamps.web.controller.editor;
1919

2020
import java.beans.PropertyEditorSupport;
21+
import java.util.regex.Pattern;
2122

22-
public class CustomCategoryNameEditor extends PropertyEditorSupport {
23-
private static final String REPEATING_SPACES_REGEXP = "[ ]{2,}";
23+
import lombok.RequiredArgsConstructor;
24+
25+
/**
26+
* @author Maxim Shestakov
27+
*/
28+
@RequiredArgsConstructor
29+
public class ReplaceRepeatingSpacesEditor extends PropertyEditorSupport {
30+
private static final Pattern REPEATING_SPACES = Pattern.compile("[ ]{2,}");
31+
private final boolean performTrimming;
2432

2533
@Override
2634
public void setAsText(String name) throws IllegalArgumentException {
27-
if (name.contains(" ")) {
28-
setValue(name.replaceAll(REPEATING_SPACES_REGEXP, " "));
29-
} else {
30-
setValue(name);
35+
String text = name;
36+
37+
if (performTrimming) {
38+
text = name.trim();
3139
}
40+
41+
if (text.contains(" ")) {
42+
REPEATING_SPACES.matcher(text).replaceAll(" ");
43+
}
44+
45+
setValue(text);
3246
}
3347

3448
}

src/main/java/ru/mystamps/web/model/ActivateAccountForm.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,19 @@ public class ActivateAccountForm implements ActivateAccountDto {
6262
groups = Login2Checks.class
6363
)
6464
})
65-
@Pattern(
66-
regexp = ValidationRules.LOGIN_REGEXP,
67-
message = "{login.invalid}",
68-
groups = Login3Checks.class
69-
)
70-
@UniqueLogin(groups = Login4Checks.class)
65+
@Pattern.List({
66+
@Pattern(
67+
regexp = ValidationRules.LOGIN_REGEXP,
68+
message = "{login.invalid}",
69+
groups = Login3Checks.class
70+
),
71+
@Pattern(
72+
regexp = ValidationRules.LOGIN_REPEATING_CHARS_REGEXP,
73+
message = "{login.repetition_chars}",
74+
groups = Login4Checks.class
75+
)
76+
})
77+
@UniqueLogin(groups = Login5Checks.class)
7178
private String login;
7279

7380
@Size(
@@ -120,7 +127,8 @@ public class ActivateAccountForm implements ActivateAccountDto {
120127
Login1Checks.class,
121128
Login2Checks.class,
122129
Login3Checks.class,
123-
Login4Checks.class
130+
Login4Checks.class,
131+
Login5Checks.class
124132
})
125133
public interface LoginChecks {
126134
}
@@ -137,6 +145,9 @@ public interface Login3Checks {
137145
public interface Login4Checks {
138146
}
139147

148+
public interface Login5Checks {
149+
}
150+
140151
@GroupSequence({
141152
Name1Checks.class,
142153
Name2Checks.class,

src/main/java/ru/mystamps/web/model/AddCategoryForm.java

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,103 +34,86 @@
3434
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_MAX_LENGTH;
3535
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_MIN_LENGTH;
3636
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_NO_HYPHEN_REGEXP;
37+
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_NO_REPEATING_HYPHENS_REGEXP;
3738
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_RU_REGEXP;
38-
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_NO_REPEATS_HYPHENS;
3939

4040
@Getter
4141
@Setter
4242
@GroupSequence({
4343
AddCategoryForm.class,
44-
AddCategoryForm.Level1Checks.class,
45-
AddCategoryForm.Level2Checks.class,
46-
AddCategoryForm.Level3Checks.class,
47-
AddCategoryForm.Level4Checks.class,
48-
AddCategoryForm.Level5Checks.class
44+
Group.Level1.class,
45+
Group.Level2.class,
46+
Group.Level3.class,
47+
Group.Level4.class,
48+
Group.Level5.class,
49+
Group.Level6.class
4950
})
5051
public class AddCategoryForm implements AddCategoryDto {
5152

52-
@NotEmpty(groups = Level1Checks.class)
53+
@NotEmpty(groups = Group.Level1.class)
5354
@Size.List({
5455
@Size(
5556
min = CATEGORY_NAME_MIN_LENGTH,
5657
message = "{value.too-short}",
57-
groups = Level2Checks.class
58+
groups = Group.Level2.class
5859
),
5960
@Size(
6061
max = CATEGORY_NAME_MAX_LENGTH,
6162
message = "{value.too-long}",
62-
groups = Level2Checks.class
63+
groups = Group.Level2.class
6364
)
6465
})
6566
@Pattern.List({
6667
@Pattern(
6768
regexp = CATEGORY_NAME_EN_REGEXP,
6869
message = "{category-name-en.invalid}",
69-
groups = Level3Checks.class
70+
groups = Group.Level3.class
7071
),
7172
@Pattern(
72-
regexp = CATEGORY_NAME_NO_HYPHEN_REGEXP,
73-
message = "{value.hyphen}",
74-
groups = Level4Checks.class
73+
regexp = CATEGORY_NAME_NO_REPEATING_HYPHENS_REGEXP,
74+
message = "{value.repeating_hyphen}",
75+
groups = Group.Level4.class
7576
),
7677
@Pattern(
77-
regexp = CATEGORY_NAME_NO_REPEATS_HYPHENS,
78-
message = "{value.hyphen_repeat_en}",
79-
groups = Level5Checks.class
78+
regexp = CATEGORY_NAME_NO_HYPHEN_REGEXP,
79+
message = "{value.hyphen}",
80+
groups = Group.Level5.class
8081
)
8182
})
82-
@UniqueCategoryName(lang = Lang.EN, groups = Level6Checks.class)
83+
@UniqueCategoryName(lang = Lang.EN, groups = Group.Level6.class)
8384
private String name;
8485

85-
@NotEmpty(groups = Level1Checks.class)
86+
@NotEmpty(groups = Group.Level1.class)
8687
@Size.List({
8788
@Size(
8889
min = CATEGORY_NAME_MIN_LENGTH,
8990
message = "{value.too-short}",
90-
groups = Level2Checks.class
91+
groups = Group.Level2.class
9192
),
9293
@Size(
9394
max = CATEGORY_NAME_MAX_LENGTH,
9495
message = "{value.too-long}",
95-
groups = Level2Checks.class
96+
groups = Group.Level2.class
9697
)
9798
})
9899
@Pattern.List({
99100
@Pattern(
100101
regexp = CATEGORY_NAME_RU_REGEXP,
101102
message = "{category-name-ru.invalid}",
102-
groups = Level3Checks.class
103+
groups = Group.Level3.class
103104
),
104105
@Pattern(
105-
regexp = CATEGORY_NAME_NO_HYPHEN_REGEXP,
106-
message = "{value.hyphen}",
107-
groups = Level4Checks.class
106+
regexp = CATEGORY_NAME_NO_REPEATING_HYPHENS_REGEXP,
107+
message = "{value.repeating_hyphen}",
108+
groups = Group.Level4.class
108109
),
109110
@Pattern(
110-
regexp = CATEGORY_NAME_NO_REPEATS_HYPHENS,
111-
message = "{value.hyphen_repeat_ru}",
112-
groups = Level5Checks.class
111+
regexp = CATEGORY_NAME_NO_HYPHEN_REGEXP,
112+
message = "{value.hyphen}",
113+
groups = Group.Level5.class
113114
)
114115
})
115-
@UniqueCategoryName(lang = Lang.RU, groups = Level6Checks.class)
116+
@UniqueCategoryName(lang = Lang.RU, groups = Group.Level6.class)
116117
private String nameRu;
117118

118-
public interface Level1Checks {
119-
}
120-
121-
public interface Level2Checks {
122-
}
123-
124-
public interface Level3Checks {
125-
}
126-
127-
public interface Level4Checks {
128-
}
129-
130-
public interface Level5Checks {
131-
}
132-
133-
public interface Level6Checks {
134-
}
135-
136119
}

src/main/java/ru/mystamps/web/model/AddCountryForm.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,83 +40,68 @@
4040
@Setter
4141
@GroupSequence({
4242
AddCountryForm.class,
43-
AddCountryForm.Level1Checks.class,
44-
AddCountryForm.Level2Checks.class,
45-
AddCountryForm.Level3Checks.class,
46-
AddCountryForm.Level4Checks.class,
47-
AddCountryForm.Level5Checks.class
43+
Group.Level1.class,
44+
Group.Level2.class,
45+
Group.Level3.class,
46+
Group.Level4.class,
47+
Group.Level5.class
4848
})
4949
public class AddCountryForm implements AddCountryDto {
5050

51-
@NotEmpty(groups = Level1Checks.class)
51+
@NotEmpty(groups = Group.Level1.class)
5252
@Size.List({
5353
@Size(
5454
min = COUNTRY_NAME_MIN_LENGTH,
5555
message = "{value.too-short}",
56-
groups = Level2Checks.class
56+
groups = Group.Level2.class
5757
),
5858
@Size(
5959
max = COUNTRY_NAME_MAX_LENGTH,
6060
message = "{value.too-long}",
61-
groups = Level2Checks.class
61+
groups = Group.Level2.class
6262
)
6363
})
6464
@Pattern.List({
6565
@Pattern(
6666
regexp = COUNTRY_NAME_EN_REGEXP,
6767
message = "{country-name-en.invalid}",
68-
groups = Level3Checks.class
68+
groups = Group.Level3.class
6969
),
7070
@Pattern(
7171
regexp = COUNTRY_NAME_NO_HYPHEN_REGEXP,
7272
message = "{value.hyphen}",
73-
groups = Level4Checks.class
73+
groups = Group.Level4.class
7474
)
7575
})
76-
@UniqueCountryName(lang = Lang.EN, groups = Level5Checks.class)
76+
@UniqueCountryName(lang = Lang.EN, groups = Group.Level5.class)
7777
private String name;
7878

79-
@NotEmpty(groups = Level1Checks.class)
79+
@NotEmpty(groups = Group.Level1.class)
8080
@Size.List({
8181
@Size(
8282
min = COUNTRY_NAME_MIN_LENGTH,
8383
message = "{value.too-short}",
84-
groups = Level2Checks.class
84+
groups = Group.Level2.class
8585
),
8686
@Size(
8787
max = COUNTRY_NAME_MAX_LENGTH,
8888
message = "{value.too-long}",
89-
groups = Level2Checks.class
89+
groups = Group.Level2.class
9090
)
9191
})
9292
@Pattern.List({
9393
@Pattern(
9494
regexp = COUNTRY_NAME_RU_REGEXP,
9595
message = "{country-name-ru.invalid}",
96-
groups = Level3Checks.class
96+
groups = Group.Level3.class
9797
),
9898
@Pattern(
9999
regexp = COUNTRY_NAME_NO_HYPHEN_REGEXP,
100100
message = "{value.hyphen}",
101-
groups = Level4Checks.class
101+
groups = Group.Level4.class
102102
)
103103
})
104-
@UniqueCountryName(lang = Lang.RU, groups = Level5Checks.class)
104+
@UniqueCountryName(lang = Lang.RU, groups = Group.Level5.class)
105105
private String nameRu;
106106

107-
public interface Level1Checks {
108-
}
109-
110-
public interface Level2Checks {
111-
}
112-
113-
public interface Level3Checks {
114-
}
115-
116-
public interface Level4Checks {
117-
}
118-
119-
public interface Level5Checks {
120-
}
121-
122107
}

0 commit comments

Comments
 (0)