From e9e7aace3164288dfa17113140d69f9bbf974ec0 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Sun, 8 Jan 2023 16:59:46 +0100 Subject: [PATCH] BAEL-6058: extracting request header --- .../requestheader/BuzzController.java | 21 ++++++ .../requestheader/FooBarController.java | 22 +++++++ .../HeaderInterceptorApplication.java | 15 +++++ .../config/HeaderInterceptorConfig.java | 33 ++++++++++ .../interceptor/OperatorHolder.java | 13 ++++ .../interceptor/OperatorInterceptor.java | 22 +++++++ .../HeaderInterceptorApplicationTest.java | 66 +++++++++++++++++++ 7 files changed, 192 insertions(+) create mode 100644 spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/BuzzController.java create mode 100644 spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/FooBarController.java create mode 100644 spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/HeaderInterceptorApplication.java create mode 100644 spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/config/HeaderInterceptorConfig.java create mode 100644 spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorHolder.java create mode 100644 spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorInterceptor.java create mode 100644 spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/requestheader/HeaderInterceptorApplicationTest.java diff --git a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/BuzzController.java b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/BuzzController.java new file mode 100644 index 0000000000..09bf16f008 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/BuzzController.java @@ -0,0 +1,21 @@ +package com.baeldung.requestheader; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.requestheader.interceptor.OperatorHolder; + +@RestController +public class BuzzController { + private final OperatorHolder operatorHolder; + + public BuzzController(OperatorHolder operatorHolder) { + this.operatorHolder = operatorHolder; + } + + @GetMapping("buzz") + public String buzz() { + return "hello, " + operatorHolder.getOperator(); + } + +} diff --git a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/FooBarController.java b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/FooBarController.java new file mode 100644 index 0000000000..e0fd5f2f64 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/FooBarController.java @@ -0,0 +1,22 @@ +package com.baeldung.requestheader; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class FooBarController { + + @GetMapping("foo") + public String foo(HttpServletRequest request) { + String operator = request.getHeader("operator"); + return "hello, " + operator; + } + + @GetMapping("bar") + public String bar(@RequestHeader("operator") String operator) { + return "hello, " + operator; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/HeaderInterceptorApplication.java b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/HeaderInterceptorApplication.java new file mode 100644 index 0000000000..f2e9aaca12 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/HeaderInterceptorApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.requestheader; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@SpringBootApplication +@EnableWebMvc +public class HeaderInterceptorApplication { + + public static void main(String[] args) { + SpringApplication.run(HeaderInterceptorApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/config/HeaderInterceptorConfig.java b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/config/HeaderInterceptorConfig.java new file mode 100644 index 0000000000..07fb5b5184 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/config/HeaderInterceptorConfig.java @@ -0,0 +1,33 @@ +package com.baeldung.requestheader.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import com.baeldung.requestheader.interceptor.OperatorHolder; +import com.baeldung.requestheader.interceptor.OperatorInterceptor; + +@Configuration +public class HeaderInterceptorConfig implements WebMvcConfigurer { + + @Override + public void addInterceptors(final InterceptorRegistry registry) { + registry.addInterceptor(operatorInterceptor()); + } + + @Bean + public OperatorInterceptor operatorInterceptor() { + return new OperatorInterceptor(operatorHolder()); + } + + @Bean + @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) + public OperatorHolder operatorHolder() { + return new OperatorHolder(); + } + +} diff --git a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorHolder.java b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorHolder.java new file mode 100644 index 0000000000..31d36c0b59 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorHolder.java @@ -0,0 +1,13 @@ +package com.baeldung.requestheader.interceptor; + +public class OperatorHolder { + private String operator; + + public String getOperator() { + return operator; + } + + public void setOperator(String operator) { + this.operator = operator; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorInterceptor.java b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorInterceptor.java new file mode 100644 index 0000000000..0d0a0c7405 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-5/src/main/java/com/baeldung/requestheader/interceptor/OperatorInterceptor.java @@ -0,0 +1,22 @@ +package com.baeldung.requestheader.interceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.web.servlet.HandlerInterceptor; + +public class OperatorInterceptor implements HandlerInterceptor { + + private final OperatorHolder operatorHolder; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String operator = request.getHeader("operator"); + operatorHolder.setOperator(operator); + return true; + } + + public OperatorInterceptor(OperatorHolder operatorHolder) { + this.operatorHolder = operatorHolder; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/requestheader/HeaderInterceptorApplicationTest.java b/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/requestheader/HeaderInterceptorApplicationTest.java new file mode 100644 index 0000000000..77b48fbe99 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-5/src/test/java/com/baeldung/requestheader/HeaderInterceptorApplicationTest.java @@ -0,0 +1,66 @@ +package com.baeldung.requestheader; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = { HeaderInterceptorApplication.class }) +@WebAppConfiguration +public class HeaderInterceptorApplicationTest { + + @Autowired + private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @BeforeEach + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext) + .build(); + } + + @Test + public void givenARequestWithOperatorHeader_whenWeCallFooEndpoint_thenOperatorIsExtracted() throws Exception { + MockHttpServletResponse response = this.mockMvc.perform(get("/foo").header("operator", "John.Doe")) + .andDo(print()) + .andReturn() + .getResponse(); + + assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe"); + } + + @Test + public void givenARequestWithOperatorHeader_whenWeCallBarEndpoint_thenOperatorIsExtracted() throws Exception { + MockHttpServletResponse response = this.mockMvc.perform(get("/bar").header("operator", "John.Doe")) + .andDo(print()) + .andReturn() + .getResponse(); + + assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe"); + } + + @Test + public void givenARequestWithOperatorHeader_whenWeCallBuzzEndpoint_thenOperatorIsIntercepted() throws Exception { + MockHttpServletResponse response = this.mockMvc.perform(get("/buzz").header("operator", "John.Doe")) + .andDo(print()) + .andReturn() + .getResponse(); + + assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe"); + } + +} \ No newline at end of file