Skip to content

Commit 16c3c75

Browse files
committed
fix(validation): URL for import must be supported by one of the site parsers.
Fix #690 (/series/import/request page) Fix #1027 (series sale import form on /series/{id} page)
1 parent d7bed7c commit 16c3c75

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2009-2019 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.feature.series.importing;
19+
20+
import javax.validation.Constraint;
21+
import javax.validation.Payload;
22+
import java.lang.annotation.Documented;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.Target;
25+
26+
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
27+
import static java.lang.annotation.ElementType.FIELD;
28+
import static java.lang.annotation.ElementType.METHOD;
29+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
30+
31+
// @todo #690 Series import: add integration test to check that URL can be parsed
32+
// @todo #1027 Series sale import: add integration test to check that URL can be parsed
33+
/**
34+
* Validates that the URL can be parsed by one of the site parsers.
35+
*/
36+
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
37+
@Retention(RUNTIME)
38+
@Constraint(validatedBy = HasSiteParserValidator.class)
39+
@Documented
40+
public @interface HasSiteParser {
41+
String message() default "{ru.mystamps.web.feature.series.importing.HasSiteParser.message}";
42+
Class<?>[] groups() default {};
43+
Class<? extends Payload>[] payload() default {};
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2009-2019 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.feature.series.importing;
19+
20+
import lombok.RequiredArgsConstructor;
21+
import ru.mystamps.web.feature.series.importing.extractor.SiteParserService;
22+
23+
import javax.validation.ConstraintValidator;
24+
import javax.validation.ConstraintValidatorContext;
25+
26+
@RequiredArgsConstructor
27+
public class HasSiteParserValidator implements ConstraintValidator<HasSiteParser, String> {
28+
29+
private final SiteParserService siteParserService;
30+
31+
@Override
32+
public void initialize(HasSiteParser annotation) {
33+
// Intentionally empty: nothing to initialize
34+
}
35+
36+
@Override
37+
public boolean isValid(String value, ConstraintValidatorContext ctx) {
38+
39+
if (value == null) {
40+
return true;
41+
}
42+
43+
// @todo #690 HasSiteParserValidator: introduce SiteParserService.hasParserForUrl()
44+
return siteParserService.findForUrl(value) != null;
45+
}
46+
47+
}

src/main/java/ru/mystamps/web/feature/series/importing/RequestImportForm.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
RequestImportForm.class,
3333
Group.Level1.class,
3434
Group.Level2.class,
35-
Group.Level3.class
35+
Group.Level3.class,
36+
Group.Level4.class
3637
})
3738
public class RequestImportForm implements RequestImportDto {
3839

3940
// @todo #995 Series sale import: use its own interface and form
40-
// @todo #995 /series/sales/import: validate that we have a parser for this url
4141
@NotEmpty(groups = Group.Level1.class)
4242
@Size(
4343
// For series sales a max length is SERIES_SALES_URL_MAX_LENGTH but since they are equal,
@@ -49,5 +49,6 @@ public class RequestImportForm implements RequestImportDto {
4949
groups = Group.Level2.class
5050
)
5151
@URL(groups = Group.Level3.class)
52+
@HasSiteParser(groups = Group.Level4.class)
5253
private String url;
5354
}

src/main/resources/ru/mystamps/i18n/ValidationMessages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ru.mystamps.web.feature.country.UniqueCountryName.message = Country already exis
2828
ru.mystamps.web.feature.country.UniqueCountrySlug.message = Country with similar name already exists
2929
ru.mystamps.web.feature.series.ReleaseDateIsNotInFuture.message = Release date must not be in future
3030
ru.mystamps.web.feature.series.RequireImageOrImageUrl.message = Image or image URL must be specified
31+
ru.mystamps.web.feature.series.importing.HasSiteParser.message = Import from this site isn't supported
3132
3233
ru.mystamps.web.feature.series.DownloadResult.INVALID_URL = Invalid URL
3334
ru.mystamps.web.feature.series.DownloadResult.INVALID_REDIRECT = URL must not redirect to another address

src/main/resources/ru/mystamps/i18n/ValidationMessages_ru.properties

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ru.mystamps.web.feature.country.UniqueCountryName.message = Страна уже
2828
ru.mystamps.web.feature.country.UniqueCountrySlug.message = Страна с похожим названием уже есть в базе
2929
ru.mystamps.web.feature.series.ReleaseDateIsNotInFuture.message = Дата выпуска не может быть в будущем
3030
ru.mystamps.web.feature.series.RequireImageOrImageUrl.message = Необходимо выбрать изображение либо указать ссылку на него
31+
ru.mystamps.web.feature.series.importing.HasSiteParser.message = Импорт с данного сайта не поддерживается
3132

3233
ru.mystamps.web.feature.series.DownloadResult.INVALID_URL = Неправильный URL
3334
ru.mystamps.web.feature.series.DownloadResult.INVALID_REDIRECT = URL не должен перенаправлять на другой адрес

0 commit comments

Comments
 (0)