org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter Java Examples

The following examples show how to use org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TraceRequestHttpHeadersFilterTests.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Test
public void should_set_tracing_headers_with_multiple_values() {
	HttpHeadersFilter filter = TraceRequestHttpHeadersFilter.create(this.httpTracing);
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.add("X-Hello-Request", "Request World");
	httpHeaders.addAll("X-Hello", Arrays.asList("World1", "World2"));
	MockServerHttpRequest request = MockServerHttpRequest.post("foo/bar")
			.headers(httpHeaders).build();
	MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

	HttpHeaders filteredHeaders = filter.filter(httpHeaders, exchange);

	BDDAssertions.then(filteredHeaders.get("X-B3-TraceId")).isNotEmpty();
	BDDAssertions.then(filteredHeaders.get("X-B3-SpanId")).isNotEmpty();
	BDDAssertions.then(filteredHeaders.get("X-Hello"))
			.isEqualTo(Arrays.asList("World1", "World2"));
	BDDAssertions.then(filteredHeaders.get("X-Hello-Request"))
			.isEqualTo(Collections.singletonList("Request World"));
	BDDAssertions
			.then((Object) exchange
					.getAttribute(TraceRequestHttpHeadersFilter.SPAN_ATTRIBUTE))
			.isNotNull();
}
 
Example #2
Source File: TraceRequestHttpHeadersFilterTests.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Test
public void should_reuse_headers_only_from_input_since_exchange_may_contain_already_ignored_headers() {
	HttpHeadersFilter filter = TraceRequestHttpHeadersFilter.create(this.httpTracing);
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.set("X-Hello", "World");
	MockServerHttpRequest request = MockServerHttpRequest.post("foo/bar")
			.headers(httpHeaders).build();
	MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

	HttpHeaders filteredHeaders = filter.filter(requestHeaders(), exchange);

	BDDAssertions.then(filteredHeaders.get("X-B3-TraceId")).isNotEmpty();
	BDDAssertions.then(filteredHeaders.get("X-B3-SpanId")).isNotEmpty();
	BDDAssertions.then(filteredHeaders.get("X-Hello")).isNullOrEmpty();
	BDDAssertions
			.then((Object) exchange
					.getAttribute(TraceRequestHttpHeadersFilter.SPAN_ATTRIBUTE))
			.isNotNull();
}
 
Example #3
Source File: TraceRequestHttpHeadersFilterTests.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Test
public void should_set_tracing_headers() {
	HttpHeadersFilter filter = TraceRequestHttpHeadersFilter.create(this.httpTracing);
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.set("X-Hello", "World");
	MockServerHttpRequest request = MockServerHttpRequest.post("foo/bar")
			.headers(httpHeaders).build();
	MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

	HttpHeaders filteredHeaders = filter.filter(requestHeaders(httpHeaders),
			exchange);

	BDDAssertions.then(filteredHeaders.get("X-B3-TraceId")).isNotEmpty();
	BDDAssertions.then(filteredHeaders.get("X-B3-SpanId")).isNotEmpty();
	BDDAssertions.then(filteredHeaders.get("X-Hello"))
			.isEqualTo(Collections.singletonList("World"));
	BDDAssertions.then(filteredHeaders.get("X-Hello-Request"))
			.isEqualTo(Collections.singletonList("Request World"));
	BDDAssertions
			.then((Object) exchange
					.getAttribute(TraceRequestHttpHeadersFilter.SPAN_ATTRIBUTE))
			.isNotNull();
}
 
Example #4
Source File: WebsocketRoutingFilter.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
private List<HttpHeadersFilter> getHeadersFilters() {
	if (this.headersFilters == null) {
		this.headersFilters = this.headersFiltersProvider
				.getIfAvailable(ArrayList::new);

		headersFilters.add((headers, exchange) -> {
			HttpHeaders filtered = new HttpHeaders();
			headers.entrySet().stream()
					.filter(entry -> !entry.getKey().toLowerCase()
							.startsWith("sec-websocket"))
					.forEach(header -> filtered.addAll(header.getKey(),
							header.getValue()));
			return filtered;
		});
	}

	return this.headersFilters;
}
 
