Search in sources :

Example 6 with ApiPredicateItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)

Example 7 with ApiPredicateItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)

Example 8 with ApiPredicateItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem 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);
}
Also used : ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)

Example 9 with ApiPredicateItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem 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();
}
Also used : RequestPath(org.springframework.http.server.RequestPath) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with ApiPredicateItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem 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);
}
Also used : ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem) HashSet(java.util.HashSet)

Aggregations

ApiPathPredicateItem (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem)18 ApiPredicateItem (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)18 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)13 LinkedHashSet (java.util.LinkedHashSet)10 HashSet (java.util.HashSet)6 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 Test (org.junit.Test)1 RequestPath (org.springframework.http.server.RequestPath)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1