Java Code Examples for reactor.core.publisher.Mono#justOrEmpty()

The following examples show how to use reactor.core.publisher.Mono#justOrEmpty() . 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: ModelAttributeMethodArgumentResolver.java    From java-technology-stack with MIT License 7 votes vote down vote up
private Mono<?> prepareAttributeMono(String attributeName, ResolvableType attributeType,
		BindingContext context, ServerWebExchange exchange) {

	Object attribute = context.getModel().asMap().get(attributeName);

	if (attribute == null) {
		attribute = findAndRemoveReactiveAttribute(context.getModel(), attributeName);
	}

	if (attribute == null) {
		return createAttribute(attributeName, attributeType.toClass(), context, exchange);
	}

	ReactiveAdapter adapterFrom = getAdapterRegistry().getAdapter(null, attribute);
	if (adapterFrom != null) {
		Assert.isTrue(!adapterFrom.isMultiValue(), "Data binding only supports single-value async types");
		return Mono.from(adapterFrom.toPublisher(attribute));
	}
	else {
		return Mono.justOrEmpty(attribute);
	}
}
 
Example 2
Source File: JWTAuthenticationStrategy.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest) throws MailboxSessionCreationException {
    Stream<Username> userLoginStream = extractTokensFromAuthHeaders(authHeaders(httpRequest))
        .filter(tokenManager::verify)
        .map(tokenManager::extractLogin)
        .map(Username::of)
        .peek(username -> {
            try {
                usersRepository.assertValid(username);
            } catch (UsersRepositoryException e) {
                throw new MailboxSessionCreationException(e);
            }
        });

    Stream<MailboxSession> mailboxSessionStream = userLoginStream
            .map(mailboxManager::createSystemSession);

    return Mono.justOrEmpty(mailboxSessionStream.findFirst());
}
 
Example 3
Source File: ReactiveAdapterDefault.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Mono<T> toMono(@Nullable Object source) {
    if (source instanceof Mono) {
        return (Mono) source;
    } else if (source instanceof Publisher) {
        return Mono.from((Publisher) source);
    } else {
        return (Mono<T>) Mono.justOrEmpty(source);
    }
}
 
Example 4
Source File: DataHandler.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public Mono<ServerResponse> countEmps(ServerRequest req) {
	
	Mono<Long> count = Flux.fromIterable(employeeServiceImpl.findAllEmps())
			.count();
	CountEmp countEmp = new CountEmp();
	countEmp.setCount(count.block());
	Mono<CountEmp> monoCntEmp = Mono.justOrEmpty(countEmp);
	return ok().contentType(MediaType.APPLICATION_STREAM_JSON).body(monoCntEmp, CountEmp.class)
			.switchIfEmpty(ServerResponse.notFound().build());
}
 
Example 5
Source File: LoginHandler.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public Mono<ServerResponse> countLogins(ServerRequest req) {
	
	Mono<Long> count = Flux.fromIterable(logindetailsServiceImpl.findAllLogindetails())
			.count();
	TotalUsers countEmp = new TotalUsers();
	countEmp.setCount(count.block());
	Mono<TotalUsers> monoCntLogins = Mono.justOrEmpty(countEmp);
	return ok().contentType(MediaType.APPLICATION_STREAM_JSON).body(monoCntLogins, TotalUsers.class)
			.switchIfEmpty(ServerResponse.notFound().build());
}
 
Example 6
Source File: DeptDataHandler.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public Mono<ServerResponse> countDepts(ServerRequest req) {
	Mono<Long> count = Flux.fromIterable(departmentServiceImpl.findAllDepts())
			.count();	
	CountDept countDept = new CountDept();
	countDept.setCount(count.block());
	Mono<CountDept> monoCntDept = Mono.justOrEmpty(countDept);
	return ok().contentType(MediaType.APPLICATION_STREAM_JSON).body(monoCntDept, CountDept.class)
			.switchIfEmpty(ServerResponse.notFound().build());
}
 
