X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fthymeproxy%2FApplication.java;h=1c33ce2fcaf2730d9ab59e5671ad2a485afba10d;hb=7ef40926db0d3d05443af767fece91ccbc2ddfa1;hp=b5a38cd1f1f12486107401c0253ed0b7b3be3439;hpb=23bc7e913d4e1afbda0805eea6371f36cf045b6d;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 b5a38cd..1c33ce2 100644 --- a/src/main/java/de/juplo/thymeproxy/Application.java +++ b/src/main/java/de/juplo/thymeproxy/Application.java @@ -2,26 +2,43 @@ package de.juplo.thymeproxy; import de.juplo.thymeleaf.JuploDialect; import java.util.HashMap; +import java.util.Locale; import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.http.HttpStatus; 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.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.exceptions.TemplateInputException; import org.thymeleaf.resourceresolver.IResourceResolver; import org.thymeleaf.resourceresolver.UrlResourceResolver; -import org.thymeleaf.spring4.SpringTemplateEngine; -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, @@ -31,7 +48,6 @@ public class Application extends WebMvcConfigurerAdapter 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); @@ -105,11 +121,18 @@ public class Application extends WebMvcConfigurerAdapter } @Bean - public ThymeleafView error(SpringTemplateEngine templateEngine) + public View error(ThymeleafViewResolver resolver) throws Exception { - return new ErrorThymeleafView(templateEngine, "/thymeleaf/error.html"); + return resolver.resolveViewName("templates/500", Locale.getDefault()); } + @Bean + public ErrorController errorController(ErrorAttributes errorAttributes) + { + return new CustomErrorController(errorAttributes, properties.getError()); + } + + @Override public void addViewControllers(ViewControllerRegistry registry) { @@ -123,12 +146,49 @@ public class Application extends WebMvcConfigurerAdapter } - static class ErrorThymeleafView extends ThymeleafView + static class CustomErrorController extends BasicErrorController { - ErrorThymeleafView(SpringTemplateEngine templateEngine, String templateName) + public final static String TEMPLATE_INPUT_EXCEPTION = + TemplateInputException.class.getCanonicalName(); + + + CustomErrorController( + ErrorAttributes errorAttributes, + ErrorProperties errorProperties + ) { - super(templateName); - setTemplateEngine(templateEngine); + super(errorAttributes, errorProperties); + } + + + @Override + public ModelAndView errorHtml( + HttpServletRequest request, + HttpServletResponse response + ) + { + Map model = + getErrorAttributes( + request, + isIncludeStackTrace(request, MediaType.TEXT_HTML) + ); + + String view; + + switch ((String)model.get("exception")) + { + case "org.thymeleaf.exceptions.TemplateInputException": + response.setStatus(HttpStatus.SC_NOT_FOUND); + view = "templates/404"; + break; + + default: + response.setStatus(getStatus(request).value()); + view = "templates/500"; + break; + } + + return new ModelAndView(view, model); } } } \ No newline at end of file