Java Code Examples for reactor.core.publisher.Flux#filter()

The following examples show how to use reactor.core.publisher.Flux#filter() . 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: ApplicationController.java    From Moss with Apache License 2.0 6 votes vote down vote up
/**
 * 得到应用的flux 实例
 * @return
 */
private Flux<MossApplication> applicationFlux(String appName){
    Flux<Instance> instanceFlux=null;
    /**
     * 当应用名不为空进行过滤分页
     */
    if(StringUtils.isNotEmpty(appName)){
        //因为注册到Eureka上的服务为大写
        instanceFlux=registry.getInstances(appName);
    }else {
        instanceFlux=registry.getInstances();
    }
    String registerSource = this.getRegisterSource();
    if(StringUtils.isNotEmpty(registerSource)){
        instanceFlux=instanceFlux.filter(instance->registerSource.equalsIgnoreCase(instance.getRegistration().getSource()));
    }
    return instanceFlux.filter(Instance::isRegistered)
            .groupBy(instance -> instance.getRegistration().getName())
            .flatMap(grouped -> toApplication(grouped.key(), grouped));
}
 
Example 2
Source File: R032_AdvancedFiltering.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void brokenFilteringWithBlocking() throws Exception {
	//given
	final Flux<String> words = Flux.just(LoremIpsum.words());

	//when
	final Flux<String> filtered = words
			.filter(s -> asyncSha256(s).block().startsWith("0"));  //No, no, NO!

	//then
	filtered
			.as(StepVerifier::create)
			.expectNext("ipsum")
			.expectNextCount(9)
			.verifyComplete();
}
 
Example 3
Source File: R032_AdvancedFiltering.java    From reactor-workshop with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void operatorsAreSingleThreaded() throws Exception {
	//given
	final Flux<String> words = Flux.just(LoremIpsum.words());

	//when
	final Flux<String> filtered = words.filter(s -> sha256(s).startsWith("0"));

	//then
	filtered
			.as(StepVerifier::create)
			.expectNext("ipsum")
			.expectNextCount(9)
			.verifyComplete();
}
 
Example 4
Source File: ReactiveTargetResolver.java    From promregator with Apache License 2.0 4 votes vote down vote up
private Flux<IntermediateTarget> resolveOrg(IntermediateTarget it) {
	/* NB: Now we have to consider three cases:
	 * Case 1: both orgName and orgRegex is empty => select all orgs
	 * Case 2: orgName is null, but orgRegex is filled => filter all orgs with the regex
	 * Case 3: orgName is filled, but orgRegex is null => select a single org
	 * In cases 1 and 2, we need the list of all orgs on the platform.
	 */
	
	if (it.getConfigTarget().getOrgRegex() == null && it.getConfigTarget().getOrgName() != null) {
		// Case 3: we have the orgName, but we also need its id
		Mono<IntermediateTarget> itMono = this.cfAccessor.retrieveOrgId(it.getConfigTarget().getOrgName())
				.map(ListOrganizationsResponse::getResources)
				.flatMap(resList -> {
					if (resList == null || resList.isEmpty()) {
						return Mono.empty();
					}
					
					return Mono.just(resList.get(0));
				})
				.map(res -> {
					it.setResolvedOrgName(res.getEntity().getName());
					it.setResolvedOrgId(res.getMetadata().getId());
					return it;
				})
				.doOnError(e -> log.warn(String.format("Error on retrieving org id for org '%s'", it.getConfigTarget().getOrgName()), e))
				.onErrorResume(__ -> Mono.empty());
		
		return itMono.flux();
	}
	
	// Case 1 & 2: Get all orgs from the platform
	Mono<ListOrganizationsResponse> responseMono = this.cfAccessor.retrieveAllOrgIds();

	Flux<OrganizationResource> orgResFlux = responseMono.map(ListOrganizationsResponse::getResources)
		.flatMapMany(Flux::fromIterable);
	
	if (it.getConfigTarget().getOrgRegex() != null) {
		// Case 2
		final Pattern filterPattern = Pattern.compile(it.getConfigTarget().getOrgRegex(), Pattern.CASE_INSENSITIVE);
		
		orgResFlux = orgResFlux.filter(orgRes -> {
			Matcher m = filterPattern.matcher(orgRes.getEntity().getName());
			return m.matches();
		});
	}
	
	return orgResFlux.map(orgRes -> {
		IntermediateTarget itnew = new IntermediateTarget(it);
		itnew.setResolvedOrgId(orgRes.getMetadata().getId());
		itnew.setResolvedOrgName(orgRes.getEntity().getName());
		
		return itnew;
	});
}
 
