From 551b78259633a9d1c42d8ed1c8e081ba5432fc06 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 18 Jun 2016 21:53:34 +0200 Subject: [PATCH] WIP: welcome + error --- .../java/de/juplo/thymeproxy/Application.java | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/juplo/thymeproxy/Application.java b/src/main/java/de/juplo/thymeproxy/Application.java index b5a38cd..0840e95 100644 --- a/src/main/java/de/juplo/thymeproxy/Application.java +++ b/src/main/java/de/juplo/thymeproxy/Application.java @@ -3,25 +3,37 @@ 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 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.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.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.SpringTemplateEngine; -import org.thymeleaf.spring4.view.ThymeleafView; import org.thymeleaf.templateresolver.TemplateResolver; @SpringBootApplication public class Application extends WebMvcConfigurerAdapter { + @Autowired + private ServerProperties properties; + + @Bean public RegexUrlHandlerMapping proxiedHandlerMapping( ProxyHttpRequestHandler proxy, @@ -105,11 +117,12 @@ public class Application extends WebMvcConfigurerAdapter } @Bean - public ThymeleafView error(SpringTemplateEngine templateEngine) + public ErrorController errorController(ErrorAttributes errorAttributes) { - return new ErrorThymeleafView(templateEngine, "/thymeleaf/error.html"); + return new CustomErrorController(errorAttributes, properties.getError()); } + @Override public void addViewControllers(ViewControllerRegistry registry) { @@ -123,12 +136,27 @@ public class Application extends WebMvcConfigurerAdapter } - static class ErrorThymeleafView extends ThymeleafView + static class CustomErrorController extends BasicErrorController { - ErrorThymeleafView(SpringTemplateEngine templateEngine, String templateName) + CustomErrorController( + ErrorAttributes errorAttributes, + ErrorProperties errorProperties + ) + { + super(errorAttributes, errorProperties); + } + + + @Override + public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { - super(templateName); - setTemplateEngine(templateEngine); + 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 -- 2.20.1