X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fthymeproxy%2FApplication.java;h=6a3d7da06f5544605e081c0443f1693ac5112e68;hb=53d411dcd6f52a22ec4513a9d43b3b5f737655a1;hp=0840e957814c8b7ab12d6f126bb848e927187a2e;hpb=551b78259633a9d1c42d8ed1c8e081ba5432fc06;p=maven-thymeleaf-skin diff --git a/src/main/java/de/juplo/thymeproxy/Application.java b/src/main/java/de/juplo/thymeproxy/Application.java index 0840e95..6a3d7da 100644 --- a/src/main/java/de/juplo/thymeproxy/Application.java +++ b/src/main/java/de/juplo/thymeproxy/Application.java @@ -3,25 +3,23 @@ package de.juplo.thymeproxy; import de.juplo.thymeleaf.JuploDialect; import java.util.HashMap; import java.util.Map; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import java.util.Properties; 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.BasicErrorController; import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ErrorController; -import org.springframework.boot.autoconfigure.web.ErrorProperties; 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.MediaType; -import org.springframework.web.servlet.ModelAndView; +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.templateresolver.TemplateResolver; @@ -37,13 +35,12 @@ public class Application extends WebMvcConfigurerAdapter @Bean public RegexUrlHandlerMapping proxiedHandlerMapping( ProxyHttpRequestHandler proxy, - UrlFilenameViewController views + UrlThymeleafViewController views ) { RegexUrlHandlerMapping mapping = new RegexUrlHandlerMapping(); mapping.setOrder(Ordered.HIGHEST_PRECEDENCE); Map mappings = new HashMap<>(); - mappings.put("/thymeleaf/.*\\.html", proxy); mappings.put("/img/.+", proxy); mappings.put("/css/.+", proxy); mappings.put("/js/.+", proxy); @@ -67,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 @@ -117,9 +116,35 @@ public class Application extends WebMvcConfigurerAdapter } @Bean - public ErrorController errorController(ErrorAttributes errorAttributes) + public SimpleMappingExceptionResolver simpleMappingExceptionResolver() { - return new CustomErrorController(errorAttributes, properties.getError()); + 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 + ) + { + ExceptionResolverErrorController controller = + new ExceptionResolverErrorController( + context, + errorAttributes, + properties.getError() + ); + controller.addErrorMapping(HttpStatus.NOT_FOUND, "templates/404"); + controller.setDefaultErrorView("templates/error"); + return controller; } @@ -134,29 +159,4 @@ public class Application extends WebMvcConfigurerAdapter { SpringApplication.run(Application.class, args); } - - - static class CustomErrorController extends BasicErrorController - { - CustomErrorController( - ErrorAttributes errorAttributes, - ErrorProperties errorProperties - ) - { - super(errorAttributes, errorProperties); - } - - - @Override - public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) - { - response.setStatus(getStatus(request).value()); - Map model = - getErrorAttributes( - request, - isIncludeStackTrace(request, MediaType.TEXT_HTML) - ); - return new ModelAndView("templates/error", model); - } - } } \ No newline at end of file