com.spring.boxes.gateway.routes.GatewayDefinition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boxes-gateway-starter Show documentation
Show all versions of spring-boxes-gateway-starter Show documentation
spring-cookie-boxes-boot-starter
The newest version!
package com.spring.boxes.gateway.routes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AddRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.support.NameUtils;
import java.util.List;
import static org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory.REGEXP_KEY;
import static org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory.REPLACEMENT_KEY;
import static org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory.PARTS_KEY;
import static org.springframework.cloud.gateway.handler.predicate.RoutePredicateFactory.PATTERN_KEY;
import static org.springframework.cloud.gateway.support.NameUtils.normalizeFilterFactoryName;
import static org.springframework.cloud.gateway.support.NameUtils.normalizeRoutePredicateName;
public class GatewayDefinition {
public static PredicateDefinition path(List pathList) {
PredicateDefinition predicate = new PredicateDefinition();
predicate.setName(normalizeRoutePredicateName(PathRoutePredicateFactory.class));
for (int i = 0; i < pathList.size(); i++) {
predicate.addArg(NameUtils.generateName(i), pathList.get(i));
}
return predicate;
}
public static PredicateDefinition path(String path) {
PredicateDefinition predicate = new PredicateDefinition();
predicate.setName(normalizeRoutePredicateName(PathRoutePredicateFactory.class));
predicate.addArg(PATTERN_KEY, StringUtils.trimToEmpty(path));
return predicate;
}
public static FilterDefinition rewrite(String regex, String replacement) {
FilterDefinition filter = new FilterDefinition();
filter.setName(normalizeFilterFactoryName(RewritePathGatewayFilterFactory.class));
filter.addArg(REGEXP_KEY, regex);
filter.addArg(REPLACEMENT_KEY, replacement);
return filter;
}
public static FilterDefinition pathTo(String origin, String target){
String originPattern = origin + "(?.*)";
String targetPattern = target + "${segment}";
return rewrite(originPattern, targetPattern);
}
public static FilterDefinition stripPrefix(String parts) {
FilterDefinition filter = new FilterDefinition();
filter.setName(normalizeFilterFactoryName(StripPrefixGatewayFilterFactory.class));
filter.addArg(PARTS_KEY, parts);
return filter;
}
public static FilterDefinition addRequestHeader(String name, String value) {
FilterDefinition filter = new FilterDefinition();
filter.setName(normalizeFilterFactoryName(AddRequestHeaderGatewayFilterFactory.class));
filter.addArg(GatewayFilter.NAME_KEY, name);
filter.addArg(GatewayFilter.VALUE_KEY, value);
return filter;
}
public static FilterDefinition requestBodyRewriteFilter() {
FilterDefinition filter = new FilterDefinition();
filter.setName(normalizeFilterFactoryName(ModifyRequestBodyGatewayFilterFactory.class));
filter.addArg("inClass", "#{T(java.lang.String)}");
filter.addArg("outClass", "#{T(java.lang.String)}");
filter.addArg("rewriteFunction", "#{@requestRewriteFunction}");
return filter;
}
public static FilterDefinition responseBodyRewriteFilter() {
FilterDefinition filter = new FilterDefinition();
filter.setName(normalizeFilterFactoryName(ModifyResponseBodyGatewayFilterFactory.class));
filter.addArg("inClass", "#{T(java.lang.String)}");
filter.addArg("outClass", "#{T(java.lang.String)}");
filter.addArg("rewriteFunction", "#{@responseRewriteFunction}");
return filter;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy