private boolean checkFilter(FilterEvaluator filterEvaluator, ProfileFilterDto filter) { try { if (filterEvaluator.matches(filter)) { LOG.trace("profile body matched"); return true; } } catch (EvaluationException ex) { LOG.warn("Failed to process filter [{}]: {} due to evaluate exception. " + "Please check your filter body", filter.getId(), filter.getBody(), ex); } catch (Exception ex) { LOG.error("Failed to process filter [{}]: {} due to exception", filter.getId(), filter.getBody(), ex); } return false; }
@Override public List<ProfileFilterDto> getAllMatchingFilters(AppProfileVersionsKey key, EndpointProfileDto profile) { String endpointProfileSchemaBody = getEndpointProfileSchemaBody(key); String serverProfileSchemaBody = getServerProfileSchemaBody(key); List<ProfileFilterDto> filters = cacheService.getFilters(key); LOG.trace("Found {} filters by {}", filters.size(), key); List<ProfileFilterDto> matchingFilters = new LinkedList<ProfileFilterDto>(); FilterEvaluator filterEvaluator = null; for (ProfileFilterDto filter : filters) { if (filterEvaluator == null) { filterEvaluator = new DefaultFilterEvaluator(); filterEvaluator.init(profile, endpointProfileSchemaBody, serverProfileSchemaBody); } LOG.trace("matching profile body with filter [{}]: {}", filter.getId(), filter.getBody()); if (checkFilter(filterEvaluator, filter)) { matchingFilters.add(filter); } } return matchingFilters; }
/** * Create new instance of <code>ProfileFilter</code>. * @param dto data transfer object contain body, * endpoint profile schema id, server profile * schema id. Assign these values on corresponding * fields of new instance */ public ProfileFilter(ProfileFilterDto dto) { super(dto); this.body = dto.getBody(); String endpointSchemaId = dto.getEndpointProfileSchemaId(); if (isNotBlank(endpointSchemaId)) { this.endpointProfileSchema = new EndpointProfileSchema(getLongId(endpointSchemaId)); } String serverSchemaId = dto.getServerProfileSchemaId(); if (isNotBlank(serverSchemaId)) { this.serverProfileSchema = new ServerProfileSchema(getLongId(serverSchemaId)); } }
@Override public boolean matches(String appToken, String profileFilterId, EndpointProfileDto profile) { AppProfileVersionsKey key = new AppProfileVersionsKey( appToken, profile.getClientProfileVersion(), profile.getServerProfileVersion()); String endpointProfileSchemaBody = getEndpointProfileSchemaBody(key); String serverProfileSchemaBody = getServerProfileSchemaBody(key); FilterEvaluator filterEvaluator = new DefaultFilterEvaluator(); filterEvaluator.init(profile, endpointProfileSchemaBody, serverProfileSchemaBody); ProfileFilterDto filter = cacheService.getFilter(profileFilterId); LOG.trace("matching profile body with filter [{}]: {}", filter.getId(), filter.getBody()); return checkFilter(filterEvaluator, filter); }
@Override public boolean matches(ProfileFilterDto filter) { final Expression expression = new SpelExpressionParser().parseExpression(filter.getBody()); StandardEvaluationContext evaluationContext; if (filter.getEndpointProfileSchemaVersion() != null) { evaluationContext = new StandardEvaluationContext(clientProfileGenericRecord); evaluationContext.setVariable(CLIENT_PROFILE_VARIABLE_NAME, clientProfileGenericRecord); } else { evaluationContext = new StandardEvaluationContext(); } evaluationContext.addPropertyAccessor(new GenericRecordPropertyAccessor()); evaluationContext.setVariable(EP_KEYHASH_VARIABLE_NAME, epKey); if (filter.getServerProfileSchemaVersion() != null) { evaluationContext.setVariable(SERVER_PROFILE_VARIABLE_NAME, serverProfileGenericRecord); } return expression.getValue(evaluationContext, Boolean.class); }
@Override public ProfileFilterDto editProfileFilter(ProfileFilterDto profileFilter) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { String username = getCurrentUser().getUsername(); if (isEmpty(profileFilter.getId())) { profileFilter.setCreatedUsername(username); checkEndpointGroupId(profileFilter.getEndpointGroupId()); } else { profileFilter.setModifiedUsername(username); ProfileFilterDto storedProfileFilter = controlService.getProfileFilter( profileFilter.getId()); Utils.checkNotNull(storedProfileFilter); checkEndpointGroupId(storedProfileFilter.getEndpointGroupId()); } validateProfileFilterBody(profileFilter.getEndpointProfileSchemaId(), profileFilter.getServerProfileSchemaId(), profileFilter.getBody()); return controlService.editProfileFilter(profileFilter); } catch (Exception ex) { throw Utils.handleException(ex); } }
inactiveStruct.setServerProfileSchemaVersion(record.getServerProfileSchemaVersion()); inactiveStruct.setDescription(record.getDescription()); inactiveStruct.setBody(record.getActiveStructureDto().getBody()); record.setInactiveStructureDto(inactiveStruct);