Example #5
Source File: TraceResponseHttpHeadersFilterTests.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Test
public void should_report_span_when_span_was_present_in_attribute() {
	HttpHeadersFilter filter = TraceResponseHttpHeadersFilter
			.create(this.httpTracing);
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.set("b3", "52f112af7472aff0-53e6ab6fc5dfee58");
	MockServerHttpRequest request = MockServerHttpRequest.post("foo/bar")
			.headers(httpHeaders).build();
	MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();
	exchange.getAttributes().put(TraceResponseHttpHeadersFilter.SPAN_ATTRIBUTE,
			this.tracing.tracer().nextSpan());

	filter.filter(httpHeaders, exchange);

	BDDAssertions.then(this.spans).isNotEmpty();
}
 
Example #6
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Bean
public WebsocketRoutingFilter websocketRoutingFilter(WebSocketClient webSocketClient,
		WebSocketService webSocketService,
		ObjectProvider<List<HttpHeadersFilter>> headersFilters) {
	return new WebsocketRoutingFilter(webSocketClient, webSocketService,
			headersFilters);
}
 
Example #7
Source File: NettyRoutingFilter.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
public NettyRoutingFilter(HttpClient httpClient,
		ObjectProvider<List<HttpHeadersFilter>> headersFiltersProvider,
		HttpClientProperties properties) {
	this.httpClient = httpClient;
	this.headersFiltersProvider = headersFiltersProvider;
	this.properties = properties;
}
 
Example #8
Source File: WebsocketRoutingFilter.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
public WebsocketRoutingFilter(WebSocketClient webSocketClient,
		WebSocketService webSocketService,
		ObjectProvider<List<HttpHeadersFilter>> headersFiltersProvider) {
	this.webSocketClient = webSocketClient;
	this.webSocketService = webSocketService;
	this.headersFiltersProvider = headersFiltersProvider;
}
 
Example #9
Source File: TraceRequestHttpHeadersFilterTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void should_override_span_tracing_headers_when_using_b3() {
	HttpHeadersFilter filter = TraceRequestHttpHeadersFilter.create(this.httpTracing);
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.set("X-Hello", "World");
	httpHeaders.set("B3", "1111111111111111-1111111111111111");
	MockServerHttpRequest request = MockServerHttpRequest.post("foo/bar")
			.headers(httpHeaders).build();
	MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

	HttpHeaders filteredHeaders = filter.filter(requestHeaders(httpHeaders),
			exchange);

	// we want to continue the trace
	BDDAssertions.then(filteredHeaders.get("X-B3-TraceId"))
			.isEqualTo(Collections.singletonList("1111111111111111"));
	// but we want to have a new span id
	BDDAssertions.then(filteredHeaders.get("X-B3-SpanId"))
			.isNotEqualTo(Collections.singletonList("1111111111111111"));
	// we don't want to propagate b3
	BDDAssertions.then(filteredHeaders.get("B3")).isNullOrEmpty();
	BDDAssertions.then(filteredHeaders.get("X-Hello"))
			.isEqualTo(Collections.singletonList("World"));
	BDDAssertions.then(filteredHeaders.get("X-Hello-Request"))
			.isEqualTo(Collections.singletonList("Request World"));
	BDDAssertions.then(filteredHeaders.get("X-Auth-User")).hasSize(1);
	BDDAssertions
			.then((Object) exchange
					.getAttribute(TraceRequestHttpHeadersFilter.SPAN_ATTRIBUTE))
			.isNotNull();
}
 
