Java Code Examples for org.springframework.cloud.gateway.support.ServerWebExchangeUtils#expand()

The following examples show how to use org.springframework.cloud.gateway.support.ServerWebExchangeUtils#expand() . 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: AddResponseHeaderGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Override
public GatewayFilter apply(NameValueConfig config) {
	return new GatewayFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange,
				GatewayFilterChain chain) {
			String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
			exchange.getResponse().getHeaders().add(config.getName(), value);

			return chain.filter(exchange);
		}

		@Override
		public String toString() {
			return filterToStringCreator(AddResponseHeaderGatewayFilterFactory.this)
					.append(config.getName(), config.getValue()).toString();
		}
	};
}
 
Example 2
Source File: SetRequestHeaderGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Override
public GatewayFilter apply(NameValueConfig config) {
	return new GatewayFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange,
				GatewayFilterChain chain) {
			String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
			ServerHttpRequest request = exchange.getRequest().mutate()
					.headers(httpHeaders -> httpHeaders.set(config.name, value))
					.build();

			return chain.filter(exchange.mutate().request(request).build());
		}

		@Override
		public String toString() {
			return filterToStringCreator(SetRequestHeaderGatewayFilterFactory.this)
					.append(config.getName(), config.getValue()).toString();
		}
	};
}
 
Example 3
Source File: AddRequestHeaderGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Override
public GatewayFilter apply(NameValueConfig config) {
	return new GatewayFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange,
				GatewayFilterChain chain) {
			String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
			ServerHttpRequest request = exchange.getRequest().mutate()
					.header(config.getName(), value).build();

			return chain.filter(exchange.mutate().request(request).build());
		}

		@Override
		public String toString() {
			return filterToStringCreator(AddRequestHeaderGatewayFilterFactory.this)
					.append(config.getName(), config.getValue()).toString();
		}
	};
}
 
Example 4
Source File: SetResponseHeaderGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Override
public GatewayFilter apply(NameValueConfig config) {
	return new GatewayFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange,
				GatewayFilterChain chain) {
			String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
			return chain.filter(exchange).then(Mono.fromRunnable(() -> exchange
					.getResponse().getHeaders().set(config.name, value)));
		}

		@Override
		public String toString() {
			return filterToStringCreator(SetResponseHeaderGatewayFilterFactory.this)
					.append(config.getName(), config.getValue()).toString();
		}
	};
}
 
Example 5
Source File: SetRequestHostHeaderGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public GatewayFilter apply(Config config) {
	return new GatewayFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange,
				GatewayFilterChain chain) {
			String value = ServerWebExchangeUtils.expand(exchange, config.getHost());

			ServerHttpRequest request = exchange.getRequest().mutate()
					.headers(httpHeaders -> {
						httpHeaders.remove("Host");
						httpHeaders.add("Host", value);
					}).build();

			// Make sure the header we just set is preserved
			exchange.getAttributes().put(PRESERVE_HOST_HEADER_ATTRIBUTE, true);

			return chain.filter(exchange.mutate().request(request).build());
		}

		@Override
		public String toString() {
			return filterToStringCreator(
					SetRequestHostHeaderGatewayFilterFactory.this)
							.append(config.getHost()).toString();
		}
	};
}
 
Example 6
Source File: AddRequestParameterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
public GatewayFilter apply(NameValueConfig config) {
	return new GatewayFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange,
				GatewayFilterChain chain) {
			URI uri = exchange.getRequest().getURI();
			StringBuilder query = new StringBuilder();
			String originalQuery = uri.getRawQuery();

			if (StringUtils.hasText(originalQuery)) {
				query.append(originalQuery);
				if (originalQuery.charAt(originalQuery.length() - 1) != '&') {
					query.append('&');
				}
			}

			String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
			// TODO urlencode?
			query.append(config.getName());
			query.append('=');
			query.append(value);

			try {
				URI newUri = UriComponentsBuilder.fromUri(uri)
						.replaceQuery(query.toString()).build(true).toUri();

				ServerHttpRequest request = exchange.getRequest().mutate().uri(newUri)
						.build();

				return chain.filter(exchange.mutate().request(request).build());
			}
			catch (RuntimeException ex) {
				throw new IllegalStateException(
						"Invalid URI query: \"" + query.toString() + "\"");
			}
		}

		@Override
		public String toString() {
			return filterToStringCreator(AddRequestParameterGatewayFilterFactory.this)
					.append(config.getName(), config.getValue()).toString();
		}
	};
}