1 package de.juplo.thymeproxy;
3 import de.juplo.thymeleaf.JuploDialect;
4 import java.util.HashMap;
5 import java.util.Locale;
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;
25 @SpringBootApplication
26 public class Application extends WebMvcConfigurerAdapter
29 private ServerProperties properties;
33 public RegexUrlHandlerMapping proxiedHandlerMapping(
34 ProxyHttpRequestHandler proxy,
35 UrlFilenameViewController views
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);
52 public ProxyHttpRequestHandler proxyHttpRequestHandler(
53 CloseableHttpClient client,
57 ProxyHttpRequestHandler handler = new ProxyHttpRequestHandler();
58 handler.setClient(client);
59 handler.setOrigin(env.getProperty("thymeproxy.origin"));
60 handler.setTtl(30000l);
65 public UrlFilenameViewController urlFilenameViewController()
67 return new UrlFilenameViewController();
71 public TemplateResolver defaultTemplateResolver(
72 IResourceResolver resources,
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")));
87 public TemplateResolver jsonTemplateResolver(
88 IResourceResolver resources,
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")));
103 public UrlResourceResolver thymeleafResourceResolver()
105 return new UrlResourceResolver();
109 public JuploDialect juploDialect()
111 return new JuploDialect();
115 public View error(ThymeleafViewResolver resolver) throws Exception
117 return resolver.resolveViewName("templates/500", Locale.getDefault());
121 public DefaultExceptionHandler defaultExceptionHandler()
123 return new DefaultExceptionHandler();
128 public void addViewControllers(ViewControllerRegistry registry)
130 registry.addViewController("/").setViewName("forward:index.html");
134 public static void main(String[] args)
136 SpringApplication.run(Application.class, args);