Java Code Examples for com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager#loadApiDefinitions()

The following examples show how to use com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager#loadApiDefinitions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GatewayConfiguration.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
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));
        }});
    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);
}
 
Example 2
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Bad data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
    } catch (Exception e) {
        RecordLog.info("Decode gateway API definition data error", e);
        return CommandResponse.ofFailure(e, "decode gateway API definition data error");
    }

    RecordLog.info("[API Server] Receiving data change (type: gateway API definition): {}", data);

    String result = SUCCESS_MSG;

    Set<ApiDefinition> apiDefinitions = parseJson(data);
    GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);
    if (!writeToDataSource(apiDefinitionWds, apiDefinitions)) {
        result = WRITE_DS_FAILURE_MSG;
    }
    return CommandResponse.ofSuccess(result);
}
 
Example 3
Source File: GatewayConfiguration.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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));
        }});
    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);
}
 
Example 4
Source File: GatewayRuleConfig.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: GatewayRuleConfig.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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("/aliyun_product/**")
                .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);
}
 
Example 6
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Bad data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
    } catch (Exception e) {
        RecordLog.info("Decode gateway API definition data error", e);
        return CommandResponse.ofFailure(e, "decode gateway API definition data error");
    }

    RecordLog.info("[API Server] Receiving data change (type: gateway API definition): {0}", data);

    String result = SUCCESS_MSG;

    Set<ApiDefinition> apiDefinitions = parseJson(data);
    GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);

    return CommandResponse.ofSuccess(result);
}
 
Example 7
Source File: GatewayRuleConfig.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
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("/aliyun_product/**")
                .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);
}
 
Example 8
Source File: SentinelGatewayFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 9
Source File: SentinelGatewayFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 10
Source File: GatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}
 
Example 11
Source File: GatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}
 
Example 12
Source File: GatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}
 
Example 13
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 14
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 15
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 16
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 17
Source File: SentinelGatewayFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 18
Source File: SentinelGatewayFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 19
Source File: SentinelGatewayFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example 20
Source File: GatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}