Resolving HTTP-status-codes explicitly to specialized error-pages
[maven-thymeleaf-skin] / src / main / java / de / juplo / thymeproxy / Application.java
index 2adafb2..6a3d7da 100644 (file)
@@ -3,36 +3,44 @@ package de.juplo.thymeproxy;
 import de.juplo.thymeleaf.JuploDialect;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
 import org.apache.http.impl.client.CloseableHttpClient;
-import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.web.ErrorAttributes;
+import org.springframework.boot.autoconfigure.web.ErrorController;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
+import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.core.Ordered;
 import org.springframework.core.env.Environment;
+import org.springframework.http.HttpStatus;
 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.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
+import org.thymeleaf.TemplateEngine;
 import org.thymeleaf.resourceresolver.IResourceResolver;
 import org.thymeleaf.resourceresolver.UrlResourceResolver;
-import org.thymeleaf.spring4.view.ThymeleafView;
-import org.thymeleaf.spring4.view.ThymeleafViewResolver;
 import org.thymeleaf.templateresolver.TemplateResolver;
 
 
 @SpringBootApplication
 public class Application extends WebMvcConfigurerAdapter
 {
+  @Autowired
+  private ServerProperties properties;
+
+
   @Bean
   public RegexUrlHandlerMapping proxiedHandlerMapping(
       ProxyHttpRequestHandler proxy,
-      UrlFilenameViewController views
+      UrlThymeleafViewController views
       )
   {
     RegexUrlHandlerMapping mapping = new RegexUrlHandlerMapping();
     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);
@@ -56,9 +64,11 @@ public class Application extends WebMvcConfigurerAdapter
   }
 
   @Bean
-  public UrlFilenameViewController urlFilenameViewController()
+  public UrlThymeleafViewController urlThymeleafViewController(
+      TemplateEngine engine
+      )
   {
-    return new UrlFilenameViewController();
+    return new UrlThymeleafViewController(engine);
   }
 
   @Bean
@@ -106,13 +116,38 @@ public class Application extends WebMvcConfigurerAdapter
   }
 
   @Bean
-  public ThymeleafView error()
+  public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
+  {
+    SimpleMappingExceptionResolver resolver =
+        new SimpleMappingExceptionResolver();
+
+    Properties mappings = new Properties();
+    mappings.setProperty("TemplateInputException", "templates/404");
+
+    resolver.setExceptionMappings(mappings);
+    resolver.setDefaultErrorView("templates/error");
+    resolver.setWarnLogCategory("exception");
+    return resolver;
+  }
+
+  @Bean
+  public ErrorController errorController(
+      ApplicationContext context,
+      ErrorAttributes errorAttributes
+      )
   {
-    ThymeleafView view = BeanUtils.instantiateClass(ThymeleafView.class);
-    view.setTemplateName("/thymeleaf/error.html");
-    return view;
+    ExceptionResolverErrorController controller =
+        new ExceptionResolverErrorController(
+            context,
+            errorAttributes,
+            properties.getError()
+            );
+    controller.addErrorMapping(HttpStatus.NOT_FOUND, "templates/404");
+    controller.setDefaultErrorView("templates/error");
+    return controller;
   }
 
+
   @Override
   public void addViewControllers(ViewControllerRegistry registry)
   {