Demo: Send Status-Code in InterceptorHandler
authorKai Moritz <kai@juplo.de>
Fri, 7 Apr 2023 11:20:11 +0000 (13:20 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 9 Apr 2023 13:18:01 +0000 (15:18 +0200)
pom.xml
src/main/java/de/juplo/demo/BackendVersionInterceptor.java [new file with mode: 0644]
src/main/java/de/juplo/demo/DemoApplication.java
src/main/java/de/juplo/demo/DemoWebMvcConfigurer.java [new file with mode: 0644]
src/main/resources/application.properties
src/test/java/de/juplo/demo/DemoApplicationTests.java

diff --git a/pom.xml b/pom.xml
index 3a71f8c..b6262c5 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -9,10 +9,11 @@
                <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>de.juplo.demos</groupId>
-       <artifactId>TEMPLATE</artifactId>
+       <artifactId>interceptor</artifactId>
        <version>0.0.1-SNAPSHOT</version>
-       <name>Demo: TEMPLATE</name>
-       <description>TEMPLATE for a Demo project for Spring Boot</description>
+       <name>Status-Code-HandlerInterceptor</name>
+       <description>Demo: Send Status-Code in HandlerInterceptor</description>
+
        <properties>
                <java.version>17</java.version>
        </properties>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
+               <dependency>
+                       <groupId>org.projectlombok</groupId>
+                       <artifactId>lombok</artifactId>
+               </dependency>
 
                <dependency>
                        <groupId>org.springframework.boot</groupId>
diff --git a/src/main/java/de/juplo/demo/BackendVersionInterceptor.java b/src/main/java/de/juplo/demo/BackendVersionInterceptor.java
new file mode 100644 (file)
index 0000000..f8a046b
--- /dev/null
@@ -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;
+    }
+  }
+}
index 76f6fe4..2917409 100644 (file)
@@ -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 (file)
index 0000000..35fa5aa
--- /dev/null
@@ -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);
+  }
+}
index 8b13789..5c35098 100644 (file)
@@ -1 +1,3 @@
-
+application.name=@project.artifactId@
+build.version=@project.version@
+build.timestamp=@timestamp@
index 850debd..f2981dc 100644 (file)
@@ -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());
+       }
 }