aboutsummaryrefslogtreecommitdiff
path: root/juick-spring-www/src/main/java/com/juick/www/controllers
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2016-11-22 03:11:22 +0700
committerGravatar Vitaly Takmazov2016-11-23 13:03:06 +0300
commitc3e73de64fb28f72cad4f92bc2a82935e4f4b7ee (patch)
tree9cd926f29ddd159bda9c763b086a4b5f642b850d /juick-spring-www/src/main/java/com/juick/www/controllers
parent771c27021c033f5b6b9a3d9fdcd4048f9d8023af (diff)
user not found page, post not found page
Diffstat (limited to 'juick-spring-www/src/main/java/com/juick/www/controllers')
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java16
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java156
2 files changed, 160 insertions, 12 deletions
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";
}
}