From 2243cb78fe142cfe8f55d0f6a76c3cfdb1cd2835 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sun, 11 Dec 2016 04:41:47 +0700 Subject: caching help --- .../src/main/java/com/juick/www/HelpService.java | 51 ++++++++++++++ .../www/configuration/WwwHelpConfiguration.java | 25 +++++++ .../juick/www/configuration/WwwInitializer.java | 6 +- .../com/juick/www/controllers/HelpController.java | 78 ++++++++++------------ .../juick/www/formatter/SpringDateFormatter.java | 1 - 5 files changed, 117 insertions(+), 44 deletions(-) create mode 100644 juick-spring-www/src/main/java/com/juick/www/HelpService.java create mode 100644 juick-spring-www/src/main/java/com/juick/www/configuration/WwwHelpConfiguration.java (limited to 'juick-spring-www/src') diff --git a/juick-spring-www/src/main/java/com/juick/www/HelpService.java b/juick-spring-www/src/main/java/com/juick/www/HelpService.java new file mode 100644 index 00000000..a3bee60a --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/HelpService.java @@ -0,0 +1,51 @@ +package com.juick.www; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.core.io.ClassPathResource; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.regex.Pattern; + +/** + * Created by aalexeev on 12/11/16. + */ +public class HelpService { + private static final Pattern LANG_PATTERN = Pattern.compile("[a-z]{2}"); + + private static final Pattern PAGE_PATTERN = Pattern.compile("[a-zA-Z0-9\\-_]+"); + + private final String helpPath; + + + public HelpService(String helpPath) { + this.helpPath = helpPath; + } + + @Cacheable("help") + public String getHelp(final String page, final String lang) { + if (canPage(page) && canLang(lang)) { + try { + Path file = Paths.get(helpPath, lang, page); + String path =file.toString(); + file = Paths.get(new ClassPathResource(path).getURI()); + + if (Files.isReadable(file)) + return new String(Files.readAllBytes(file)); + } catch (IOException e) { + } + } + + return null; + } + + public boolean canPage(final String anything) { + return anything != null && PAGE_PATTERN.matcher(anything).matches(); + } + + public boolean canLang(final String anything) { + return anything != null && LANG_PATTERN.matcher(anything).matches(); + } +} diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwHelpConfiguration.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwHelpConfiguration.java new file mode 100644 index 00000000..d3b1aa9b --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwHelpConfiguration.java @@ -0,0 +1,25 @@ +package com.juick.www.configuration; + +import com.juick.www.HelpService; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.guava.GuavaCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created by aalexeev on 12/11/16. + */ +@Configuration +@EnableCaching +public class WwwHelpConfiguration { + + @Bean + public GuavaCacheManager cacheManager() { + return new GuavaCacheManager("help"); + } + + @Bean + public HelpService helpService() { + return new HelpService("help"); + } +} 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 e62b1042..c8c162bd 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 @@ -17,7 +17,11 @@ public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { return new Class[]{ - WebAppConfiguration.class, DataConfiguration.class, SearchConfiguration.class, WebSecurityConfig.class}; + WebAppConfiguration.class, + DataConfiguration.class, + SearchConfiguration.class, + WebSecurityConfig.class, + WwwHelpConfiguration.class}; } @Override 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 lang, - @PathVariable("page") Optional 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 langParam, + @PathVariable("page") Optional pageParam, + @PathVariable("langOrPage") Optional 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 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(); + } } diff --git a/juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java b/juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java index 61a2fe88..bbc776c2 100644 --- a/juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java +++ b/juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java @@ -41,5 +41,4 @@ public class SpringDateFormatter implements Formatter { return new DateFormatter(pattern); } - } -- cgit v1.2.3