Example #10
Source File: TraceRequestHttpHeadersFilterTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void should_override_span_tracing_headers() {
	HttpHeadersFilter filter = TraceRequestHttpHeadersFilter.create(this.httpTracing);
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.set("X-Hello", "World");
	httpHeaders.set("X-B3-TraceId", "52f112af7472aff0");
	httpHeaders.set("X-B3-SpanId", "53e6ab6fc5dfee58");
	MockServerHttpRequest request = MockServerHttpRequest.post("foo/bar")
			.headers(httpHeaders).build();
	MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

	HttpHeaders filteredHeaders = filter.filter(requestHeaders(httpHeaders),
			exchange);

	// we want to continue the trace
	BDDAssertions.then(filteredHeaders.get("X-B3-TraceId"))
			.isEqualTo(httpHeaders.get("X-B3-TraceId"));
	// but we want to have a new span id
	BDDAssertions.then(filteredHeaders.get("X-B3-SpanId"))
			.isNotEqualTo(httpHeaders.get("X-B3-SpanId"));
	BDDAssertions.then(filteredHeaders.get("X-Hello"))
			.isEqualTo(Collections.singletonList("World"));
	BDDAssertions.then(filteredHeaders.get("X-Hello-Request"))
			.isEqualTo(Collections.singletonList("Request World"));
	BDDAssertions.then(filteredHeaders.get("X-Auth-User")).hasSize(1);
	BDDAssertions
			.then((Object) exchange
					.getAttribute(TraceRequestHttpHeadersFilter.SPAN_ATTRIBUTE))
			.isNotNull();
}
 
Example #11
Source File: TraceResponseHttpHeadersFilterTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void should_not_report_span_when_no_span_was_present_in_attribute() {
	HttpHeadersFilter filter = TraceResponseHttpHeadersFilter
			.create(this.httpTracing);
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.set("b3", "52f112af7472aff0-53e6ab6fc5dfee58");
	MockServerHttpRequest request = MockServerHttpRequest.post("foo/bar")
			.headers(httpHeaders).build();
	MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

	filter.filter(httpHeaders, exchange);

	BDDAssertions.then(this.spans).isEmpty();
}
 
Example #12
Source File: TraceRequestHttpHeadersFilter.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
static HttpHeadersFilter create(HttpTracing httpTracing) {
	return new TraceRequestHttpHeadersFilter(httpTracing);
}
 
Example #13
Source File: TraceWebClientAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Bean
HttpHeadersFilter traceResponseHttpHeadersFilter(HttpTracing httpTracing) {
	return TraceResponseHttpHeadersFilter.create(httpTracing);
}
 
Example #14
Source File: TraceWebClientAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Bean
HttpHeadersFilter traceRequestHttpHeadersFilter(HttpTracing httpTracing) {
	return TraceRequestHttpHeadersFilter.create(httpTracing);
}
 
Example #15
Source File: TraceRequestHttpHeadersFilter.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
static HttpHeadersFilter create(HttpTracing httpTracing) {
	return new TraceResponseHttpHeadersFilter(httpTracing);
}
 
Example #16
Source File: GatewayTracingAutoConfiguration.java    From java-spring-cloud with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnClass(HttpHeadersFilter.class)
@ConditionalOnBean(Tracer.class)
HttpHeadersFilter traceRequestHttpHeadersFilter(Tracer tracer) {
  return new TraceRequestHttpHeadersFilter(tracer);
}
 
Example #17
Source File: WebClientHttpRoutingFilter.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public List<HttpHeadersFilter> getHeadersFilters() {
	if (headersFilters == null) {
		headersFilters = headersFiltersProvider.getIfAvailable();
	}
	return headersFilters;
}
 
Example #18
Source File: WebClientHttpRoutingFilter.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public WebClientHttpRoutingFilter(WebClient webClient,
		ObjectProvider<List<HttpHeadersFilter>> headersFiltersProvider) {
	this.webClient = webClient;
	this.headersFiltersProvider = headersFiltersProvider;
}
 
Example #19
Source File: NettyRoutingFilter.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public List<HttpHeadersFilter> getHeadersFilters() {
	if (headersFilters == null) {
		headersFilters = headersFiltersProvider.getIfAvailable();
	}
	return headersFilters;
}
 
Example #20
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean
public NettyRoutingFilter routingFilter(HttpClient httpClient,
		ObjectProvider<List<HttpHeadersFilter>> headersFilters,
		HttpClientProperties properties) {
	return new NettyRoutingFilter(httpClient, headersFilters, properties);
}
 
Example #21
Source File: GatewayTracingAutoConfiguration.java    From java-spring-cloud with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnClass(HttpHeadersFilter.class)
@ConditionalOnBean(Tracer.class)
HttpHeadersFilter traceResponseHttpHeadersFilter(Tracer tracer) {
  return new TraceResponseHttpHeadersFilter(tracer);
}