From 9639c89ecb4d10ffd9cfd6f4889247ae1881fc3a Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 7 Apr 2023 13:20:11 +0200 Subject: [PATCH 1/1] Demo: Send Status-Code in InterceptorHandler --- pom.xml | 11 ++++-- .../juplo/demo/BackendVersionInterceptor.java | 38 +++++++++++++++++++ .../java/de/juplo/demo/DemoApplication.java | 19 +++++++++- .../de/juplo/demo/DemoWebMvcConfigurer.java | 21 ++++++++++ src/main/resources/application.properties | 4 +- .../de/juplo/demo/DemoApplicationTests.java | 32 ++++++++++++++-- 6 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 src/main/java/de/juplo/demo/BackendVersionInterceptor.java create mode 100644 src/main/java/de/juplo/demo/DemoWebMvcConfigurer.java diff --git a/pom.xml b/pom.xml index 3a71f8c..b6262c5 100644 --- a/pom.xml +++ b/pom.xml @@ -9,10 +9,11 @@ de.juplo.demos - TEMPLATE + interceptor 0.0.1-SNAPSHOT - Demo: TEMPLATE - TEMPLATE for a Demo project for Spring Boot + Status-Code-HandlerInterceptor + Demo: Send Status-Code in HandlerInterceptor + 17 @@ -25,6 +26,10 @@ org.springframework.boot spring-boot-starter-web + + org.projectlombok + lombok + org.springframework.boot diff --git a/src/main/java/de/juplo/demo/BackendVersionInterceptor.java b/src/main/java/de/juplo/demo/BackendVersionInterceptor.java new file mode 100644 index 0000000..f8a046b --- /dev/null +++ b/src/main/java/de/juplo/demo/BackendVersionInterceptor.java @@ -0,0 +1,38 @@ +package de.juplo.demo; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.servlet.HandlerInterceptor; + +import java.io.IOException; + + +@RequiredArgsConstructor +public class BackendVersionInterceptor implements HandlerInterceptor +{ + public final static String BACKEND_VERSION = "X-Backend-Version"; + + + private final String backendVersion; + + + @Override + public boolean preHandle( + HttpServletRequest request, + HttpServletResponse response, + Object handler) throws IOException + { + String requstedVersion = request.getHeader(BACKEND_VERSION); + if (requstedVersion != null && !requstedVersion.equals(backendVersion)) + { + response.sendError(HttpStatus.GONE.value()); + return false; + } + else + { + return true; + } + } +} diff --git a/src/main/java/de/juplo/demo/DemoApplication.java b/src/main/java/de/juplo/demo/DemoApplication.java index 76f6fe4..2917409 100644 --- a/src/main/java/de/juplo/demo/DemoApplication.java +++ b/src/main/java/de/juplo/demo/DemoApplication.java @@ -1,12 +1,27 @@ package de.juplo.demo; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + @SpringBootApplication -public class DemoApplication { +public class DemoApplication +{ + @Value("${build.version}") + String projectVersion; + + + @Bean + public BackendVersionInterceptor backendVersionInterceptor() + { + return new BackendVersionInterceptor(projectVersion); + } + - public static void main(String[] args) { + public static void main(String[] args) + { SpringApplication.run(DemoApplication.class, args); } diff --git a/src/main/java/de/juplo/demo/DemoWebMvcConfigurer.java b/src/main/java/de/juplo/demo/DemoWebMvcConfigurer.java new file mode 100644 index 0000000..35fa5aa --- /dev/null +++ b/src/main/java/de/juplo/demo/DemoWebMvcConfigurer.java @@ -0,0 +1,21 @@ +package de.juplo.demo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + + +@Configuration +public class DemoWebMvcConfigurer implements WebMvcConfigurer +{ + @Autowired + BackendVersionInterceptor backendVersionInterceptor; + + + @Override + public void addInterceptors(InterceptorRegistry registry) + { + registry.addInterceptor(backendVersionInterceptor); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..5c35098 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,3 @@ - +application.name=@project.artifactId@ +build.version=@project.version@ +build.timestamp=@timestamp@ diff --git a/src/test/java/de/juplo/demo/DemoApplicationTests.java b/src/test/java/de/juplo/demo/DemoApplicationTests.java index 850debd..f2981dc 100644 --- a/src/test/java/de/juplo/demo/DemoApplicationTests.java +++ b/src/test/java/de/juplo/demo/DemoApplicationTests.java @@ -2,26 +2,52 @@ package de.juplo.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; +import static de.juplo.demo.BackendVersionInterceptor.BACKEND_VERSION; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc -class DemoApplicationTests { +class DemoApplicationTests +{ + + @Value("${build.version}") + String projectVersion; + @Test - void contextLoads() { + void contextLoads() + { } @Test - void getWelcomeResponseIsOk(@Autowired MockMvc mvc) throws Exception + void getWelcomeResponseIsOkIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception { mvc.perform(get("/")).andExpect(status().isOk()); } + + @Test + void getWelcomeResponseIsOkIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(get("/") + .header(BACKEND_VERSION, projectVersion)) + .andExpect(status().isOk()); + } + + @Test + void getWelcomeResponseIsGoneIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(get("/") + .header(BACKEND_VERSION, "FOO")) + .andExpect(status().isGone()); + } } -- 2.20.1