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 /juick-spring-www/src/main/java/com/juick | |
parent | 771c27021c033f5b6b9a3d9fdcd4048f9d8023af (diff) |
user not found page, post not found page
Diffstat (limited to 'juick-spring-www/src/main/java/com/juick')
5 files changed, 178 insertions, 27 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 65d07dbaf..dcbd54596 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 6b7b4ebc9..6abb9f79e 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 01cee39fd..869fd6517 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 000000000..07266adcf --- /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 338dc9be6..b8c251352 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"; } } |