From cc551432bf80e4466b92c42a77a094f31408abeb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 27 Mar 2018 22:08:58 +0300 Subject: move url guessing logic from mapping to Filter --- .../com/juick/www/controllers/AnythingFilter.java | 67 ++++++++++++++++++++++ .../java/com/juick/www/controllers/Messages.java | 31 ---------- .../src/test/java/com/juick/www/WebAppTests.java | 5 ++ 3 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java (limited to 'juick-www') diff --git a/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java b/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java new file mode 100644 index 00000000..e5d2ec3f --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java @@ -0,0 +1,67 @@ +package com.juick.www.controllers; + +import com.juick.server.util.WebUtils; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import org.apache.commons.lang3.math.NumberUtils; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.web.util.UriComponents; + +import javax.inject.Inject; +import javax.servlet.*; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@Component +public class AnythingFilter implements Filter { + @Inject + private MessagesService messagesService; + @Inject + private UserService userService; + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + UriComponents components = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); + String anything = components.getPath().substring(1); + int before = NumberUtils.toInt(components.getQueryParams().getFirst("before"), 0); + if (before == 0) { + boolean isPostNumber = WebUtils.isPostNumber(anything); + 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) { + ((HttpServletResponse)servletResponse).sendRedirect("/" + author.getName() + "/" + anything); + return; + } + } + } + com.juick.User user = userService.getUserByName(anything); + if (user.getUid() > 0) { + ((HttpServletResponse)servletResponse).sendRedirect("/" + user.getName() + "/"); + } else { + filterChain.doFilter(servletRequest, servletResponse); + } + } else { + com.juick.User user = userService.getUserByName(anything); + if (user.getUid() > 0) { + ((HttpServletResponse) servletResponse).sendRedirect("/" + user.getName() + "/?before=" + before); + } else { + filterChain.doFilter(servletRequest, servletResponse); + } + } + } + + @Override + public void destroy() { + + } +} diff --git a/juick-www/src/main/java/com/juick/www/controllers/Messages.java b/juick-www/src/main/java/com/juick/www/controllers/Messages.java index ce21f97d..65e122a6 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Messages.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Messages.java @@ -81,37 +81,6 @@ public class Messages { .stream().sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).limit(20).map(t -> t.getTag().getName()).collect(Collectors.toList())); } - @GetMapping("/{anything}/**") - protected String parseAnyThing(@PathVariable String anything, - @RequestParam(required = false, defaultValue = "0") int before) throws IOException { - if (before == 0) { - boolean isPostNumber = WebUtils.isPostNumber(anything); - 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; - } - } - } - com.juick.User user = userService.getUserByName(anything); - if (user.getUid() > 0) { - return "redirect:/" + user.getName() + "/"; - } - throw new HttpNotFoundException(); - } - com.juick.User user = userService.getUserByName(anything); - if (user.getUid() > 0) { - return "redirect:/" + user.getName() + "/?before=" + before; - } else { - throw new HttpNotFoundException(); - } - } - @GetMapping("/") protected String doGet( @RequestParam(required = false) String tag, diff --git a/juick-www/src/test/java/com/juick/www/WebAppTests.java b/juick-www/src/test/java/com/juick/www/WebAppTests.java index e1fbace6..c34fcea8 100644 --- a/juick-www/src/test/java/com/juick/www/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/www/WebAppTests.java @@ -375,4 +375,9 @@ public class WebAppTests { .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/login/success")); } + @Test + public void anythingRedirects() throws Exception { + int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null); + mockMvc.perform(get(String.format("/%d", mid))).andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid))); + } } -- cgit v1.2.3