aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/configuration
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-28 22:38:21 +0300
committerGravatar Vitaly Takmazov2017-01-10 12:08:35 +0300
commit2f682b5e3cfc3fc5f961b60129be7bc90e0d6a03 (patch)
tree840aea9eb94d5c1f667a710d3298135854bc5002 /juick-www/src/main/java/com/juick/www/configuration
parenteb440ea4f120115613880e340b010eed5397e72c (diff)
juick-www: now on spring-webmvc
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/configuration')
-rw-r--r--juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java50
-rw-r--r--juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java52
-rw-r--r--juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java99
3 files changed, 201 insertions, 0 deletions
diff --git a/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java
new file mode 100644
index 00000000..cd681190
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java
@@ -0,0 +1,50 @@
+package com.juick.www.configuration;
+
+import com.juick.service.TagService;
+import com.juick.service.UserService;
+import com.juick.www.HelpService;
+import com.juick.www.controllers.PageTemplates;
+import com.juick.www.WebApp;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.guava.GuavaCacheManager;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.core.env.Environment;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+
+/**
+ * Created by aalexeev on 11/22/16.
+ */
+@Configuration
+@PropertySource("classpath:juick.conf")
+@EnableCaching
+public class WebAppConfiguration {
+ @Resource
+ private Environment env;
+ @Inject
+ private UserService userService;
+ @Inject
+ private TagService tagService;
+
+ @Bean
+ public WebApp webApp() {
+ return new WebApp(env, templates(), userService, tagService);
+ }
+ @Bean
+ public PageTemplates templates() {
+ return new PageTemplates();
+ }
+ @Bean
+ public GuavaCacheManager cacheManager() {
+ return new GuavaCacheManager("help");
+ }
+
+ @Bean
+ public HelpService helpService() {
+ return new HelpService("help");
+ }
+
+}
diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java
new file mode 100644
index 00000000..cd5429c2
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java
@@ -0,0 +1,52 @@
+package com.juick.www.configuration;
+
+import com.juick.configuration.DataConfiguration;
+import com.juick.configuration.SearchConfiguration;
+import org.apache.commons.lang3.CharEncoding;
+import org.springframework.web.filter.CharacterEncodingFilter;
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+import javax.servlet.FilterRegistration;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+/**
+ * Created by aalexeev on 11/20/16.
+ */
+public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
+
+ @Override
+ protected Class<?>[] getRootConfigClasses() {
+ return new Class<?>[]{
+ WebAppConfiguration.class,
+ DataConfiguration.class,
+ SearchConfiguration.class
+ };
+ }
+
+ @Override
+ protected Class<?>[] getServletConfigClasses() {
+ return new Class<?>[]{WwwServletConfiguration.class};
+ }
+
+ @Override
+ protected String[] getServletMappings() {
+ return new String[]{"/"};
+ }
+
+ @Override
+ protected String getServletName() {
+ return "WWW-spring dispatcher servlet";
+ }
+
+ @Override
+ public void onStartup(ServletContext servletContext) throws ServletException {
+ super.onStartup(servletContext);
+
+ FilterRegistration.Dynamic registration = servletContext.addFilter(
+ "encodingFilter", new CharacterEncodingFilter(CharEncoding.UTF_8, true));
+
+ registration.addMappingForUrlPatterns(null, true, "/*");
+ }
+}
+
diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java
new file mode 100644
index 00000000..c4e6c07c
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java
@@ -0,0 +1,99 @@
+package com.juick.www.configuration;
+
+import com.mitchellbosecke.pebble.PebbleEngine;
+import com.mitchellbosecke.pebble.loader.Loader;
+import com.mitchellbosecke.pebble.loader.ServletLoader;
+import com.mitchellbosecke.pebble.spring4.PebbleViewResolver;
+import com.mitchellbosecke.pebble.spring4.extension.SpringExtension;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.core.env.Environment;
+import org.springframework.http.CacheControl;
+import org.springframework.web.multipart.MultipartResolver;
+import org.springframework.web.multipart.commons.CommonsMultipartResolver;
+import org.springframework.web.servlet.ViewResolver;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
+import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
+import org.springframework.web.servlet.resource.PathResourceResolver;
+
+import javax.inject.Inject;
+import javax.servlet.ServletContext;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by vitalyster on 28.06.2016.
+ */
+@Configuration
+@ComponentScan(basePackages = {"com.juick.www.controllers"})
+@PropertySource("classpath:juick.conf")
+public class WwwServletConfiguration extends WebMvcConfigurationSupport {
+ @Inject
+ private Environment env;
+
+ @Override
+ protected void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.setOrder(0);
+ registry.addResourceHandler(
+ "/scripts.js*",
+ "/style.css*",
+ "/*.png",
+ "/favicon.ico")
+ .addResourceLocations("/")
+ .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
+ .resourceChain(true)
+ .addResolver(new PathResourceResolver());
+
+ registry.addResourceHandler("/static/**")
+ .addResourceLocations("/static/")
+ .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS));
+ }
+
+ @Override
+ public RequestMappingHandlerMapping requestMappingHandlerMapping() {
+ RequestMappingHandlerMapping result = super.requestMappingHandlerMapping();
+
+ result.setOrder(1);
+
+ return result;
+ }
+
+ @Bean
+ public MultipartResolver multipartResolver() {
+ CommonsMultipartResolver resolver = new CommonsMultipartResolver();
+ resolver.setMaxUploadSize(10000000);
+ return resolver;
+ }
+
+ @Inject
+ private ServletContext servletContext;
+
+ @Bean
+ public Loader templateLoader(){
+ return new ServletLoader(servletContext);
+ }
+
+ @Bean
+ public SpringExtension springExtension() {
+ return new SpringExtension();
+ }
+
+ @Bean
+ public PebbleEngine pebbleEngine() {
+ return new PebbleEngine.Builder()
+ .loader(this.templateLoader())
+ .extension(springExtension())
+ .build();
+ }
+
+ @Bean
+ public ViewResolver viewResolver() {
+ PebbleViewResolver viewResolver = new PebbleViewResolver();
+ viewResolver.setPrefix("/WEB-INF/");
+ viewResolver.setSuffix(".html");
+ viewResolver.setPebbleEngine(pebbleEngine());
+ return viewResolver;
+ }
+}