com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem Java Examples
The following examples show how to use
com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem.
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 |
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: SentinelGatewayAutoConfiguration.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
@Override public ApiPredicateItem deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { ObjectMapper mapper = (ObjectMapper) jp.getCodec(); ObjectNode root = mapper.readTree(jp); Class<? extends ApiPredicateItem> apiPredicateItemClass = null; Iterator<Entry<String, JsonNode>> elementsIterator = root.fields(); while (elementsIterator.hasNext()) { Entry<String, JsonNode> element = elementsIterator.next(); String name = element.getKey(); if (registry.containsKey(name)) { apiPredicateItemClass = registry.get(name); break; } } if (apiPredicateItemClass == null) { return null; } return mapper.readValue(root.toString(), apiPredicateItemClass); }
Example #3
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java From Sentinel with Apache License 2.0 | 6 votes |
/** * 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 |
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 |
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 |
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 |
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 |
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: GatewayRuleConfig.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
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 #10
Source File: ApiDefinitionEntity.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
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 #11
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
/** * 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 #12
Source File: ApiDefinitionEntity.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
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 #13
Source File: SentinelGatewayAutoConfiguration.java From spring-cloud-alibaba with Apache License 2.0 | 5 votes |
public SentinelXmlConfiguration() { xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer(); deserializer.registerApiPredicateItem("pattern", ApiPathPredicateItem.class); deserializer.registerApiPredicateItem("items", ApiPredicateGroupItem.class); SimpleModule module = new SimpleModule( "PolymorphicGatewayDeserializerModule", new Version(1, 0, 0, null)); module.addDeserializer(ApiPredicateItem.class, deserializer); xmlMapper.registerModule(module); }
Example #14
Source File: SentinelGatewayAutoConfiguration.java From spring-cloud-alibaba with Apache License 2.0 | 5 votes |
public SentinelJsonConfiguration() { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer(); deserializer.registerApiPredicateItem("pattern", ApiPathPredicateItem.class); deserializer.registerApiPredicateItem("items", ApiPredicateGroupItem.class); SimpleModule module = new SimpleModule( "PolymorphicApiPredicateItemDeserializerModule", new Version(1, 0, 0, null)); module.addDeserializer(ApiPredicateItem.class, deserializer); objectMapper.registerModule(module); }
Example #15
Source File: RequestContextApiMatcher.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Override protected void initializeMatchers() { if (apiDefinition.getPredicateItems() != null) { for (ApiPredicateItem item : apiDefinition.getPredicateItems()) { Predicate<RequestContext> predicate = fromApiPredicate(item); if (predicate != null) { matchers.add(predicate); } } } }
Example #16
Source File: RequestContextApiMatcher.java From Sentinel with Apache License 2.0 | 5 votes |
@Override protected void initializeMatchers() { if (apiDefinition.getPredicateItems() != null) { for (ApiPredicateItem item : apiDefinition.getPredicateItems()) { Predicate<RequestContext> predicate = fromApiPredicate(item); if (predicate != null) { matchers.add(predicate); } } } }
Example #17
Source File: HttpRequestMessageApiMatcher.java From Sentinel with Apache License 2.0 | 5 votes |
@Override protected void initializeMatchers() { if (apiDefinition.getPredicateItems() != null) { for (ApiPredicateItem item : apiDefinition.getPredicateItems()) { Predicate<HttpRequestMessage> predicate = fromApiPredicate(item); if (predicate != null) { matchers.add(predicate); } } } }
Example #18
Source File: SentinelGatewayFilterTest.java From Sentinel with Apache License 2.0 | 4 votes |
@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 #19
Source File: SentinelGatewayAutoConfiguration.java From spring-cloud-alibaba with Apache License 2.0 | 4 votes |
void registerApiPredicateItem(String uniqueAttribute, Class<? extends ApiPredicateItem> apiPredicateItemClass) { registry.put(uniqueAttribute, apiPredicateItemClass); }
Example #20
Source File: SentinelGatewayAutoConfiguration.java From spring-cloud-alibaba with Apache License 2.0 | 4 votes |
ApiPredicateItemDeserializer() { super(ApiPredicateItem.class); }
Example #21
Source File: HttpRequestMessageApiMatcher.java From Sentinel with Apache License 2.0 | 4 votes |
private Predicate<HttpRequestMessage> fromApiPredicate(/*@NonNull*/ ApiPredicateItem item) { if (item instanceof ApiPathPredicateItem) { return fromApiPathPredicate((ApiPathPredicateItem)item); } return null; }
Example #22
Source File: WebExchangeApiMatcher.java From Sentinel with Apache License 2.0 | 4 votes |
private Optional<Predicate<ServerWebExchange>> fromApiPredicate(/*@NonNull*/ ApiPredicateItem item) { if (item instanceof ApiPathPredicateItem) { return fromApiPathPredicate((ApiPathPredicateItem)item); } return Optional.empty(); }
Example #23
Source File: RequestContextApiMatcher.java From Sentinel with Apache License 2.0 | 4 votes |
private Predicate<RequestContext> fromApiPredicate(/*@NonNull*/ ApiPredicateItem item) { if (item instanceof ApiPathPredicateItem) { return fromApiPathPredicate((ApiPathPredicateItem)item); } return null; }
Example #24
Source File: RequestContextApiMatcher.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
private Predicate<RequestContext> fromApiPredicate(/*@NonNull*/ ApiPredicateItem item) { if (item instanceof ApiPathPredicateItem) { return fromApiPathPredicate((ApiPathPredicateItem)item); } return null; }
Example #25
Source File: WebExchangeApiMatcher.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
private Optional<Predicate<ServerWebExchange>> fromApiPredicate(/*@NonNull*/ ApiPredicateItem item) { if (item instanceof ApiPathPredicateItem) { return fromApiPathPredicate((ApiPathPredicateItem)item); } return Optional.empty(); }
Example #26
Source File: SentinelGatewayFilterTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
@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(); }