private FallbackHeadersGatewayFilterFactory getFallbackHeadersGatewayFilterFactory() { FallbackHeadersGatewayFilterFactory factory; try { factory = getBean(FallbackHeadersGatewayFilterFactory.class); } catch (NoSuchBeanDefinitionException e) { throw new NoSuchBeanDefinitionException(FallbackHeadersGatewayFilterFactory.class, "This is probably because Hystrix is missing from the classpath, which can be resolved by adding dependency on 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'"); } return factory; }
/** * A filter that will set up a request rate limiter for a route. * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public RequestRateLimiterSpec requestRateLimiter() { return new RequestRateLimiterSpec(getBean(RequestRateLimiterGatewayFilterFactory.class)); }
/** * A filter that will return a redirect response back to the client. * @param status an HTTP status code, should be a {@code 300} series redirect * @param url the URL to redirect to. This URL will be set in the {@code location} header * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec redirect(HttpStatus status, URL url) { try { return filter(getBean(RedirectToGatewayFilterFactory.class).apply(status, url.toURI())); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid URL", e); } }
/** * A filter that will retry failed requests. * @param repeat a {@link Repeat} * @param retry a {@link Retry} * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec retry(Repeat<ServerWebExchange> repeat, Retry<ServerWebExchange> retry) { return filter(getBean(RetryGatewayFilterFactory.class).apply(repeat, retry)); }
/** * A filter which forces a {@code WebSession::save} operation before forwarding the call downstream. This is of * particular use when using something like <a href="http://projects.spring.io/spring-session/">Spring Session</a> * with a lazy data store and need to ensure the session state has been saved before making the forwarded call. * If you are integrating <a href="http://projects.spring.io/spring-security/">Spring Security</a> with * Spring Session, and want to ensure security details have been forwarded to the remote process, this is critical. * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ @SuppressWarnings("unchecked") public GatewayFilterSpec saveSession() { return filter(getBean(SaveSessionGatewayFilterFactory.class).apply(c -> {})); }
/** * A filter that will return a redirect response back to the client. * @param status an HTTP status code, should be a {@code 300} series redirect * @param url the URL to redirect to. This URL will be set in the {@code location} header * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec redirect(String status, String url) { return filter(getBean(RedirectToGatewayFilterFactory.class).apply(status, url)); }
/** * A filter that will retry failed requests. * @param retryConsumer a {@link Consumer} which returns a {@link org.springframework.cloud.gateway.filter.factory.RetryGatewayFilterFactory.RetryConfig} * to configure the retry functionality * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec retry(Consumer<RetryGatewayFilterFactory.RetryConfig> retryConsumer) { return filter(getBean(RetryGatewayFilterFactory.class).apply(retryConsumer)); }
/** * A filter that will preserve the host header of the request on the outgoing request from the Gateway. * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec preserveHostHeader() { return filter(getBean(PreserveHostHeaderGatewayFilterFactory.class).apply()); }
public <C, R extends RateLimiter<C>> RequestRateLimiterSpec rateLimiter(Class<R> rateLimiterType, Consumer<C> configConsumer) { R rateLimiter = getBean(rateLimiterType); C config = rateLimiter.newConfig(); configConsumer.accept(config); rateLimiter.getConfig().put(routeBuilder.getId(), config); return this; }
/** * Strips the prefix from the path of the request before it is routed by the Gateway. * @param parts the number of parts of the path to remove * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec stripPrefix(int parts) { return filter(getBean(StripPrefixGatewayFilterFactory.class) .apply(c -> c.setParts(parts))); }
/** * A filter that will retry failed requests. * By default {@code 5xx} errors and {@code GET}s are retryable. * @param retries max number of retries * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec retry(int retries) { return filter(getBean(RetryGatewayFilterFactory.class) .apply(retryConfig -> retryConfig.setRetries(retries))); }
/** * A filter that sets the path of the request before it is routed by the Gateway. * @param template the path to set on the request, allows multiple matching segments using URI templates from * Spring Framework * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec setPath(String template) { return filter(getBean(SetPathGatewayFilterFactory.class) .apply(c -> c.setTemplate(template))); }
/** * A filter that sets the maximum permissible size of a Request. * @param size the maximum size of a request * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec setRequestSize(Long size) { return filter(getBean(RequestSizeGatewayFilterFactory.class).apply(c -> c.setMaxSize(size))); }
/** * A filter that will remove a response header before the Gateway returns the response to the client. * @param headerName the name of the header to remove * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec removeResponseHeader(String headerName) { return filter(getBean(RemoveResponseHeaderGatewayFilterFactory.class) .apply(c -> c.setName(headerName))); }
/** * A filter that sets the status on the response before it is returned to the client by the Gateway. * @param status the status to set on the response * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec setStatus(String status) { return filter(getBean(SetStatusGatewayFilterFactory.class) .apply(c -> c.setStatus(status))); }
/** * Adds a request header to the request before it is routed by the Gateway. * @param headerName the header name * @param headerValue the header value * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec addRequestHeader(String headerName, String headerValue) { return filter(getBean(AddRequestHeaderGatewayFilterFactory.class) .apply(c -> c.setName(headerName).setValue(headerValue))); }
/** * A filter that sets a header on the response before it is returned to the client by the Gateway. * @param headerName the header name * @param headerValue the value of the header * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec setResponseHeader(String headerName, String headerValue) { return filter(getBean(SetResponseHeaderGatewayFilterFactory.class) .apply(c -> c.setName(headerName).setValue(headerValue))); }
/** * Adds a header to the response returned to the Gateway from the route. * @param headerName the header name * @param headerValue the header value * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec addResponseHeader(String headerName, String headerValue) { return filter(getBean(AddResponseHeaderGatewayFilterFactory.class) .apply(c -> c.setName(headerName).setValue(headerValue))); }
/** * A filter which rewrites the request path before it is routed by the Gateway * @param regex a Java regular expression to match the path against * @param replacement the replacement for the path * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec rewritePath(String regex, String replacement) { return filter(getBean(RewritePathGatewayFilterFactory.class) .apply(c -> c.setRegexp(regex).setReplacement(replacement))); }
/** * A filter that rewrites a header value on the response before it is returned to the client by the Gateway. * @param headerName the header name * @param regex a Java regular expression to match the path against * @param replacement the replacement for the path * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec rewriteResponseHeader(String headerName, String regex, String replacement) { return filter(getBean(RewriteResponseHeaderGatewayFilterFactory.class) .apply(c -> c.setReplacement(replacement).setRegexp(regex).setName(headerName))); }