use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem in project XHuiCloud by sindaZeng.
the class ApiDefinitionEntity method toApiDefinition.
public ApiDefinition toApiDefinition() {
ApiDefinition apiDefinition = new ApiDefinition();
apiDefinition.setApiName(apiName);
Set<ApiPredicateItem> apiPredicateItems = new LinkedHashSet<>();
apiDefinition.setPredicateItems(apiPredicateItems);
if (predicateItems != null) {
for (ApiPredicateItemEntity predicateItem : predicateItems) {
ApiPathPredicateItem apiPredicateItem = new ApiPathPredicateItem();
apiPredicateItems.add(apiPredicateItem);
apiPredicateItem.setMatchStrategy(predicateItem.getMatchStrategy());
apiPredicateItem.setPattern(predicateItem.getPattern());
}
}
return apiDefinition;
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem in project XHuiCloud by sindaZeng.
the class ApiDefinitionEntity method fromApiDefinition.
public static ApiDefinitionEntity fromApiDefinition(String app, String ip, Integer port, ApiDefinition apiDefinition) {
ApiDefinitionEntity entity = new ApiDefinitionEntity();
entity.setApp(app);
entity.setIp(ip);
entity.setPort(port);
entity.setApiName(apiDefinition.getApiName());
Set<ApiPredicateItemEntity> predicateItems = new LinkedHashSet<>();
entity.setPredicateItems(predicateItems);
Set<ApiPredicateItem> apiPredicateItems = apiDefinition.getPredicateItems();
if (apiPredicateItems != null) {
for (ApiPredicateItem apiPredicateItem : apiPredicateItems) {
ApiPredicateItemEntity itemEntity = new ApiPredicateItemEntity();
predicateItems.add(itemEntity);
ApiPathPredicateItem pathPredicateItem = (ApiPathPredicateItem) apiPredicateItem;
itemEntity.setPattern(pathPredicateItem.getPattern());
itemEntity.setMatchStrategy(pathPredicateItem.getMatchStrategy());
}
}
return entity;
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem in project spring-cloud-alibaba by longlylong.
the class NacosSentinelConfig method initCustomizedApis.
private void initCustomizedApis() {
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("some_customized_api").setPredicateItems(new HashSet<ApiPredicateItem>() {
{
add(new ApiPathPredicateItem().setPattern("/ahas"));
add(new ApiPathPredicateItem().setPattern("/product/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}
});
definitions.add(api1);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem in project Sentinel by alibaba.
the class SentinelGatewayFilterTest method testPickMatchingApiDefinitions.
@Test
public void testPickMatchingApiDefinitions() {
// Mock a request.
ServerWebExchange exchange = mock(ServerWebExchange.class);
ServerHttpRequest request = mock(ServerHttpRequest.class);
when(exchange.getRequest()).thenReturn(request);
RequestPath requestPath = mock(RequestPath.class);
when(request.getPath()).thenReturn(requestPath);
// Prepare API definitions.
Set<ApiDefinition> apiDefinitions = new HashSet<>();
String apiName1 = "some_customized_api";
ApiDefinition api1 = new ApiDefinition(apiName1).setPredicateItems(Collections.singleton(new ApiPathPredicateItem().setPattern("/product/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)));
String apiName2 = "another_customized_api";
ApiDefinition api2 = new ApiDefinition(apiName2).setPredicateItems(new HashSet<ApiPredicateItem>() {
{
add(new ApiPathPredicateItem().setPattern("/something"));
add(new ApiPathPredicateItem().setPattern("/other/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}
});
apiDefinitions.add(api1);
apiDefinitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);
SentinelGatewayFilter filter = new SentinelGatewayFilter();
when(requestPath.value()).thenReturn("/product/123");
Set<String> matchingApis = filter.pickMatchingApiDefinitions(exchange);
assertThat(matchingApis.size()).isEqualTo(1);
assertThat(matchingApis.contains(apiName1)).isTrue();
when(requestPath.value()).thenReturn("/products");
assertThat(filter.pickMatchingApiDefinitions(exchange).size()).isZero();
when(requestPath.value()).thenReturn("/something");
matchingApis = filter.pickMatchingApiDefinitions(exchange);
assertThat(matchingApis.size()).isEqualTo(1);
assertThat(matchingApis.contains(apiName2)).isTrue();
when(requestPath.value()).thenReturn("/other/foo/3");
matchingApis = filter.pickMatchingApiDefinitions(exchange);
assertThat(matchingApis.size()).isEqualTo(1);
assertThat(matchingApis.contains(apiName2)).isTrue();
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem in project Sentinel by alibaba.
the class GatewayRuleConfig method initCustomizedApis.
private void initCustomizedApis() {
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("some_customized_api").setPredicateItems(new HashSet<ApiPredicateItem>() {
{
add(new ApiPathPredicateItem().setPattern("/images"));
add(new ApiPathPredicateItem().setPattern("/comments").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}
});
ApiDefinition api2 = new ApiDefinition("another_customized_api").setPredicateItems(new HashSet<ApiPredicateItem>() {
{
add(new ApiPathPredicateItem().setPattern("/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}
});
definitions.add(api1);
definitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
Aggregations