<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>
--- /dev/null
+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;
+ }
+ }
+}
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);
}
--- /dev/null
+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);
+ }
+}
-
+application.name=@project.artifactId@
+build.version=@project.version@
+build.timestamp=@timestamp@
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());
+ }
}