Example 7
Source File: CustomWebSecurityConfig.java    From tutorials with MIT License 5 votes vote down vote up
public Mono<String> customer(Authentication authentication) {
    return Mono.justOrEmpty(authentication
      .getPrincipal()
      .toString()
      .startsWith("customer") ? authentication
      .getPrincipal()
      .toString() : null);
}
 
Example 8
Source File: CloudFoundryAppSchedulerTests.java    From spring-cloud-deployer-cloudfoundry with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> delete(DeleteJobRequest request) {
	for (int i = 0; i < this.jobResources.size(); i++) {
		if (this.jobResources.get(i).getId().equals(request.getJobId())) {
			jobResources.remove(i);
			break;
		}
	}
	return Mono.justOrEmpty(null);
}
 
Example 9
Source File: ReactiveProjectToTasksRepository.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> removeRelations(ReactiveProject source, Collection<Long> targetIds, ResourceField field) {
	ArrayList<Long> ids = new ArrayList<>();
	if (relationMap.containsKey(source.getId())) {
		ids.addAll(relationMap.getList(source.getId()));
	}
	ids.removeAll(targetIds);
	relationMap.set(source.getId(), ids);
	return Mono.justOrEmpty(null);
}
 
Example 10
Source File: TopicPart.java    From jetlinks-community with Apache License 2.0 4 votes vote down vote up
public Mono<TopicPart> get(String topic) {
    return Mono.justOrEmpty(getOrDefault(topic, ((topicPart, s) -> null)));
}
 
Example 11
Source File: MockServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<WebSession> session() {
	return Mono.justOrEmpty(this.session);
}
 
Example 12
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<String> getName() {
	return Mono.justOrEmpty(name);
}
 
Example 13
Source File: MovieService.java    From spring-5-examples with MIT License 4 votes vote down vote up
public Mono<Movie> getById(final String id) {
    return Mono.justOrEmpty(repository.findById(id));
//    return repository.findById(id);
  }
 
Example 14
Source File: AbstractNamedValueSyncArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected final Mono<Object> resolveName(String name, MethodParameter param, ServerWebExchange exchange) {
	return Mono.justOrEmpty(resolveNamedValue(name, param, exchange));
}
 
Example 15
Source File: UserRepositorySample.java    From Building-RESTful-Web-Services-with-Spring-5-Second-Edition with MIT License 4 votes vote down vote up
@Override
public Mono<User> getUser(Integer id){
	return Mono.justOrEmpty(this.users.get(id));		
}
 
Example 16
Source File: AbstractNamedValueSyncArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected final Mono<Object> resolveName(String name, MethodParameter param, ServerWebExchange exchange) {
	return Mono.justOrEmpty(resolveNamedValue(name, param, exchange));
}
 
Example 17
Source File: ReactiveTaskToProjectRepository.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> setRelation(ReactiveTask source, Long targetId, ResourceField field) {
	relationMap.put(source.getId(), targetId);
	return Mono.justOrEmpty(null);
}
 
Example 18
Source File: BeanCatalogService.java    From spring-cloud-open-service-broker with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<ServiceDefinition> getServiceDefinition(final String serviceId) {
	return Mono.justOrEmpty(serviceDefs.get(serviceId));
}
 
Example 19
Source File: SyncHandlerMethodArgumentResolver.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>By default this simply delegates to {@link #resolveArgumentValue} for
 * synchronous resolution.
 */
@Override
default Mono<Object> resolveArgument(MethodParameter parameter, Message<?> message) {
	return Mono.justOrEmpty(resolveArgumentValue(parameter, message));
}
 
Example 20
Source File: UserController.java    From Spring-Boot-Book with Apache License 2.0 2 votes vote down vote up
/**
 * 获取单个用户
 *
 * @param id
 * @return
 */
@GetMapping("/{id}")
public Mono<User> getUser(@PathVariable Long id) {
    return Mono.justOrEmpty(users.get(id));
}