com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition Java Examples

The following examples show how to use com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition. 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: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * Parse json data to set of {@link ApiDefinition}.
 *
 * Since the predicateItems of {@link ApiDefinition} is set of interface,
 * here we parse predicateItems to {@link ApiPathPredicateItem} temporarily.
 */
private Set<ApiDefinition> parseJson(String data) {
    Set<ApiDefinition> apiDefinitions = new HashSet<>();
    JSONArray array = JSON.parseArray(data);
    for (Object obj : array) {
        JSONObject o = (JSONObject)obj;
        ApiDefinition apiDefinition = new ApiDefinition((o.getString("apiName")));
        Set<ApiPredicateItem> predicateItems = new HashSet<>();
        JSONArray itemArray = o.getJSONArray("predicateItems");
        if (itemArray != null) {
            predicateItems.addAll(itemArray.toJavaList(ApiPathPredicateItem.class));
        }
        apiDefinition.setPredicateItems(predicateItems);
        apiDefinitions.add(apiDefinition);
    }

    return apiDefinitions;
}
 
Example #4
Source File: ApiDefinitionEntity.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: ApiDefinitionEntity.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #6
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 #7
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 #8
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 #9
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
/**
 * Parse json data to set of {@link ApiDefinition}.
 *
 * Since the predicateItems of {@link ApiDefinition} is set of interface,
 * here we parse predicateItems to {@link ApiPathPredicateItem} temporarily.
 */
private Set<ApiDefinition> parseJson(String data) {
    Set<ApiDefinition> apiDefinitions = new HashSet<>();
    JSONArray array = JSON.parseArray(data);
    for (Object obj : array) {
        JSONObject o = (JSONObject)obj;
        ApiDefinition apiDefinition = new ApiDefinition((o.getString("apiName")));
        Set<ApiPredicateItem> predicateItems = new HashSet<>();
        JSONArray itemArray = o.getJSONArray("predicateItems");
        if (itemArray != null) {
            predicateItems.addAll(itemArray.toJavaList(ApiPathPredicateItem.class));
        }
        apiDefinition.setPredicateItems(predicateItems);
        apiDefinitions.add(apiDefinition);
    }

    return apiDefinitions;
}
 
Example #10
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 #11
Source File: ApiDefinitionEntity.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #12
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 #13
Source File: ApiDefinitionEntity.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #14
Source File: GatewayApiMatcherManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
static synchronized void loadApiDefinitions(/*@Valid*/ Set<ApiDefinition> definitions) {
    if (definitions == null || definitions.isEmpty()) {
        API_MATCHER_MAP.clear();
        return;
    }
    definitions.forEach(GatewayApiMatcherManager::addApiDefinition);
}
 
Example #15
Source File: ZuulGatewayApiMatcherManager.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public static Set<ApiDefinition> getApiDefinitionSet() {
    Set<ApiDefinition> set = new HashSet<>();
    for (RequestContextApiMatcher matcher : API_MATCHER_MAP.values()) {
        set.add(matcher.getApiDefinition());
    }
    return set;
}
 
Example #16
Source File: ZuulGatewayApiMatcherManager.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
static synchronized void loadApiDefinitions(/*@Valid*/ Set<ApiDefinition> definitions) {
    if (definitions == null || definitions.isEmpty()) {
        API_MATCHER_MAP.clear();
        return;
    }
    for (ApiDefinition definition : definitions) {
        addApiDefinition(definition);
    }
}
 
Example #17
Source File: ZuulGatewayApiMatcherManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public static Set<ApiDefinition> getApiDefinitionSet() {
    Set<ApiDefinition> set = new HashSet<>();
    for (RequestContextApiMatcher matcher : API_MATCHER_MAP.values()) {
        set.add(matcher.getApiDefinition());
    }
    return set;
}
 
