Skip to content

RSocketRequestSpec handling metadata values that is a Publisher type #23640

@rwinch

Description

@rwinch

RSocketRequester already handles Publisher for the body. It would be very nice if it could do the same for metadata values. For example right now a user needs to do something like this:

public Mono<AirportLocation> findRadar(String iata) {
	return this.requesterMono.flatMap(req ->
		token().flatMap(token ->
			req.route("find.radar.{iata}", iata)
					.metadata(jwt(token))
					.data(Mono.empty())
					.retrieveMono(AirportLocation.class)
		)
	);
}

private Mono<String> token() {
	return ReactiveSecurityContextHolder.getContext()
			.map(SecurityContext::getAuthentication)
			.map(Authentication::getPrincipal)
			.cast(Jwt.class)
			.map(Jwt::getTokenValue);
}

private Consumer<RSocketRequester.MetadataSpec> jwt(String token) {
	return r -> r.metadata(token, BearerTokenMetadata.BEARER_AUTHENTICATION_MIME_TYPE);
}

If metadata could accept a publisher this can be simplified quite a bit:

public Mono<AirportLocation> findRadar(String iata) {
	return this.requesterMono.flatMap(req ->
		req.route("find.radar.{iata}", iata)
				.metadata(jwt())
				.data(Mono.empty())
				.retrieveMono(AirportLocation.class)
	);
}

private Mono<String> token() {
	return ReactiveSecurityContextHolder.getContext()
			.map(SecurityContext::getAuthentication)
			.map(Authentication::getPrincipal)
			.cast(Jwt.class)
			.map(Jwt::getTokenValue);
}

private Consumer<RSocketRequester.MetadataSpec> jwt() {
	return r -> r.metadata(token(), BearerTokenMetadata.BEARER_AUTHENTICATION_MIME_TYPE);
}

Notice that the user no longer needs to subscribe to the Mono<Jwt> which decreases the number of mappings necessary. The user is also able to only consume a single method now Consumer<RSocketRequester.MetadataSpec> jwt().

Metadata

Metadata

Assignees

Labels

in: messagingIssues in messaging modules (jms, messaging)type: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions