Skip to content

Commit 618f549

Browse files
Merge pull request #13256 from etrandafir93/features/BAEL-6058-extracting_request_header
BAEL-6058: extracting request header
2 parents caa6632 + a844943 commit 618f549

7 files changed

Lines changed: 191 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.requestheader;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
import com.baeldung.requestheader.interceptor.OperatorHolder;
7+
8+
@RestController
9+
public class BuzzController {
10+
private final OperatorHolder operatorHolder;
11+
12+
public BuzzController(OperatorHolder operatorHolder) {
13+
this.operatorHolder = operatorHolder;
14+
}
15+
16+
@GetMapping("buzz")
17+
public String buzz() {
18+
return "hello, " + operatorHolder.getOperator();
19+
}
20+
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.requestheader;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestHeader;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class FooBarController {
11+
12+
@GetMapping("foo")
13+
public String foo(HttpServletRequest request) {
14+
String operator = request.getHeader("operator");
15+
return "hello, " + operator;
16+
}
17+
18+
@GetMapping("bar")
19+
public String bar(@RequestHeader("operator") String operator) {
20+
return "hello, " + operator;
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.requestheader;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
7+
@SpringBootApplication
8+
@EnableWebMvc
9+
public class HeaderInterceptorApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(HeaderInterceptorApplication.class, args);
13+
}
14+
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.requestheader.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Scope;
6+
import org.springframework.context.annotation.ScopedProxyMode;
7+
import org.springframework.web.context.WebApplicationContext;
8+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
10+
11+
import com.baeldung.requestheader.interceptor.OperatorHolder;
12+
import com.baeldung.requestheader.interceptor.OperatorInterceptor;
13+
14+
@Configuration
15+
public class HeaderInterceptorConfig implements WebMvcConfigurer {
16+
17+
@Override
18+
public void addInterceptors(final InterceptorRegistry registry) {
19+
registry.addInterceptor(operatorInterceptor());
20+
}
21+
22+
@Bean
23+
public OperatorInterceptor operatorInterceptor() {
24+
return new OperatorInterceptor(operatorHolder());
25+
}
26+
27+
@Bean
28+
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
29+
public OperatorHolder operatorHolder() {
30+
return new OperatorHolder();
31+
}
32+
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.requestheader.interceptor;
2+
3+
public class OperatorHolder {
4+
private String operator;
5+
6+
public String getOperator() {
7+
return operator;
8+
}
9+
10+
public void setOperator(String operator) {
11+
this.operator = operator;
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.requestheader.interceptor;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.springframework.web.servlet.HandlerInterceptor;
7+
8+
public class OperatorInterceptor implements HandlerInterceptor {
9+
10+
private final OperatorHolder operatorHolder;
11+
12+
@Override
13+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
14+
String operator = request.getHeader("operator");
15+
operatorHolder.setOperator(operator);
16+
return true;
17+
}
18+
19+
public OperatorInterceptor(OperatorHolder operatorHolder) {
20+
this.operatorHolder = operatorHolder;
21+
}
22+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.requestheader;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
6+
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.mock.web.MockHttpServletResponse;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit.jupiter.SpringExtension;
14+
import org.springframework.test.context.web.WebAppConfiguration;
15+
import org.springframework.test.web.servlet.MockMvc;
16+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
17+
import org.springframework.web.context.WebApplicationContext;
18+
19+
@ExtendWith(SpringExtension.class)
20+
@ContextConfiguration(classes = { HeaderInterceptorApplication.class })
21+
@WebAppConfiguration
22+
public class HeaderInterceptorIntegrationTest {
23+
24+
@Autowired
25+
private WebApplicationContext webApplicationContext;
26+
27+
private MockMvc mockMvc;
28+
29+
@BeforeEach
30+
public void setup() {
31+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext)
32+
.build();
33+
}
34+
35+
@Test
36+
public void givenARequestWithOperatorHeader_whenWeCallFooEndpoint_thenOperatorIsExtracted() throws Exception {
37+
MockHttpServletResponse response = this.mockMvc.perform(get("/foo").header("operator", "John.Doe"))
38+
.andDo(print())
39+
.andReturn()
40+
.getResponse();
41+
42+
assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe");
43+
}
44+
45+
@Test
46+
public void givenARequestWithOperatorHeader_whenWeCallBarEndpoint_thenOperatorIsExtracted() throws Exception {
47+
MockHttpServletResponse response = this.mockMvc.perform(get("/bar").header("operator", "John.Doe"))
48+
.andDo(print())
49+
.andReturn()
50+
.getResponse();
51+
52+
assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe");
53+
}
54+
55+
@Test
56+
public void givenARequestWithOperatorHeader_whenWeCallBuzzEndpoint_thenOperatorIsIntercepted() throws Exception {
57+
MockHttpServletResponse response = this.mockMvc.perform(get("/buzz").header("operator", "John.Doe"))
58+
.andDo(print())
59+
.andReturn()
60+
.getResponse();
61+
62+
assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe");
63+
}
64+
65+
}

0 commit comments

Comments
 (0)