blob: 338dc9be641ea189f546fb008882d49fa4861bb0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.juick.www.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 java.util.regex.Pattern;
/**
* Created by aalexeev on 11/21/16.
*/
@Controller
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 final Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping("/{anything}")
public String parseAnything(
@PathVariable String anything,
@RequestParam(required = false, defaultValue = "0") int before,
Model model) {
boolean isUsername = USER_NAME_PATTERN.matcher(anything).matches();
boolean isPostNumber = POST_NUMBER_PATTERN.matcher(anything).matches();
return "redirect:/";
}
@RequestMapping("/{userName}/{postNumber}")
public String checkShowPost(
@PathVariable String userName,
@PathVariable String postNumber,
@RequestParam(required = false, defaultValue = "0") int before,
Model model) {
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:/";
}
return "index";
}
}
|