Example #18
Source File: ZuulGatewayApiMatcherManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
static synchronized void loadApiDefinitions(/*@Valid*/ Set<ApiDefinition> definitions) {
    if (definitions == null || definitions.isEmpty()) {
        API_MATCHER_MAP.clear();
        return;
    }
    for (ApiDefinition definition : definitions) {
        addApiDefinition(definition);
    }
}
 
Example #19
Source File: GatewayApiMatcherManager.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
static synchronized void loadApiDefinitions(/*@Valid*/ Set<ApiDefinition> definitions) {
    if (definitions == null || definitions.isEmpty()) {
        API_MATCHER_MAP.clear();
        return;
    }
    definitions.forEach(GatewayApiMatcherManager::addApiDefinition);
}
 
Example #20
Source File: AbstractApiMatcher.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public AbstractApiMatcher(ApiDefinition apiDefinition) {
    AssertUtil.notNull(apiDefinition, "apiDefinition cannot be null");
    AssertUtil.assertNotBlank(apiDefinition.getApiName(), "apiName cannot be empty");
    this.apiName = apiDefinition.getApiName();
    this.apiDefinition = apiDefinition;

    try {
        initializeMatchers();
    } catch (Exception ex) {
        RecordLog.warn("[GatewayApiMatcher] Failed to initialize internal matchers", ex);
    }
}
 
Example #21
Source File: AbstractApiMatcher.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public AbstractApiMatcher(ApiDefinition apiDefinition) {
    AssertUtil.notNull(apiDefinition, "apiDefinition cannot be null");
    AssertUtil.assertNotBlank(apiDefinition.getApiName(), "apiName cannot be empty");
    this.apiName = apiDefinition.getApiName();
    this.apiDefinition = apiDefinition;

    try {
        initializeMatchers();
    } catch (Exception ex) {
        RecordLog.warn("[GatewayApiMatcher] Failed to initialize internal matchers", ex);
    }
}
 
Example #22
Source File: ZuulGatewayApiMatcherManager.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public static Set<ApiDefinition> getApiDefinitionSet() {
    Set<ApiDefinition> set = new HashSet<>();
    for (HttpRequestMessageApiMatcher matcher : API_MATCHER_MAP.values()) {
        set.add(matcher.getApiDefinition());
    }
    return set;
}
 
Example #23
Source File: ZuulGatewayApiMatcherManager.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
static synchronized void loadApiDefinitions(/*@Valid*/ Set<ApiDefinition> definitions) {
    if (definitions == null || definitions.isEmpty()) {
        API_MATCHER_MAP.clear();
        return;
    }
    for (ApiDefinition definition : definitions) {
        addApiDefinition(definition);
    }
}
 
Example #24
Source File: AbstractApiMatcher.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public ApiDefinition getApiDefinition() {
    return apiDefinition;
}
 
Example #25
Source File: SentinelGatewayAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Bean("sentinel-xml-gw-api-group-converter")
public XmlConverter xmlApiConverter() {
	return new XmlConverter(xmlMapper, ApiDefinition.class);
}
 
Example #26
Source File: ZuulGatewayApiMatcherManager.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
static void addApiDefinition(ApiDefinition definition) {
    API_MATCHER_MAP.put(definition.getApiName(), new HttpRequestMessageApiMatcher(definition));
}
 
Example #27
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 #28
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public synchronized static WritableDataSource<Set<ApiDefinition>> getWritableDataSource() {
    return apiDefinitionWds;
}
 
Example #29
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public synchronized static void setWritableDataSource(WritableDataSource<Set<ApiDefinition>> apiDefinitionWds) {
    UpdateGatewayApiDefinitionGroupCommandHandler.apiDefinitionWds = apiDefinitionWds;
}
 
Example #30
Source File: SentinelGatewayAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Bean("sentinel-json-gw-api-group-converter")
public JsonConverter jsonApiConverter() {
	return new JsonConverter(objectMapper, ApiDefinition.class);
}