Java Code Examples for org.springframework.cloud.gateway.route.Route#AsyncBuilder

The following examples show how to use org.springframework.cloud.gateway.route.Route#AsyncBuilder . 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: GatewayFilterSpecTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetModifyBodyResponseFilterWithRewriteFunction() {
	ConfigurableApplicationContext context = mock(
			ConfigurableApplicationContext.class);
	Route.AsyncBuilder routeBuilder = Route.async().id("123").uri("abc:123")
			.predicate(exchange -> true);

	when(context.getBean(ModifyResponseBodyGatewayFilterFactory.class))
			.thenReturn(new ModifyResponseBodyGatewayFilterFactory(
					HandlerStrategies.withDefaults().messageReaders(),
					Collections.emptySet(), Collections.emptySet()));

	RouteLocatorBuilder.Builder routes = new RouteLocatorBuilder(context).routes();
	GatewayFilterSpec spec = new GatewayFilterSpec(routeBuilder, routes);
	spec.modifyResponseBody(String.class, String.class,
			(exchange, s) -> Mono.just(s));

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

	assertFilter(route.getFilters().get(0),
			ModifyResponseBodyGatewayFilterFactory.ModifyResponseGatewayFilter.class,
			NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1);
}
 
Example 3
Source File: GatewayFilterSpecTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetModifyBodyResponseFilterWithRewriteFunctionAndEmptyBodySupplier() {
	ConfigurableApplicationContext context = mock(
			ConfigurableApplicationContext.class);
	Route.AsyncBuilder routeBuilder = Route.async().id("123").uri("abc:123")
			.predicate(exchange -> true);

	when(context.getBean(ModifyResponseBodyGatewayFilterFactory.class))
			.thenReturn(new ModifyResponseBodyGatewayFilterFactory(
					HandlerStrategies.withDefaults().messageReaders(),
					Collections.emptySet(), Collections.emptySet()));

	RouteLocatorBuilder.Builder routes = new RouteLocatorBuilder(context).routes();
	GatewayFilterSpec spec = new GatewayFilterSpec(routeBuilder, routes);
	spec.modifyResponseBody(String.class, String.class,
			(exchange, s) -> Mono.just(s == null ? "emptybody" : s));

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

	assertFilter(route.getFilters().get(0),
			ModifyResponseBodyGatewayFilterFactory.ModifyResponseGatewayFilter.class,
			NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1);
}
 
Example 4
Source File: GatewayFilterSpecTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetModifyBodyResponseFilterWithRewriteFunctionAndNewContentType() {
	ConfigurableApplicationContext context = mock(
			ConfigurableApplicationContext.class);
	Route.AsyncBuilder routeBuilder = Route.async().id("123").uri("abc:123")
			.predicate(exchange -> true);

	when(context.getBean(ModifyResponseBodyGatewayFilterFactory.class))
			.thenReturn(new ModifyResponseBodyGatewayFilterFactory(
					HandlerStrategies.withDefaults().messageReaders(),
					Collections.emptySet(), Collections.emptySet()));

	RouteLocatorBuilder.Builder routes = new RouteLocatorBuilder(context).routes();
	GatewayFilterSpec spec = new GatewayFilterSpec(routeBuilder, routes);
	spec.modifyResponseBody(String.class, String.class,
			MediaType.APPLICATION_JSON_VALUE, (exchange, s) -> Mono.just(s));

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

	assertFilter(route.getFilters().get(0),
			ModifyResponseBodyGatewayFilterFactory.ModifyResponseGatewayFilter.class,
			NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1);
}
 
Example 5
Source File: GatewayFilterSpecTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetModifyBodyResponseFilterWithConfigConsumer() {
	ConfigurableApplicationContext context = mock(
			ConfigurableApplicationContext.class);
	Route.AsyncBuilder routeBuilder = Route.async().id("123").uri("abc:123")
			.predicate(exchange -> true);

	when(context.getBean(ModifyResponseBodyGatewayFilterFactory.class))
			.thenReturn(new ModifyResponseBodyGatewayFilterFactory(
					HandlerStrategies.withDefaults().messageReaders(),
					Collections.emptySet(), Collections.emptySet()));

	RouteLocatorBuilder.Builder routes = new RouteLocatorBuilder(context).routes();
	GatewayFilterSpec spec = new GatewayFilterSpec(routeBuilder, routes);
	spec.modifyResponseBody(
			(smth) -> new ModifyResponseBodyGatewayFilterFactory.Config()
					.setRewriteFunction(String.class, String.class,
							(exchange, s) -> Mono.just(s)));

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

	assertFilter(route.getFilters().get(0),
			ModifyResponseBodyGatewayFilterFactory.ModifyResponseGatewayFilter.class,
			NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1);
}
 
Example 6
Source File: RouteProvider.java    From syncope with Apache License 2.0 6 votes vote down vote up
public List<Route.AsyncBuilder> fetch() {
    synchronized (this) {
        if (client == null) {
            try {
                client = new SyncopeClientFactoryBean().
                        setAddress(serviceOps.get(NetworkService.Type.CORE).getAddress()).
                        setUseCompression(useGZIPCompression).
                        create(new AnonymousAuthenticationHandler(anonymousUser, anonymousKey));
            } catch (Exception e) {
                LOG.error("Could not init SyncopeClient", e);
                return List.of();
            }
        }
    }

    synchronized (routeTOs) {
        routeTOs.clear();
        routeTOs.addAll(client.getService(SRARouteService.class).list());
    }

    return routeTOs.stream().map(this::toRoute).collect(Collectors.toList());
}
 
Example 7
Source File: GatewayFilterSpecTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private void testFilter(Class<? extends GatewayFilter> type,
		GatewayFilter gatewayFilter, int order) {
	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.filter(gatewayFilter);

	Route route = routeBuilder.build();
	assertThat(route.getFilters()).hasSize(1);
	assertFilter(route.getFilters().get(0), type, order);
}
 
Example 8
Source File: UriSpec.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
UriSpec(Route.AsyncBuilder routeBuilder, RouteLocatorBuilder.Builder builder) {
	this.routeBuilder = routeBuilder;
	this.builder = builder;
}
 
Example 9
Source File: UriSpec.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public UriSpec customize(Consumer<Route.AsyncBuilder> routeConsumer) {
	routeConsumer.accept(this.routeBuilder);
	return this;
}
 
Example 10
Source File: BooleanSpec.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public BooleanSpec(Route.AsyncBuilder routeBuilder,
		RouteLocatorBuilder.Builder builder) {
	super(routeBuilder, builder);
	// save current predicate useful in kotlin dsl
	predicate = routeBuilder.getPredicate();
}
 
Example 11
Source File: BooleanSpec.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
BooleanOpSpec(Route.AsyncBuilder routeBuilder,
		RouteLocatorBuilder.Builder builder, Operator operator) {
	super(routeBuilder, builder);
	Assert.notNull(operator, "operator may not be null");
	this.operator = operator;
}
 
Example 12
Source File: PredicateSpec.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
PredicateSpec(Route.AsyncBuilder routeBuilder, RouteLocatorBuilder.Builder builder) {
	super(routeBuilder, builder);
}
 
Example 13
Source File: GatewayFilterSpec.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public GatewayFilterSpec(Route.AsyncBuilder routeBuilder,
		RouteLocatorBuilder.Builder builder) {
	super(routeBuilder, builder);
}