/** * 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()); }
/** * Adds hystrix execution exception headers to fallback request. * Depends on @{code org.springframework.cloud::spring-cloud-starter-netflix-hystrix} being on the classpath, * {@see http://cloud.spring.io/spring-cloud-netflix/} * * @param configConsumer a {@link Consumer} which can be used to set up the names of the headers in the config. * If header names arguments are not provided, default values are used. * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec fallbackHeaders(Consumer<FallbackHeadersGatewayFilterFactory.Config> configConsumer) { FallbackHeadersGatewayFilterFactory factory = getFallbackHeadersGatewayFilterFactory(); return filter(factory.apply(configConsumer)); }
/** * 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))); }
/** * 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 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))); }
/** * A filter that can be used to add a prefix to the path of a request before it is routed by the Gateway. * @param prefix the prefix to add to the path * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec prefixPath(String prefix) { return filter(getBean(PrefixPathGatewayFilterFactory.class) .apply(c -> c.setPrefix(prefix))); }
/** * A filter that will remove a request header before the request is routed by the Gateway. * @param headerName the name of the header to remove * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec removeRequestHeader(String headerName) { return filter(getBean(RemoveRequestHeaderGatewayFilterFactory.class) .apply(c -> c.setName(headerName))); }
/** * 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 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))); }
/** * 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))); }