* Added code for BAEL-1972 (#4632)

This commit is contained in:
rozagerardo 2018-07-08 05:40:02 -03:00 committed by Grzegorz Piwowarek
parent e78ca7c830
commit 3786d4e1b1
10 changed files with 411 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.baeldung.reactive.responseheaders.controllers;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/response-header")
public class ResponseHeaderController {
@GetMapping("/response-entity")
public Mono<ResponseEntity<String>> usingResponseEntityBuilder() {
String responseHeaderKey = "Baeldung-Example-Header";
String responseHeaderValue = "Value-ResponseEntityBuilder";
String responseBody = "Response with header using ResponseEntity (builder)";
return Mono.just(ResponseEntity.ok()
.header(responseHeaderKey, responseHeaderValue)
.body(responseBody));
}
@GetMapping("/server-http-response")
public Mono<String> usingServerHttpResponse(ServerHttpResponse response) {
String responseHeaderKey = "Baeldung-Example-Header";
String responseHeaderValue = "Value-ServerHttpResponse";
String responseBody = "Response with header using ServerHttpResponse";
response.getHeaders().add(responseHeaderKey, responseHeaderValue);
return Mono.just(responseBody);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.reactive.responseheaders.filter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
@Component
public class AddResponseHeaderWebFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
exchange.getResponse()
.getHeaders()
.add("Baeldung-Example-Filter-Header", "Value-Filter");
return chain.filter(exchange);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.reactive.responseheaders.functional.handlers;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class ResponseHeaderHandler {
public Mono<ServerResponse> useHandler(final ServerRequest request) {
String responseHeaderKey = "Baeldung-Example-Header";
String responseHeaderValue = "Value-Handler";
String responseBody = "Response with header using Handler";
return ServerResponse.ok()
.header(responseHeaderKey, responseHeaderValue)
.body(Mono.just(responseBody),String.class);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.reactive.responseheaders.functional.routers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import com.baeldung.reactive.responseheaders.functional.handlers.ResponseHeaderHandler;
@Configuration
public class ResponseHeadersRouterFunctions {
@Bean
public RouterFunction<ServerResponse> responseHeaderRoute(@Autowired ResponseHeaderHandler handler) {
return RouterFunctions.route(RequestPredicates.GET("/functional-response-header/single-handler"), handler::useHandler);
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.reactive.responseheaders;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ResponseHeaderLiveTest {
private static final String BASE_URL = "http://localhost:8080";
private static final String ANNOTATION_BASE_URL = BASE_URL + "/response-header";
private static final String FUNCTIONAL_BASE_URL = BASE_URL + "/functional-response-header";
private static final String SERVICE_SINGLE_RESPONSE_HEADER = "Baeldung-Example-Header";
private static final String SERVICE_FILTER_RESPONSE_HEADER = "Baeldung-Example-Filter-Header";
private static final String SERVICE_FILTER_RESPONSE_HEADER_VALUE = "Value-Filter";
private static WebTestClient client;
@BeforeAll
public static void setup() {
client = WebTestClient.bindToServer()
.baseUrl(BASE_URL)
.build();
}
@Test
public void whenUsingResponseEntityBuilderRequest_thenObtainResponseWithCorrectHeaders() {
client = WebTestClient.bindToServer()
.baseUrl(BASE_URL)
.build();
ResponseSpec response = client.get()
.uri(ANNOTATION_BASE_URL + "/response-entity")
.exchange();
response.expectHeader().valueEquals(SERVICE_SINGLE_RESPONSE_HEADER, "Value-ResponseEntityBuilder")
.expectHeader().valueEquals(SERVICE_FILTER_RESPONSE_HEADER, SERVICE_FILTER_RESPONSE_HEADER_VALUE);
}
@Test
public void whenUsingServerHttpResponseRequest_thenObtainResponseWithCorrectHeaders() {
ResponseSpec response = client.get()
.uri(ANNOTATION_BASE_URL + "/server-http-response")
.exchange();
response.expectHeader().valueEquals(SERVICE_SINGLE_RESPONSE_HEADER, "Value-ServerHttpResponse")
.expectHeader().valueEquals(SERVICE_FILTER_RESPONSE_HEADER, SERVICE_FILTER_RESPONSE_HEADER_VALUE);
}
@Test
public void whenUsingFunctionalHandlerRequest_thenObtainResponseWithCorrectHeaders() {
ResponseSpec response = client.get()
.uri(FUNCTIONAL_BASE_URL + "/single-handler")
.exchange();
response.expectHeader().valueEquals(SERVICE_SINGLE_RESPONSE_HEADER, "Value-Handler")
.expectHeader().valueEquals(SERVICE_FILTER_RESPONSE_HEADER, SERVICE_FILTER_RESPONSE_HEADER_VALUE);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.responseheaders;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class ResponseHeadersApplication {
public static void main(String[] args) {
SpringApplication.run(ResponseHeadersApplication.class, args);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.responseheaders.controllers;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/filter-response-header")
public class FilterResponseHeaderController {
@GetMapping("/no-extra-header")
public String FilterHeaderResponseWithNoExtraHeader() {
return "Response body with Filter header and no extra header";
}
@GetMapping("/extra-header")
public String FilterHeaderResponseWithExtraHeader(HttpServletResponse response) {
response.addHeader("Baeldung-Example-Header", "Value-ExtraHeader");
return "Response body with Filter header and one extra header";
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.responseheaders.controllers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/single-response-header")
public class ResponseHeaderController {
@GetMapping("/http-servlet-response")
public String usingHttpServletResponse(HttpServletResponse response) {
response.addHeader("Baeldung-Example-Header", "Value-HttpServletResponse");
return "Response with header using HttpServletResponse";
}
@GetMapping("/response-entity-constructor")
public ResponseEntity<String> usingResponseEntityConstructor() {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Baeldung-Example-Header", "Value-ResponseEntityContructor");
String responseBody = "Response with header using ResponseEntity (constructor)";
HttpStatus responseStatus = HttpStatus.OK;
return new ResponseEntity<String>(responseBody, responseHeaders, responseStatus);
}
@GetMapping("/response-entity-contructor-multiple-headers")
public ResponseEntity<String> usingResponseEntityConstructorAndMultipleHeaders() {
List<MediaType> acceptableMediaTypes = new ArrayList<>(Arrays.asList(MediaType.APPLICATION_JSON));
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Baeldung-Example-Header", "Value-ResponseEntityConstructorAndHeaders");
responseHeaders.setAccept(acceptableMediaTypes);
String responseBody = "Response with header using ResponseEntity (constructor)";
HttpStatus responseStatus = HttpStatus.OK;
return new ResponseEntity<String>(responseBody, responseHeaders, responseStatus);
}
@GetMapping("/response-entity-builder")
public ResponseEntity<String> usingResponseEntityBuilder() {
String responseHeaderKey = "Baeldung-Example-Header";
String responseHeaderValue = "Value-ResponseEntityBuilder";
String responseBody = "Response with header using ResponseEntity (builder)";
return ResponseEntity.ok()
.header(responseHeaderKey, responseHeaderValue)
.body(responseBody);
}
@GetMapping("/response-entity-builder-with-http-headers")
public ResponseEntity<String> usingResponseEntityBuilderAndHttpHeaders() {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Baeldung-Example-Header", "Value-ResponseEntityBuilderWithHttpHeaders");
String responseBody = "Response with header using ResponseEntity (builder)";
return ResponseEntity.ok()
.headers(responseHeaders)
.body(responseBody);
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.responseheaders.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebFilter("/filter-response-header/*")
public class AddResponseHeaderFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(AddResponseHeaderFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// add special initialization requirements here
LOGGER.trace("Initializing filter...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.setHeader("Baeldung-Example-Filter-Header", "Value-Filter");
chain.doFilter(request, response);
}
@Override
public void destroy() {
// clean up any resource being held by the filter here
LOGGER.trace("Destroying filter...");
}
}

View File

@ -0,0 +1,103 @@
package com.baeldung.responseheaders;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ResponseHeaderLiveTest {
private static final String BASE_URL = "http://localhost:8082/spring-rest";
private static final String SINGLE_BASE_URL = BASE_URL + "/single-response-header";
private static final String FILTER_BASE_URL = BASE_URL + "/filter-response-header";
private static final String SERVICE_SINGLE_RESPONSE_HEADER = "Baeldung-Example-Header";
private static final String SERVICE_FILTER_RESPONSE_HEADER = "Baeldung-Example-Filter-Header";
@Autowired
private TestRestTemplate template;
@Test
public void whenHttpServletResponseRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/http-servlet-response";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-HttpServletResponse"));
}
@Test
public void whenResponseEntityConstructorRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/response-entity-constructor";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityContructor"));
}
@Test
public void whenResponseEntityConstructorAndMultipleHeadersRequest_thenObtainResponseWithCorrectHeaders() {
final String requestUrl = "/response-entity-contructor-multiple-headers";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityConstructorAndHeaders"));
assertThat(responseHeaders).containsEntry("Accept", Arrays.asList(MediaType.APPLICATION_JSON.toString()));
}
@Test
public void whenResponseEntityBuilderRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/response-entity-builder";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityBuilder"));
}
@Test
public void whenResponseEntityBuilderAndHttpHeadersRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/response-entity-builder-with-http-headers";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityBuilderWithHttpHeaders"));
}
@Test
public void whenFilterWithNoExtraHeaderRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/no-extra-header";
ResponseEntity<String> response = template.getForEntity(FILTER_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_FILTER_RESPONSE_HEADER, Arrays.asList("Value-Filter"));
}
@Test
public void whenFilterWithExtraHeaderRequest_thenObtainResponseWithCorrectHeaders() {
final String requestUrl = "/extra-header";
ResponseEntity<String> response = template.getForEntity(FILTER_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_FILTER_RESPONSE_HEADER, Arrays.asList("Value-Filter"));
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ExtraHeader"));
}
}