Example 5
Source File: ReactiveTargetResolver.java    From promregator with Apache License 2.0 4 votes vote down vote up
private Flux<IntermediateTarget> resolveSpace(IntermediateTarget it) {
	/* NB: Now we have to consider three cases:
	 * Case 1: both spaceName and spaceRegex is empty => select all spaces (within the org)
	 * Case 2: spaceName is null, but spaceRegex is filled => filter all spaces with the regex
	 * Case 3: spaceName is filled, but spaceRegex is null => select a single space
	 * In cases 1 and 2, we need the list of all spaces in the org.
	 */
	
	if (it.getConfigTarget().getSpaceRegex() == null && it.getConfigTarget().getSpaceName() != null) {
		// Case 3: we have the spaceName, but we also need its id
		Mono<IntermediateTarget> itMono = this.cfAccessor.retrieveSpaceId(it.getResolvedOrgId(), it.getConfigTarget().getSpaceName())
				.map(ListSpacesResponse::getResources)
				.flatMap(resList -> {
					if (resList == null || resList.isEmpty()) {
						return Mono.empty();
					}
					
					return Mono.just(resList.get(0));
				})
				.map(res -> {
					it.setResolvedSpaceName(res.getEntity().getName());
					it.setResolvedSpaceId(res.getMetadata().getId());
					return it;
				}).doOnError(e -> log.warn(String.format("Error on retrieving space id for org '%s' and space '%s'", it.getResolvedOrgName(), it.getConfigTarget().getSpaceName()), e))
				.onErrorResume(__ -> Mono.empty());
		
		return itMono.flux();
	}
	
	// Case 1 & 2: Get all spaces in the current org
	Mono<ListSpacesResponse> responseMono = this.cfAccessor.retrieveSpaceIdsInOrg(it.getResolvedOrgId());

	Flux<SpaceResource> spaceResFlux = responseMono.map(ListSpacesResponse::getResources)
		.flatMapMany(Flux::fromIterable);
	
	if (it.getConfigTarget().getSpaceRegex() != null) {
		// Case 2
		final Pattern filterPattern = Pattern.compile(it.getConfigTarget().getSpaceRegex(), Pattern.CASE_INSENSITIVE);
		
		spaceResFlux = spaceResFlux.filter(spaceRes -> {
			Matcher m = filterPattern.matcher(spaceRes.getEntity().getName());
			return m.matches();
		});
	}
	
	return spaceResFlux.map(spaceRes -> {
		IntermediateTarget itnew = new IntermediateTarget(it);
		itnew.setResolvedSpaceId(spaceRes.getMetadata().getId());
		itnew.setResolvedSpaceName(spaceRes.getEntity().getName());
		
		return itnew;
	});
}
 
Example 6
Source File: ReactiveTargetResolver.java    From promregator with Apache License 2.0 4 votes vote down vote up
private Flux<IntermediateTarget> resolveApplication(IntermediateTarget it) {
	/* NB: Now we have to consider three cases:
	 * Case 1: both applicationName and applicationRegex is empty => select all applications (in the space)
	 * Case 2: applicationName is null, but applicationRegex is filled => filter all applications with the regex
	 * Case 3: applicationName is filled, but applicationRegex is null => select a single application
	 * In cases 1 and 2, we need the list of all applications in the space.
	 */
	
	if (it.getConfigTarget().getApplicationRegex() == null && it.getConfigTarget().getApplicationName() != null) {
		// Case 3: we have the applicationName, but we also need its id
		
		String appNameToSearchFor = it.getConfigTarget().getApplicationName().toLowerCase(Locale.ENGLISH);
		
		Mono<IntermediateTarget> itMono = this.cfAccessor.retrieveAllApplicationIdsInSpace(it.getResolvedOrgId(), it.getResolvedSpaceId())
				.map(ListApplicationsResponse::getResources)
				.flatMapMany(Flux::fromIterable)
				.filter(appResource -> appNameToSearchFor.equals(appResource.getEntity().getName().toLowerCase(Locale.ENGLISH)))
				.single()
				.doOnError(e -> {
					if (e instanceof NoSuchElementException) {
						logEmptyTarget.warn(String.format("Application id could not be found for org '%s', space '%s' and application '%s'. Check your configuration of targets; skipping it for now; this message may be muted by setting the log level of the emitting logger accordingly!", it.getResolvedOrgName(), it.getResolvedSpaceName(), it.getConfigTarget().getApplicationName()));
					}
				})
				.onErrorResume(e -> Mono.empty())
				.filter( res -> this.isApplicationInScrapableState(res.getEntity().getState()))
				.map(res -> {
					it.setResolvedApplicationName(res.getEntity().getName());
					it.setResolvedApplicationId(res.getMetadata().getId());
					return it;
				}).doOnError(e ->
					log.warn(String.format("Error on retrieving application id for org '%s', space '%s' and application '%s'", it.getResolvedOrgName(), it.getResolvedSpaceName(), it.getConfigTarget().getApplicationName()), e)
				)
				.onErrorResume(__ -> Mono.empty());
		
		return itMono.flux();
	}
	
	// Case 1 & 2: Get all applications in the current space
	Mono<ListApplicationsResponse> responseMono = this.cfAccessor.retrieveAllApplicationIdsInSpace(it.getResolvedOrgId(), it.getResolvedSpaceId());

	Flux<ApplicationResource> appResFlux = responseMono.map(ListApplicationsResponse::getResources)
		.flatMapMany(Flux::fromIterable)
		.doOnError(e ->
			log.warn(String.format("Error on retrieving list of applications in org '%s' and space '%s'", it.getResolvedOrgName(), it.getResolvedSpaceName()), e))
		.onErrorResume(__ -> Flux.empty());
	
	if (it.getConfigTarget().getApplicationRegex() != null) {
		// Case 2
		final Pattern filterPattern = Pattern.compile(it.getConfigTarget().getApplicationRegex(), Pattern.CASE_INSENSITIVE);
		
		appResFlux = appResFlux.filter(appRes -> {
			Matcher m = filterPattern.matcher(appRes.getEntity().getName());
			return m.matches();
		});
	}
	
	Flux<ApplicationResource> scrapableFlux = appResFlux.filter(appRes ->
			this.isApplicationInScrapableState(appRes.getEntity().getState()));
	
	return scrapableFlux.map(appRes -> {
		IntermediateTarget itnew = new IntermediateTarget(it);
		itnew.setResolvedApplicationId(appRes.getMetadata().getId());
		itnew.setResolvedApplicationName(appRes.getEntity().getName());
		
		return itnew;
	});
}
 
Example 7
Source File: FluxOperatorTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
Flux<O> conditional(Flux<O> output) {
	return output.filter(t -> true);
}