diff options
author | Alexander Alexeev | 2016-11-22 03:11:22 +0700 |
---|---|---|
committer | Vitaly Takmazov | 2016-11-23 13:03:06 +0300 |
commit | c3e73de64fb28f72cad4f92bc2a82935e4f4b7ee (patch) | |
tree | 9cd926f29ddd159bda9c763b086a4b5f642b850d | |
parent | 771c27021c033f5b6b9a3d9fdcd4048f9d8023af (diff) |
user not found page, post not found page
18 files changed, 249 insertions, 29 deletions
diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java index 65d07dba..dcbd5459 100644 --- a/juick-spring-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java @@ -6,7 +6,6 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; -import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @@ -30,12 +29,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { super(true); } - @Bean("authManager") - @Override - public AuthenticationManager authenticationManagerBean() throws Exception { - return super.authenticationManagerBean(); - } - @Bean("userDetailsService") @Override public UserDetailsService userDetailsServiceBean() throws Exception { diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java index 6b7b4ebc..6abb9f79 100644 --- a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java @@ -5,7 +5,6 @@ import com.juick.configuration.SearchConfiguration; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; -import javax.servlet.Filter; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -45,7 +44,7 @@ public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletIni encodingFilter.setForceEncoding(true); FilterRegistration.Dynamic registration = servletContext.addFilter( - "encodingFilter", new CharacterEncodingFilter()); + "encodingFilter", new CharacterEncodingFilter()); registration.addMappingForUrlPatterns(null, true, "/*"); } diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java index 01cee39f..869fd651 100644 --- a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java @@ -11,8 +11,9 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import org.springframework.web.servlet.resource.PathResourceResolver; import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring4.view.ThymeleafViewResolver; @@ -84,16 +85,21 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.setOrder(0); - registry.addResourceHandler("/scripts.js").addResourceLocations("/"); - registry.addResourceHandler("/style.css").addResourceLocations("/"); - registry.addResourceHandler("/favicon.ico").addResourceLocations("/static/favicon.ico"); + registry.addResourceHandler( + "/scripts.js*", + "/style.css*", + "/*.png", + "/favicon.ico") + .addResourceLocations("/") + .resourceChain(true) + .addResolver(new PathResourceResolver()); } @Bean public ResourceBundleMessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - messageSource.setBasename("messages"); + messageSource.setBasenames("messages", "errors"); messageSource.setDefaultEncoding("UTF-8"); messageSource.setFallbackToSystemLocale(false); @@ -101,6 +107,11 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport { } @Override - protected void addViewControllers(ViewControllerRegistry registry) { + public RequestMappingHandlerMapping requestMappingHandlerMapping() { + RequestMappingHandlerMapping result = super.requestMappingHandlerMapping(); + + result.setOrder(1); + + return result; } } diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java new file mode 100644 index 00000000..07266adc --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java @@ -0,0 +1,16 @@ +package com.juick.www.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by aalexeev on 11/21/16. + */ +@Controller +public class DivideByZeroController { + + @RequestMapping("/divideByZero") + public String getPage() { + return "divideByZero"; + } +} diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java index 338dc9be..b8c25135 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java @@ -1,13 +1,17 @@ package com.juick.www.controllers; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.juick.User; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import javax.inject.Inject; import java.util.regex.Pattern; /** @@ -17,36 +21,164 @@ import java.util.regex.Pattern; public class ShowMessageController { private static final Pattern USER_NAME_PATTERN = Pattern.compile("[a-zA-Z-_\\d]{2,16}"); - private static final Pattern POST_NUMBER_PATTERN = Pattern.compile("\\d+"); + private static final Pattern POST_NUMBER_PATTERN = Pattern.compile("-?\\d+"); - private final Logger logger = LoggerFactory.getLogger(getClass()); + @Inject + private UserService userService; + @Inject + private MessagesService messagesService; @RequestMapping("/{anything}") public String parseAnything( @PathVariable String anything, @RequestParam(required = false, defaultValue = "0") int before, + @RequestParam(required = false) String tag, Model model) { - boolean isUsername = USER_NAME_PATTERN.matcher(anything).matches(); - boolean isPostNumber = POST_NUMBER_PATTERN.matcher(anything).matches(); + if (StringUtils.isNotEmpty(tag)) { + StringBuilder builder = new StringBuilder(); - return "redirect:/"; + builder.append("redirect:/").append(anything).append("/tag/").append(tag); + + if (before > 0) + builder.append("?before=").append(before); + + return builder.toString(); + } + + if (before == 0) { + boolean isPostNumber = POST_NUMBER_PATTERN.matcher(anything).matches(); + int messageId = isPostNumber ? + NumberUtils.toInt(anything) : 0; + + if (isPostNumber && anything.equals(Integer.toString(messageId))) { + if (messageId > 0) { + com.juick.User author = messagesService.getMessageAuthor(messageId); + if (author != null) + return "redirect:/" + author.getName() + "/" + anything; + } else if (messageId == 0) + return "forward:/divideByZero.html"; + + model.addAttribute("messageId", anything); + + return "postNotFound"; + } + } + return showUserMessages(anything, null, before, model); + } + + @RequestMapping("/{userName}/tag/{tagName}") + public String showUserMessagesByTag( + @PathVariable String userName, + @PathVariable String tagName, + @RequestParam(required = false, defaultValue = "0") int before, + Model model) { + + return showUserMessages(userName, tagName, before, model); + } + + private String showUserMessages( + String userName, + String tagName, + int before, + Model model) { + // Check validity of user name before quering from database + if (!USER_NAME_PATTERN.matcher(userName).matches()) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + User user = userService.getUserByName(userName); + if (user == null) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + return "index"; + } + + @RequestMapping("/{userName}/friends") + public String getFriends( + @PathVariable String userName, + Model model) { + // Check validity of user name before quering from database + if (!USER_NAME_PATTERN.matcher(userName).matches()) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + User user = userService.getUserByName(userName); + if (user == null) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + return "index"; + } + + @RequestMapping("/{userName}/readers") + public String getReaders( + @PathVariable String userName, + Model model) { + // Check validity of user name before quering from database + if (!USER_NAME_PATTERN.matcher(userName).matches()) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + User user = userService.getUserByName(userName); + if (user == null) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + return "index"; + } + + @RequestMapping("/{userName}/tags") + public String geTags( + @PathVariable String userName, + Model model) { + // Check validity of user name before quering from database + if (!USER_NAME_PATTERN.matcher(userName).matches()) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + User user = userService.getUserByName(userName); + if (user == null) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + return "index"; } @RequestMapping("/{userName}/{postNumber}") public String checkShowPost( @PathVariable String userName, @PathVariable String postNumber, - @RequestParam(required = false, defaultValue = "0") int before, Model model) { + // Check validity of post number before quering from database + if (!POST_NUMBER_PATTERN.matcher(postNumber).matches()) { + model.addAttribute("messageId", postNumber); + return "postNotFound"; + } - if (!USER_NAME_PATTERN.matcher(userName).matches() || - !POST_NUMBER_PATTERN.matcher(postNumber).matches()) { - logger.warn("Invalid user name or post number, user name \"{}\", post number \"{}\"", userName, postNumber); - return "redirect:/"; + // Check validity of user name before quering from database + if (!USER_NAME_PATTERN.matcher(userName).matches()) { + model.addAttribute("userName", userName); + return "userNotFound"; } + User user = userService.getUserByName(userName); + if (user == null) { + model.addAttribute("userName", userName); + return "userNotFound"; + } + + return "index"; } } diff --git a/juick-spring-www/src/main/resources/errors.properties b/juick-spring-www/src/main/resources/errors.properties new file mode 100644 index 00000000..e0e14c56 --- /dev/null +++ b/juick-spring-www/src/main/resources/errors.properties @@ -0,0 +1,4 @@ +error.pageNotFound = Страница не найдена +errors.pageNotFound.extended = Похоже, пользователь удалил пост #{0}, или пост не был создан. +error.userNotFound = Пользователь не найден +errors.userNotFound.extended = Пользователь {0} не найден
\ No newline at end of file diff --git a/juick-spring-www/src/main/resources/errors_en.properties b/juick-spring-www/src/main/resources/errors_en.properties new file mode 100644 index 00000000..99e70b14 --- /dev/null +++ b/juick-spring-www/src/main/resources/errors_en.properties @@ -0,0 +1,4 @@ +error.pageNotFound = Page not found +errors.pageNotFound.extended = Probably, user deleted this post #{0}, or this post never existed. +error.userNotFound = User not found +errors.userNotFound.extended = User {0} not found
\ No newline at end of file diff --git a/juick-spring-www/src/main/resources/messages.properties b/juick-spring-www/src/main/resources/messages.properties index e69de29b..9749ddef 100644 --- a/juick-spring-www/src/main/resources/messages.properties +++ b/juick-spring-www/src/main/resources/messages.properties @@ -0,0 +1 @@ +return.toMain=Вернуться на главную
\ No newline at end of file diff --git a/juick-spring-www/src/main/resources/messages_en.properties b/juick-spring-www/src/main/resources/messages_en.properties index e69de29b..67fbb7d7 100644 --- a/juick-spring-www/src/main/resources/messages_en.properties +++ b/juick-spring-www/src/main/resources/messages_en.properties @@ -0,0 +1 @@ +return.toMain=Return to main
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/divideByZero.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/divideByZero.html new file mode 100644 index 00000000..7e03d74a --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/divideByZero.html @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org"> +<head> + <meta charset="UTF-8"> + <title>Title</title> +</head> +<body> +00000000000000000000000000000000000000000000000000000000000000000000000 + +</body> +</html>
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html index 566549bd..a5c5b348 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html @@ -1,10 +1,11 @@ <!DOCTYPE html> -<html lang="en"> +<html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> +<h1>Index page</h1> </body> </html>
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html index e69de29b..1182e61d 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html @@ -0,0 +1,3 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org"> +</html>
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html index 91a76f72..659a3fb0 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html @@ -1,5 +1,5 @@ <!DOCTYPE html> -<html> +<html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html new file mode 100644 index 00000000..c8712c9f --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html @@ -0,0 +1,22 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org"> +<head> + <meta charset="UTF-8"> + <title th:text="#{error.pageNotFound}">Страница не найдена</title> + <link rel="stylesheet" type="text/css" href="/style.css" th:href="@{/style.css}"/> +</head> + +<body> + +<div id="pagetitle"> + <h1 th:text="#{error.pageNotFound}">Страница не найдена</h1> +</div> + +<div id="wrapper"> + <p th:text="#{errors.pageNotFound.extended(${messageId})}">Похоже, пользователь удалил страницу, возможна она и не была создана.</p> +</div> +<div> + <a th:href="@{/}" href="/"><span th:text="#{return.toMain}">Вернуться на главную</span></a> +</div> +</body> +</html>
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html new file mode 100644 index 00000000..300b7730 --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html @@ -0,0 +1,22 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org"> +<head> + <meta charset="UTF-8"> + <title th:text="#{error.userNotFound}">Пользователь не найден</title> + <link rel="stylesheet" type="text/css" href="/style.css" th:href="@{/style.css}"/> +</head> + +<body> + +<div id="pagetitle"> + <h1 th:text="#{error.userNotFound}">Пользователь не найден</h1> +</div> + +<div id="wrapper"> + <p th:text="#{errors.userNotFound.extended(${userName})}">Пользователь не найден.</p> +</div> +<div> + <a th:href="@{/}" href="/"><span th:text="#{return.toMain}">Вернуться на главную</span></a> +</div> +</body> +</html>
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/favicon.ico b/juick-spring-www/src/main/webapp/favicon.ico Binary files differnew file mode 100644 index 00000000..bc7161e2 --- /dev/null +++ b/juick-spring-www/src/main/webapp/favicon.ico diff --git a/juick-spring-www/src/main/webapp/favicon.png b/juick-spring-www/src/main/webapp/favicon.png Binary files differnew file mode 100644 index 00000000..bc7161e2 --- /dev/null +++ b/juick-spring-www/src/main/webapp/favicon.png diff --git a/juick-spring-www/src/main/webapp/logo.png b/juick-spring-www/src/main/webapp/logo.png Binary files differnew file mode 100644 index 00000000..933f6099 --- /dev/null +++ b/juick-spring-www/src/main/webapp/logo.png |