Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSocketRequestSpec handling metadata values that is a Publisher type #23640

Closed
rwinch opened this issue Sep 13, 2019 · 0 comments
Closed

RSocketRequestSpec handling metadata values that is a Publisher type #23640

rwinch opened this issue Sep 13, 2019 · 0 comments
Assignees
Milestone

Comments

@rwinch
Copy link
Member

@rwinch rwinch commented Sep 13, 2019

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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.