Skip to content

Commit 0c6f156

Browse files
committed
refactor(AccountUrl): move account-related values to the corresponding class.
Addressed to #927 No functional changes.
1 parent a00046d commit 0c6f156

File tree

8 files changed

+78
-34
lines changed

8 files changed

+78
-34
lines changed

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

+7-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web;
1919

20+
import ru.mystamps.web.feature.account.AccountUrl;
21+
2022
import java.util.HashMap;
2123
import java.util.Map;
2224

@@ -41,12 +43,6 @@ public final class Url {
4143
public static final String DAILY_STATISTICS = "/report/daily";
4244
public static final String SITE_EVENTS_PAGE = "/site/events";
4345

44-
public static final String REGISTRATION_PAGE = "/account/register";
45-
public static final String AUTHENTICATION_PAGE = "/account/auth";
46-
public static final String LOGIN_PAGE = "/account/login";
47-
public static final String LOGOUT_PAGE = "/account/logout";
48-
public static final String ACTIVATE_ACCOUNT_PAGE = "/account/activate";
49-
5046
public static final String ADD_SERIES_PAGE = "/series/add";
5147
public static final String ADD_SERIES_ASK_PAGE = "/series/{id}/ask";
5248
public static final String INFO_SERIES_PAGE = "/series/{id}";
@@ -84,7 +80,6 @@ public final class Url {
8480
public static final String INTERNAL_ERROR_PAGE = "/error/500";
8581

8682
// For backward compatibility
87-
public static final String ACTIVATE_ACCOUNT_PAGE_WITH_KEY = "/account/activate/key/{key}";
8883
public static final String LIST_CATEGORIES_PAGE = "/category/list";
8984
public static final String LIST_COUNTRIES_PAGE = "/country/list";
9085
public static final String INFO_CATEGORY_BY_ID_PAGE = "/category/{id}/{slug}";
@@ -140,14 +135,14 @@ public static Map<String, String> asMap(boolean production) {
140135
// Not all URLs are listed here but only those that are being used on views
141136
// Constants sorted in an ascending order.
142137
Map<String, String> map = new HashMap<>();
143-
map.put("ACTIVATE_ACCOUNT_PAGE", ACTIVATE_ACCOUNT_PAGE);
138+
map.put("ACTIVATE_ACCOUNT_PAGE", AccountUrl.ACTIVATE_ACCOUNT_PAGE);
144139
map.put("ADD_CATEGORY_PAGE", ADD_CATEGORY_PAGE);
145140
map.put("ADD_COUNTRY_PAGE", ADD_COUNTRY_PAGE);
146141
map.put("ADD_IMAGE_SERIES_PAGE", ADD_IMAGE_SERIES_PAGE);
147142
map.put("ADD_PARTICIPANT_PAGE", ADD_PARTICIPANT_PAGE);
148143
map.put("ADD_SERIES_ASK_PAGE", ADD_SERIES_ASK_PAGE);
149144
map.put("ADD_SERIES_PAGE", ADD_SERIES_PAGE);
150-
map.put("AUTHENTICATION_PAGE", AUTHENTICATION_PAGE);
145+
map.put("AUTHENTICATION_PAGE", AccountUrl.AUTHENTICATION_PAGE);
151146
map.put("BOOTSTRAP_LANGUAGE", BOOTSTRAP_LANGUAGE);
152147
map.put("DAILY_STATISTICS", DAILY_STATISTICS);
153148
map.put("ESTIMATION_COLLECTION_PAGE", ESTIMATION_COLLECTION_PAGE);
@@ -159,10 +154,10 @@ public static Map<String, String> asMap(boolean production) {
159154
map.put("INFO_SERIES_PAGE", INFO_SERIES_PAGE);
160155
map.put("LIST_IMPORT_REQUESTS_PAGE", LIST_IMPORT_REQUESTS_PAGE);
161156
map.put("IMPORT_SERIES_SALES", IMPORT_SERIES_SALES);
162-
map.put("LOGIN_PAGE", LOGIN_PAGE);
163-
map.put("LOGOUT_PAGE", LOGOUT_PAGE);
157+
map.put("LOGIN_PAGE", AccountUrl.LOGIN_PAGE);
158+
map.put("LOGOUT_PAGE", AccountUrl.LOGOUT_PAGE);
164159
map.put("PUBLIC_URL", production ? PUBLIC_URL : SITE);
165-
map.put("REGISTRATION_PAGE", REGISTRATION_PAGE);
160+
map.put("REGISTRATION_PAGE", AccountUrl.REGISTRATION_PAGE);
166161
map.put("REQUEST_IMPORT_PAGE", REQUEST_IMPORT_PAGE);
167162
map.put("REQUEST_IMPORT_SERIES_PAGE", REQUEST_IMPORT_SERIES_PAGE);
168163
map.put("SEARCH_SERIES_BY_CATALOG", SEARCH_SERIES_BY_CATALOG);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.web.servlet.resource.VersionResourceResolver;
4242
import ru.mystamps.web.Url;
4343
import ru.mystamps.web.feature.account.AccountConfig;
44+
import ru.mystamps.web.feature.account.AccountUrl;
4445
import ru.mystamps.web.feature.category.CategoryConfig;
4546
import ru.mystamps.web.feature.category.CategoryLinkEntityDtoConverter;
4647
import ru.mystamps.web.feature.category.CategoryService;
@@ -100,7 +101,7 @@ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer conf
100101

101102
@Override
102103
public void addViewControllers(ViewControllerRegistry registry) {
103-
registry.addViewController(Url.AUTHENTICATION_PAGE);
104+
registry.addViewController(AccountUrl.AUTHENTICATION_PAGE);
104105
registry.addViewController(Url.FORBIDDEN_PAGE);
105106
}
106107

src/main/java/ru/mystamps/web/feature/account/AccountController.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.web.servlet.View;
3434
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
3535
import org.springframework.web.servlet.view.RedirectView;
36-
import ru.mystamps.web.Url;
3736
import ru.mystamps.web.feature.account.ActivateAccountForm.ActKeyChecks;
3837
import ru.mystamps.web.feature.account.ActivateAccountForm.FormChecks;
3938
import ru.mystamps.web.feature.account.ActivateAccountForm.LoginChecks;
@@ -62,12 +61,12 @@ protected void activationInitBinder(WebDataBinder binder) {
6261
binder.registerCustomEditor(String.class, "name", new StringTrimmerEditor(true));
6362
}
6463

65-
@GetMapping(Url.REGISTRATION_PAGE)
64+
@GetMapping(AccountUrl.REGISTRATION_PAGE)
6665
public RegisterAccountForm showRegistrationForm() {
6766
return new RegisterAccountForm();
6867
}
6968

70-
@PostMapping(Url.REGISTRATION_PAGE)
69+
@PostMapping(AccountUrl.REGISTRATION_PAGE)
7170
public String processRegistrationForm(
7271
@Valid RegisterAccountForm form,
7372
BindingResult result,
@@ -82,10 +81,10 @@ public String processRegistrationForm(
8281

8382
redirectAttributes.addFlashAttribute("justRegisteredUser", true);
8483

85-
return "redirect:" + Url.ACTIVATE_ACCOUNT_PAGE;
84+
return "redirect:" + AccountUrl.ACTIVATE_ACCOUNT_PAGE;
8685
}
8786

88-
@GetMapping(Url.ACTIVATE_ACCOUNT_PAGE)
87+
@GetMapping(AccountUrl.ACTIVATE_ACCOUNT_PAGE)
8988
public ActivateAccountForm showActivationForm(
9089
@RequestParam(name = "key", required = false) String activationKey) {
9190

@@ -97,7 +96,7 @@ public ActivateAccountForm showActivationForm(
9796
return form;
9897
}
9998

100-
@GetMapping(Url.ACTIVATE_ACCOUNT_PAGE_WITH_KEY)
99+
@GetMapping(AccountUrl.ACTIVATE_ACCOUNT_PAGE_WITH_KEY)
101100
public View showActivationFormWithKey(
102101
@PathVariable("key") String activationKey,
103102
RedirectAttributes redirectAttributes) {
@@ -106,12 +105,12 @@ public View showActivationFormWithKey(
106105

107106
RedirectView view = new RedirectView();
108107
view.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
109-
view.setUrl(Url.ACTIVATE_ACCOUNT_PAGE);
108+
view.setUrl(AccountUrl.ACTIVATE_ACCOUNT_PAGE);
110109

111110
return view;
112111
}
113112

114-
@PostMapping(Url.ACTIVATE_ACCOUNT_PAGE)
113+
@PostMapping(AccountUrl.ACTIVATE_ACCOUNT_PAGE)
115114
public String processActivationForm(
116115
@Validated({
117116
LoginChecks.class, NameChecks.class, PasswordChecks.class,
@@ -126,7 +125,7 @@ public String processActivationForm(
126125

127126
redirectAttributes.addFlashAttribute("justActivatedUser", true);
128127

129-
return "redirect:" + Url.AUTHENTICATION_PAGE;
128+
return "redirect:" + AccountUrl.AUTHENTICATION_PAGE;
130129
}
131130

132131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.account;
19+
20+
/**
21+
* Account-related URLs.
22+
*
23+
* Should be used everywhere instead of hard-coded paths.
24+
*
25+
* @author Slava Semushin
26+
*/
27+
@SuppressWarnings("PMD.CommentDefaultAccessModifier")
28+
public final class AccountUrl {
29+
30+
public static final String REGISTRATION_PAGE = "/account/register";
31+
public static final String AUTHENTICATION_PAGE = "/account/auth";
32+
public static final String LOGIN_PAGE = "/account/login";
33+
public static final String LOGOUT_PAGE = "/account/logout";
34+
public static final String ACTIVATE_ACCOUNT_PAGE = "/account/activate";
35+
36+
// For backward compatibility
37+
static final String ACTIVATE_ACCOUNT_PAGE_WITH_KEY = "/account/activate/key/{key}";
38+
39+
private AccountUrl() {
40+
}
41+
42+
}

src/main/java/ru/mystamps/web/feature/site/MailServiceImpl.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.context.MessageSource;
2727
import org.springframework.scheduling.annotation.Async;
2828
import ru.mystamps.web.Url;
29+
import ru.mystamps.web.feature.account.AccountUrl;
2930
import ru.mystamps.web.feature.account.SendUsersActivationDto;
3031
import ru.mystamps.web.feature.report.AdminDailyReport;
3132
import ru.mystamps.web.feature.report.ReportService;
@@ -123,8 +124,11 @@ private MailgunEmail prepareEmail() {
123124
private String getTextOfActivationMail(SendUsersActivationDto activation) {
124125
String template = messageSource.getMessage("activation.text", null, activation.getLocale());
125126

126-
String activationUrl =
127-
String.format("%s?key=%s", Url.ACTIVATE_ACCOUNT_PAGE, activation.getActivationKey());
127+
String activationUrl = String.format(
128+
"%s?key=%s",
129+
AccountUrl.ACTIVATE_ACCOUNT_PAGE,
130+
activation.getActivationKey()
131+
);
128132

129133
Map<String, String> ctx = new HashMap<>();
130134
ctx.put("site_url", testMode ? Url.SITE : Url.PUBLIC_URL);

src/main/java/ru/mystamps/web/feature/site/RobotsTxtController.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.stereotype.Controller;
2323
import org.springframework.web.bind.annotation.GetMapping;
2424
import ru.mystamps.web.Url;
25+
import ru.mystamps.web.feature.account.AccountUrl;
2526

2627
import javax.servlet.http.HttpServletResponse;
2728
import java.io.IOException;
@@ -42,10 +43,10 @@ public void getRobotsText(HttpServletResponse response) {
4243

4344
writer.println("# robots.txt for " + Url.PUBLIC_URL);
4445
writer.println("User-Agent: *");
45-
writer.println("Disallow: " + Url.REGISTRATION_PAGE);
46-
writer.println("Disallow: " + Url.ACTIVATE_ACCOUNT_PAGE);
47-
writer.println("Disallow: " + Url.AUTHENTICATION_PAGE);
48-
writer.println("Disallow: " + Url.LOGIN_PAGE);
46+
writer.println("Disallow: " + AccountUrl.REGISTRATION_PAGE);
47+
writer.println("Disallow: " + AccountUrl.ACTIVATE_ACCOUNT_PAGE);
48+
writer.println("Disallow: " + AccountUrl.AUTHENTICATION_PAGE);
49+
writer.println("Disallow: " + AccountUrl.LOGIN_PAGE);
4950
writer.println("Disallow: " + Url.ADD_COUNTRY_PAGE);
5051
writer.println("Disallow: " + Url.ADD_SERIES_PAGE);
5152
writer.println("Disallow: " + Url.ADD_CATEGORY_PAGE);

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.security.web.access.AccessDeniedHandler;
4343
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
4444
import ru.mystamps.web.Url;
45+
import ru.mystamps.web.feature.account.AccountUrl;
4546
import ru.mystamps.web.feature.account.UserService;
4647
import ru.mystamps.web.feature.site.SiteService;
4748

@@ -101,16 +102,16 @@ protected void configure(HttpSecurity http) throws Exception {
101102
.anyRequest().permitAll()
102103
.and()
103104
.formLogin()
104-
.loginPage(Url.AUTHENTICATION_PAGE)
105+
.loginPage(AccountUrl.AUTHENTICATION_PAGE)
105106
.usernameParameter("login")
106107
.passwordParameter("password")
107-
.loginProcessingUrl(Url.LOGIN_PAGE)
108-
.failureUrl(Url.AUTHENTICATION_PAGE + "?failed")
108+
.loginProcessingUrl(AccountUrl.LOGIN_PAGE)
109+
.failureUrl(AccountUrl.AUTHENTICATION_PAGE + "?failed")
109110
.defaultSuccessUrl(Url.INDEX_PAGE, true)
110111
.permitAll()
111112
.and()
112113
.logout()
113-
.logoutUrl(Url.LOGOUT_PAGE)
114+
.logoutUrl(AccountUrl.LOGOUT_PAGE)
114115
.logoutSuccessUrl(Url.INDEX_PAGE)
115116
.invalidateHttpSession(true)
116117
.permitAll()
@@ -181,7 +182,7 @@ public FilterRegistrationBean getResetLocaleFilter(
181182
bean.setOrder(requestContextFilter.getOrder() + 1);
182183

183184
// url pattern should match HttpSecurity.formLogin().loginProcessingUrl()
184-
bean.setUrlPatterns(Collections.singletonList(Url.LOGIN_PAGE));
185+
bean.setUrlPatterns(Collections.singletonList(AccountUrl.LOGIN_PAGE));
185186

186187
return bean;
187188
}

src/test/java/ru/mystamps/web/test/integration/cucumber/step/StepDefinitions.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openqa.selenium.WebDriver;
2626
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
2727
import ru.mystamps.web.Url;
28+
import ru.mystamps.web.feature.account.AccountUrl;
2829

2930
import static org.junit.Assert.assertEquals;
3031

@@ -36,7 +37,7 @@ public class StepDefinitions {
3637

3738
@Given("^as a user$")
3839
public void loginAsUser() {
39-
driver.get(Url.SITE + Url.AUTHENTICATION_PAGE);
40+
driver.get(Url.SITE + AccountUrl.AUTHENTICATION_PAGE);
4041
driver.findElement(By.id("login")).click();
4142
driver.findElement(By.id("login")).clear();
4243
driver.findElement(By.id("login")).sendKeys("coder");

0 commit comments

Comments
 (0)