WIP: welcom + error - 404 anstatt 500
[maven-thymeleaf-skin] / src / main / java / de / juplo / thymeproxy / Application.java
index d4ccd53..7eb7333 100644 (file)
@@ -1,22 +1,34 @@
 package de.juplo.thymeproxy;
 
+import de.juplo.thymeleaf.JuploDialect;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.core.Ordered;
 import org.springframework.core.env.Environment;
+import org.springframework.web.servlet.View;
+import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 import org.springframework.web.servlet.mvc.UrlFilenameViewController;
 import org.thymeleaf.resourceresolver.IResourceResolver;
 import org.thymeleaf.resourceresolver.UrlResourceResolver;
+import org.thymeleaf.spring4.view.ThymeleafViewResolver;
 import org.thymeleaf.templateresolver.TemplateResolver;
 
 
 @SpringBootApplication
-public class Application
+public class Application extends WebMvcConfigurerAdapter
 {
+  @Autowired
+  private ServerProperties properties;
+
+
   @Bean
   public RegexUrlHandlerMapping proxiedHandlerMapping(
       ProxyHttpRequestHandler proxy,
@@ -27,10 +39,11 @@ public class Application
     mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
     Map<String, Object> mappings = new HashMap<>();
     mappings.put("/thymeleaf/.*\\.html", proxy);
+    mappings.put("/img/.+", proxy);
     mappings.put("/css/.+", proxy);
     mappings.put("/js/.+", proxy);
     mappings.put("/fonts/.+", proxy);
-    mappings.put(".*\\.html", views);
+    mappings.put("/.*\\.html", views);
     mapping.setUrlMap(mappings);
        return mapping;
   }
@@ -70,12 +83,53 @@ public class Application
     return resolver;
   }
 
+  @Bean
+  public TemplateResolver jsonTemplateResolver(
+      IResourceResolver resources,
+      Environment env
+      )
+  {
+    TemplateResolver resolver = new TemplateResolver();
+    resolver.setResourceResolver(resources);
+    resolver.setPrefix("http://localhost:8080/thymeleaf/");
+    resolver.setSuffix(".json");
+    resolver.setTemplateMode("JSON");
+    resolver.setCharacterEncoding("UTF-8");
+    resolver.setCacheable(Boolean.valueOf(env.getProperty("thymeproxy.cacheable")));
+    return resolver;
+  }
+
   @Bean
   public UrlResourceResolver thymeleafResourceResolver()
   {
     return new UrlResourceResolver();
   }
 
+  @Bean
+  public JuploDialect juploDialect()
+  {
+    return new JuploDialect();
+  }
+
+  @Bean
+  public View error(ThymeleafViewResolver resolver) throws Exception
+  {
+    return resolver.resolveViewName("templates/500", Locale.getDefault());
+  }
+
+  @Bean
+  public ExceptionMappings exceptionMappings()
+  {
+    return new ExceptionMappings();
+  }
+
+
+  @Override
+  public void addViewControllers(ViewControllerRegistry registry)
+  {
+    registry.addViewController("/").setViewName("forward:index.html");
+  }
+
 
   public static void main(String[] args)
   {