48bea7dbdeafebc022fc5e21852a8c3249235498
[maven-thymeleaf-skin] / src / main / java / de / juplo / thymeproxy / Application.java
1 package de.juplo.thymeproxy;
2
3 import de.juplo.thymeleaf.JuploDialect;
4 import java.util.HashMap;
5 import java.util.Locale;
6 import java.util.Map;
7 import org.apache.http.impl.client.CloseableHttpClient;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.SpringApplication;
10 import org.springframework.boot.autoconfigure.SpringBootApplication;
11 import org.springframework.boot.autoconfigure.web.ServerProperties;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.core.Ordered;
14 import org.springframework.core.env.Environment;
15 import org.springframework.web.servlet.View;
16 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
17 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
18 import org.springframework.web.servlet.mvc.UrlFilenameViewController;
19 import org.thymeleaf.resourceresolver.IResourceResolver;
20 import org.thymeleaf.resourceresolver.UrlResourceResolver;
21 import org.thymeleaf.spring4.view.ThymeleafViewResolver;
22 import org.thymeleaf.templateresolver.TemplateResolver;
23
24
25 @SpringBootApplication
26 public class Application extends WebMvcConfigurerAdapter
27 {
28   @Autowired
29   private ServerProperties properties;
30
31
32   @Bean
33   public RegexUrlHandlerMapping proxiedHandlerMapping(
34       ProxyHttpRequestHandler proxy,
35       UrlFilenameViewController views
36       )
37   {
38     RegexUrlHandlerMapping mapping = new RegexUrlHandlerMapping();
39     mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
40     Map<String, Object> mappings = new HashMap<>();
41     mappings.put("/thymeleaf/.*\\.html", proxy);
42     mappings.put("/img/.+", proxy);
43     mappings.put("/css/.+", proxy);
44     mappings.put("/js/.+", proxy);
45     mappings.put("/fonts/.+", proxy);
46     mappings.put("/.*\\.html", views);
47     mapping.setUrlMap(mappings);
48         return mapping;
49   }
50
51   @Bean
52   public ProxyHttpRequestHandler proxyHttpRequestHandler(
53       CloseableHttpClient client,
54       Environment env
55       )
56   {
57     ProxyHttpRequestHandler handler = new ProxyHttpRequestHandler();
58     handler.setClient(client);
59     handler.setOrigin(env.getProperty("thymeproxy.origin"));
60     handler.setTtl(30000l);
61     return handler;
62   }
63
64   @Bean
65   public UrlFilenameViewController urlFilenameViewController()
66   {
67     return new UrlFilenameViewController();
68   }
69
70   @Bean
71   public TemplateResolver defaultTemplateResolver(
72       IResourceResolver resources,
73       Environment env
74       )
75   {
76     TemplateResolver resolver = new TemplateResolver();
77     resolver.setResourceResolver(resources);
78     resolver.setPrefix("http://localhost:8080/thymeleaf/");
79     resolver.setSuffix(".html");
80     resolver.setTemplateMode("HTML5");
81     resolver.setCharacterEncoding("UTF-8");
82     resolver.setCacheable(Boolean.valueOf(env.getProperty("thymeproxy.cacheable")));
83     return resolver;
84   }
85
86   @Bean
87   public TemplateResolver jsonTemplateResolver(
88       IResourceResolver resources,
89       Environment env
90       )
91   {
92     TemplateResolver resolver = new TemplateResolver();
93     resolver.setResourceResolver(resources);
94     resolver.setPrefix("http://localhost:8080/thymeleaf/");
95     resolver.setSuffix(".json");
96     resolver.setTemplateMode("JSON");
97     resolver.setCharacterEncoding("UTF-8");
98     resolver.setCacheable(Boolean.valueOf(env.getProperty("thymeproxy.cacheable")));
99     return resolver;
100   }
101
102   @Bean
103   public UrlResourceResolver thymeleafResourceResolver()
104   {
105     return new UrlResourceResolver();
106   }
107
108   @Bean
109   public JuploDialect juploDialect()
110   {
111     return new JuploDialect();
112   }
113
114   @Bean
115   public View error(ThymeleafViewResolver resolver) throws Exception
116   {
117     return resolver.resolveViewName("templates/500", Locale.getDefault());
118   }
119
120   @Bean
121   public DefaultExceptionHandler defaultExceptionHandler()
122   {
123     return new DefaultExceptionHandler();
124   }
125
126
127   @Override
128   public void addViewControllers(ViewControllerRegistry registry)
129   {
130     registry.addViewController("/").setViewName("forward:index.html");
131   }
132
133
134   public static void main(String[] args)
135   {
136     SpringApplication.run(Application.class, args);
137   }
138 }