org.springframework.cloud.gateway.filter.OrderedGatewayFilter Java Examples

The following examples show how to use org.springframework.cloud.gateway.filter.OrderedGatewayFilter. 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: GatewayFilterSpecTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilters() {
	ConfigurableApplicationContext context = mock(
			ConfigurableApplicationContext.class);
	Route.AsyncBuilder routeBuilder = Route.async().id("123").uri("abc:123")
			.predicate(exchange -> true);
	RouteLocatorBuilder.Builder routes = new RouteLocatorBuilder(context).routes();
	GatewayFilterSpec spec = new GatewayFilterSpec(routeBuilder, routes);
	spec.filters(new MyUnorderedFilter(), new MyOrderedFilter());

	Route route = routeBuilder.build();
	assertThat(route.getFilters()).hasSize(2);

	assertFilter(route.getFilters().get(0), OrderedGatewayFilter.class, 0);
	assertFilter(route.getFilters().get(1), MyOrderedFilter.class, 1000);
}
 
Example #2
Source File: FilteringWebHandler.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private static List<GatewayFilter> loadFilters(List<GlobalFilter> filters) {
	return filters.stream().map(filter -> {
		GatewayFilterAdapter gatewayFilter = new GatewayFilterAdapter(filter);
		if (filter instanceof Ordered) {
			int order = ((Ordered) filter).getOrder();
			return new OrderedGatewayFilter(gatewayFilter, order);
		}
		return gatewayFilter;
	}).collect(Collectors.toList());
}
 
Example #3
Source File: AbstractChangeRequestUriGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
public GatewayFilter apply(T config) {
	return new OrderedGatewayFilter((exchange, chain) -> {
		Optional<URI> uri = this.determineRequestUri(exchange, config);
		uri.ifPresent(u -> {
			Map<String, Object> attributes = exchange.getAttributes();
			attributes.put(GATEWAY_REQUEST_URL_ATTR, u);
		});
		return chain.filter(exchange);
	}, this.order);
}
 
Example #4
Source File: RequestHeaderToRequestUriGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public GatewayFilter apply(NameConfig config) {
	// AbstractChangeRequestUriGatewayFilterFactory.apply() returns
	// OrderedGatewayFilter
	OrderedGatewayFilter gatewayFilter = (OrderedGatewayFilter) super.apply(config);
	return new OrderedGatewayFilter(gatewayFilter, gatewayFilter.getOrder()) {
		@Override
		public String toString() {
			return filterToStringCreator(
					RequestHeaderToRequestUriGatewayFilterFactory.this)
							.append("name", config.getName()).toString();
		}
	};
}
 
Example #5
Source File: GatewayFilterSpec.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
/**
 * Applies the filter to the route.
 * @param gatewayFilter the filter to apply
 * @param order the order to apply the filter
 * @return a {@link GatewayFilterSpec} that can be used to apply additional filters
 */
public GatewayFilterSpec filter(GatewayFilter gatewayFilter, int order) {
	if (gatewayFilter instanceof Ordered) {
		this.routeBuilder.filter(gatewayFilter);
		log.warn("GatewayFilter already implements ordered "
				+ gatewayFilter.getClass() + "ignoring order parameter: " + order);
		return this;
	}
	this.routeBuilder.filter(new OrderedGatewayFilter(gatewayFilter, order));
	return this;
}
 
Example #6
Source File: GatewayFilterSpec.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
public List<GatewayFilter> transformToOrderedFilters(Stream<GatewayFilter> stream) {
	return stream.map(filter -> {
		if (filter instanceof Ordered) {
			return filter;
		}
		else {
			return new OrderedGatewayFilter(filter, 0);
		}
	}).collect(Collectors.toList());
}
 
Example #7
Source File: RouteDefinitionRouteLocatorTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private String getFilterClassName(GatewayFilter target) {
	if (target instanceof OrderedGatewayFilter) {
		return getFilterClassName(((OrderedGatewayFilter) target).getDelegate());
	}
	else {
		String simpleName = target.getClass().getSimpleName();
		if (StringUtils.isEmpty(simpleName)) {
			// maybe a lambda using new toString methods
			simpleName = target.toString();
		}
		return simpleName;
	}
}
 
Example #8
Source File: LoggingGatewayFilterFactory.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public GatewayFilter apply(Config config) {
    return new OrderedGatewayFilter((exchange, chain) -> {
        if (config.isPreLogger())
            logger.info("Pre GatewayFilter logging: " + config.getBaseMessage());
        return chain.filter(exchange)
            .then(Mono.fromRunnable(() -> {
                if (config.isPostLogger())
                    logger.info("Post GatewayFilter logging: " + config.getBaseMessage());
            }));
    }, 1);
}
 
Example #9
Source File: RouteDefinitionRouteLocator.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
List<GatewayFilter> loadGatewayFilters(String id,
		List<FilterDefinition> filterDefinitions) {
	ArrayList<GatewayFilter> ordered = new ArrayList<>(filterDefinitions.size());
	for (int i = 0; i < filterDefinitions.size(); i++) {
		FilterDefinition definition = filterDefinitions.get(i);
		GatewayFilterFactory factory = this.gatewayFilterFactories
				.get(definition.getName());
		if (factory == null) {
			throw new IllegalArgumentException(
					"Unable to find GatewayFilterFactory with name "
							+ definition.getName());
		}
		if (logger.isDebugEnabled()) {
			logger.debug("RouteDefinition " + id + " applying filter "
					+ definition.getArgs() + " to " + definition.getName());
		}

		// @formatter:off
		Object configuration = this.configurationService.with(factory)
				.name(definition.getName())
				.properties(definition.getArgs())
				.eventFunction((bound, properties) -> new FilterArgsEvent(
						// TODO: why explicit cast needed or java compile fails
						RouteDefinitionRouteLocator.this, id, (Map<String, Object>) properties))
				.bind();
		// @formatter:on

		// some filters require routeId
		// TODO: is there a better place to apply this?
		if (configuration instanceof HasRouteId) {
			HasRouteId hasRouteId = (HasRouteId) configuration;
			hasRouteId.setRouteId(id);
		}

		GatewayFilter gatewayFilter = factory.apply(configuration);
		if (gatewayFilter instanceof Ordered) {
			ordered.add(gatewayFilter);
		}
		else {
			ordered.add(new OrderedGatewayFilter(gatewayFilter, i + 1));
		}
	}

	return ordered;
}
 
Example #10
Source File: RouteDefinitionRouteLocatorTests.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
public GatewayFilter apply(Object config) {
	return new OrderedGatewayFilter((exchange, chain) -> chain.filter(exchange),
			9999);
}
 
Example #11
Source File: GatewayFilterSpecTests.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Test
public void unorderedWithDefaultOrder() {
	testFilter(OrderedGatewayFilter.class, new MyUnorderedFilter(), 0);
}