aboutsummaryrefslogtreecommitdiff
path: root/juick-spring-www/src/main/java/com/juick/www/controllers
diff options
context:
space:
mode:
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/HelpController.java78
1 files changed, 36 insertions, 42 deletions
diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/HelpController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/HelpController.java
index 49e24c8c..b5f0134c 100644
--- a/juick-spring-www/src/main/java/com/juick/www/controllers/HelpController.java
+++ b/juick-spring-www/src/main/java/com/juick/www/controllers/HelpController.java
@@ -1,23 +1,18 @@
package com.juick.www.controllers;
-import com.juick.User;
import com.juick.server.util.HttpNotFoundException;
-import com.juick.service.UserService;
import com.juick.util.UserUtils;
-import org.springframework.core.io.ClassPathResource;
+import com.juick.www.HelpService;
import org.springframework.stereotype.Controller;
-import org.springframework.ui.ModelMap;
+import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.inject.Inject;
import java.io.IOException;
import java.net.URISyntaxException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.security.Principal;
+import java.util.Locale;
import java.util.Optional;
-import java.util.stream.Collectors;
/**
* Created by aalexeev on 11/21/16.
@@ -25,44 +20,43 @@ import java.util.stream.Collectors;
@Controller
public class HelpController {
@Inject
- UserService userService;
+ private HelpService helpService;
- @RequestMapping({"/help", "/help/{lang}", "/help/{lang}/{page}"})
- protected String doGetHelp(
- Principal principal,
- @PathVariable("lang") Optional<String> lang,
- @PathVariable("page") Optional<String> page,
- ModelMap model) throws IOException, URISyntaxException {
- String name = UserUtils.getUsername(principal, null);
- User visitor = userService.getUserByName(name);
+ @RequestMapping({"/help", "/help/{langOrPage}", "/help/{lang}/{page}"})
+ public String showHelp(
+ Locale locale,
+ @PathVariable("lang") Optional<String> langParam,
+ @PathVariable("page") Optional<String> pageParam,
+ @PathVariable("langOrPage") Optional<String> langOrPageParam,
+ Model model) throws IOException, URISyntaxException {
- lang.ifPresent(l -> {
- if (l.length() != 2 || !l.matches("^[a-z]+$")) {
- throw new HttpNotFoundException();
- }
- });
- if (!lang.isPresent()) {
- lang = Optional.of("ru");
- }
+ String page = pageParam.orElse("index");
+ String lang = langParam.orElse(locale.getLanguage());
- page.ifPresent(p -> {
- if (!p.matches("^[a-zA-Z0-9\\-]*$") || p.equals("navigation") || p.equals("index")) {
- throw new HttpNotFoundException();
- }
- });
- if (!page.isPresent()) {
- page = Optional.of("index");
- }
+ model.addAttribute("visitor", UserUtils.getCurrentUser());
+ model.addAttribute("help_nav", getContent("navigation", lang, Optional.empty()));
+ model.addAttribute("help_data", getContent(page, lang, langOrPageParam));
- String filePath = "help/" + lang.get() + "/" + page.get();
- String navigationPath = "help/" + lang.get() + "/navigation";
-
- model.addAttribute("title", "Помощь");
- model.addAttribute("visitor", visitor);
- model.addAttribute("help_nav", Files.readAllLines(Paths.get(new ClassPathResource(navigationPath).getURI()))
- .stream().collect(Collectors.joining()));
- model.addAttribute("help_data", Files.readAllLines(Paths.get(new ClassPathResource(filePath).getURI()))
- .stream().collect(Collectors.joining()));
return "views/help";
}
+
+ private String getContent(String page, String lang, Optional<String> langOrPageParam) {
+ String content = null;
+
+ if (langOrPageParam.isPresent()) {
+ String anything = langOrPageParam.get();
+
+ if (helpService.canLang(anything))
+ content = helpService.getHelp(page, anything);
+
+ if (content == null && helpService.canPage(anything))
+ content = helpService.getHelp(anything, lang);
+ } else
+ content = helpService.getHelp(page, lang);
+
+ if (content != null)
+ return content;
+
+ throw new HttpNotFoundException();
+ }
}