org.springframework.messaging.handler.DestinationPatternsMessageCondition Java Examples

The following examples show how to use org.springframework.messaging.handler.DestinationPatternsMessageCondition. 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: SendToMethodReturnValueHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected String[] getTargetDestinations(@Nullable Annotation annotation, Message<?> message, String defaultPrefix) {
	if (annotation != null) {
		String[] value = (String[]) AnnotationUtils.getValue(annotation);
		if (!ObjectUtils.isEmpty(value)) {
			return value;
		}
	}

	String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER;
	String destination = (String) message.getHeaders().get(name);
	if (!StringUtils.hasText(destination)) {
		throw new IllegalStateException("No lookup destination header in " + message);
	}

	return (destination.startsWith("/") ?
			new String[] {defaultPrefix + destination} : new String[] {defaultPrefix + '/' + destination});
}
 
Example #2
Source File: AbstractMethodMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	String destination = getDestination(message);
	if (destination == null) {
		return;
	}
	String lookupDestination = getLookupDestination(destination);
	if (lookupDestination == null) {
		return;
	}

	MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
	headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
	headerAccessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders());

	if (logger.isDebugEnabled()) {
		logger.debug("Searching methods to handle " +
				headerAccessor.getShortLogMessage(message.getPayload()) +
				", lookupDestination='" + lookupDestination + "'");
	}

	handleMessageInternal(message, lookupDestination);
	headerAccessor.setImmutable();
}
 
Example #3
Source File: AbstractMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	String destination = getDestination(message);
	if (destination == null) {
		return;
	}
	String lookupDestination = getLookupDestination(destination);
	if (lookupDestination == null) {
		return;
	}
	MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
	headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
	headerAccessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders());

	if (logger.isDebugEnabled()) {
		logger.debug("Searching methods to handle " + headerAccessor.getShortLogMessage(message.getPayload()));
	}

	handleMessageInternal(message, lookupDestination);
	headerAccessor.setImmutable();
}
 
Example #4
Source File: SendToMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected String[] getTargetDestinations(Annotation annotation, Message<?> message, String defaultPrefix) {
	if (annotation != null) {
		String[] value = (String[]) AnnotationUtils.getValue(annotation);
		if (!ObjectUtils.isEmpty(value)) {
			return value;
		}
	}
	String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER;
	String destination = (String) message.getHeaders().get(name);
	if (!StringUtils.hasText(destination)) {
		throw new IllegalStateException("No lookup destination header in " + message);
	}

	return (destination.startsWith("/") ?
			new String[] {defaultPrefix + destination} : new String[] {defaultPrefix + "/" + destination});
}
 
Example #5
Source File: MessageMappingMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected Mono<Void> handleMatch(CompositeMessageCondition mapping, HandlerMethod handlerMethod, Message<?> message) {
	Set<String> patterns = mapping.getCondition(DestinationPatternsMessageCondition.class).getPatterns();
	if (!CollectionUtils.isEmpty(patterns)) {
		String pattern = patterns.iterator().next();
		String destination = getDestination(message);
		Assert.state(destination != null, "Missing destination header");
		Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, destination);
		if (!CollectionUtils.isEmpty(vars)) {
			MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
			Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required");
			mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
		}
	}
	return super.handleMatch(mapping, handlerMethod, message);
}
 
Example #6
Source File: AbstractMethodMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	String destination = getDestination(message);
	if (destination == null) {
		return;
	}
	String lookupDestination = getLookupDestination(destination);
	if (lookupDestination == null) {
		return;
	}

	MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
	headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
	headerAccessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders());

	if (logger.isDebugEnabled()) {
		logger.debug("Searching methods to handle " +
				headerAccessor.getShortLogMessage(message.getPayload()) +
				", lookupDestination='" + lookupDestination + "'");
	}

	handleMessageInternal(message, lookupDestination);
	headerAccessor.setImmutable();
}
 
Example #7
Source File: SendToMethodReturnValueHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected String[] getTargetDestinations(@Nullable Annotation annotation, Message<?> message, String defaultPrefix) {
	if (annotation != null) {
		String[] value = (String[]) AnnotationUtils.getValue(annotation);
		if (!ObjectUtils.isEmpty(value)) {
			return value;
		}
	}

	String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER;
	String destination = (String) message.getHeaders().get(name);
	if (!StringUtils.hasText(destination)) {
		throw new IllegalStateException("No lookup destination header in " + message);
	}

	return (destination.startsWith("/") ?
			new String[] {defaultPrefix + destination} : new String[] {defaultPrefix + '/' + destination});
}
 
Example #8
Source File: MethodMessageHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void bestMatch() throws NoSuchMethodException {
	TestMethodMessageHandler handler = new TestMethodMessageHandler();
	TestController controller = new TestController();
	handler.register(controller, TestController.class.getMethod("handleMessageMatch1"), "/bestmatch/{foo}/path");
	handler.register(controller, TestController.class.getMethod("handleMessageMatch2"), "/bestmatch/*/*");
	handler.afterPropertiesSet();

	Message<?> message = new GenericMessage<>("body", Collections.singletonMap(
			DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, "/bestmatch/bar/path"));

	handler.handleMessage(message).block(Duration.ofSeconds(5));

	StepVerifier.create((Publisher<Object>) handler.getLastReturnValue())
			.expectNext("handleMessageMatch1")
			.verifyComplete();
}
 
Example #9
Source File: MethodMessageHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void argumentResolution() {

	ArgumentResolverConfigurer configurer = new ArgumentResolverConfigurer();
	configurer.addCustomResolver(new StubArgumentResolver(String.class, "foo"));

	TestMethodMessageHandler handler = initMethodMessageHandler(
			theHandler -> theHandler.setArgumentResolverConfigurer(configurer),
			TestController.class);

	Message<?> message = new GenericMessage<>("body", Collections.singletonMap(
			DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, "/handleMessageWithArgument"));

	handler.handleMessage(message).block(Duration.ofSeconds(5));

	StepVerifier.create((Publisher<Object>) handler.getLastReturnValue())
			.expectNext("handleMessageWithArgument,payload=foo")
			.verifyComplete();
}
 
Example #10
Source File: MethodMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Comparator<String> getMappingComparator(final Message<?> message) {
	return new Comparator<String>() {
		@Override
		public int compare(String info1, String info2) {
			DestinationPatternsMessageCondition cond1 = new DestinationPatternsMessageCondition(info1);
			DestinationPatternsMessageCondition cond2 = new DestinationPatternsMessageCondition(info2);
			return cond1.compareTo(cond2, message);
		}
	};
}
 
Example #11
Source File: SendToMethodReturnValueHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Message<?> createMessage(String sessId, String subsId, String destPrefix, String dest, Principal user) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
	headerAccessor.setSessionId(sessId);
	headerAccessor.setSubscriptionId(subsId);
	if (dest != null && destPrefix != null) {
		headerAccessor.setDestination(destPrefix + dest);
		headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, dest);
	}
	if (user != null) {
		headerAccessor.setUser(user);
	}
	return MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
}
 
Example #12
Source File: SimpMessageMappingInfo.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public SimpMessageMappingInfo getMatchingCondition(Message<?> message) {
	SimpMessageTypeMessageCondition typeCond = this.messageTypeMessageCondition.getMatchingCondition(message);
	if (typeCond == null) {
		return null;
	}
	DestinationPatternsMessageCondition destCond = this.destinationConditions.getMatchingCondition(message);
	if (destCond == null) {
		return null;
	}
	return new SimpMessageMappingInfo(typeCond, destCond);
}
 
Example #13
Source File: SimpMessageMappingInfo.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public SimpMessageMappingInfo combine(SimpMessageMappingInfo other) {
	SimpMessageTypeMessageCondition typeCond =
			this.getMessageTypeMessageCondition().combine(other.getMessageTypeMessageCondition());
	DestinationPatternsMessageCondition destCond =
			this.destinationConditions.combine(other.getDestinationConditions());
	return new SimpMessageMappingInfo(typeCond, destCond);
}
 
Example #14
Source File: MethodMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Comparator<String> getMappingComparator(final Message<?> message) {
	return (info1, info2) -> {
		DestinationPatternsMessageCondition cond1 = new DestinationPatternsMessageCondition(info1);
		DestinationPatternsMessageCondition cond2 = new DestinationPatternsMessageCondition(info2);
		return cond1.compareTo(cond2, message);
	};
}
 
Example #15
Source File: MethodMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Comparator<String> getMappingComparator(Message<?> message) {
	return (info1, info2) -> {
		DestinationPatternsMessageCondition cond1 = new DestinationPatternsMessageCondition(info1);
		DestinationPatternsMessageCondition cond2 = new DestinationPatternsMessageCondition(info2);
		return cond1.compareTo(cond2, message);
	};
}
 
Example #16
Source File: SimpMessageMappingInfo.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public SimpMessageMappingInfo combine(SimpMessageMappingInfo other) {
	SimpMessageTypeMessageCondition typeCond =
			this.getMessageTypeMessageCondition().combine(other.getMessageTypeMessageCondition());
	DestinationPatternsMessageCondition destCond =
			this.destinationConditions.combine(other.getDestinationConditions());
	return new SimpMessageMappingInfo(typeCond, destCond);
}
 
Example #17
Source File: MethodMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void handleException() {

	TestMethodMessageHandler handler = initMethodMessageHandler(TestController.class);

	Message<?> message = new GenericMessage<>("body", Collections.singletonMap(
			DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, "/handleMessageWithError"));

	handler.handleMessage(message).block(Duration.ofSeconds(5));

	StepVerifier.create((Publisher<Object>) handler.getLastReturnValue())
			.expectNext("handleIllegalStateException,ex=rejected")
			.verifyComplete();
}
 
Example #18
Source File: MessageMappingMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<?> message(String destination, String... content) {
	Flux<DataBuffer> payload = Flux.fromIterable(Arrays.asList(content)).map(parts -> toDataBuffer(parts));
	MessageHeaderAccessor headers = new MessageHeaderAccessor();
	headers.setLeaveMutable(true);
	headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination);
	return MessageBuilder.createMessage(payload, headers.getMessageHeaders());
}
 
Example #19
Source File: MessageMappingMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void unhandledExceptionShouldFlowThrough() {

	GenericMessage<?> message = new GenericMessage<>(new Object(),
			Collections.singletonMap(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, "string"));

	StepVerifier.create(initMesssageHandler().handleMessage(message))
			.expectErrorSatisfies(ex -> assertTrue(
					"Actual: " + ex.getMessage(),
					ex.getMessage().startsWith("Could not resolve method parameter at index 0")))
			.verify(Duration.ofSeconds(5));
}
 
Example #20
Source File: SendToMethodReturnValueHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<?> createMessage(String sessId, String subsId, String destPrefix, String dest, Principal user) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
	headerAccessor.setSessionId(sessId);
	headerAccessor.setSubscriptionId(subsId);
	if (dest != null && destPrefix != null) {
		headerAccessor.setDestination(destPrefix + dest);
		headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, dest);
	}
	if (user != null) {
		headerAccessor.setUser(user);
	}
	return MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
}
 
Example #21
Source File: MessagingRSocket.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(String destination, @Nullable MonoProcessor<?> replyMono) {
	MessageHeaderAccessor headers = new MessageHeaderAccessor();
	headers.setLeaveMutable(true);
	headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination);
	if (this.dataMimeType != null) {
		headers.setContentType(this.dataMimeType);
	}
	headers.setHeader(RSocketRequesterMethodArgumentResolver.RSOCKET_REQUESTER_HEADER, this.requester);
	if (replyMono != null) {
		headers.setHeader(RSocketPayloadReturnValueHandler.RESPONSE_HEADER, replyMono);
	}
	headers.setHeader(HandlerMethodReturnValueHandler.DATA_BUFFER_FACTORY_HEADER, this.bufferFactory);
	return headers.getMessageHeaders();
}
 
Example #22
Source File: SimpMessageMappingInfo.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public SimpMessageMappingInfo getMatchingCondition(Message<?> message) {
	SimpMessageTypeMessageCondition typeCond = this.messageTypeMessageCondition.getMatchingCondition(message);
	if (typeCond == null) {
		return null;
	}
	DestinationPatternsMessageCondition destCond = this.destinationConditions.getMatchingCondition(message);
	if (destCond == null) {
		return null;
	}
	return new SimpMessageMappingInfo(typeCond, destCond);
}
 
Example #23
Source File: MessageMappingMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Set<String> getDirectLookupMappings(CompositeMessageCondition mapping) {
	Set<String> result = new LinkedHashSet<>();
	for (String pattern : mapping.getCondition(DestinationPatternsMessageCondition.class).getPatterns()) {
		if (!this.pathMatcher.isPattern(pattern)) {
			result.add(pattern);
		}
	}
	return result;
}
 
Example #24
Source File: MessageMappingMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private CompositeMessageCondition getCondition(AnnotatedElement element) {
	MessageMapping annot = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
	if (annot == null || annot.value().length == 0) {
		return null;
	}
	String[] destinations = annot.value();
	if (this.valueResolver != null) {
		destinations = Arrays.stream(annot.value())
				.map(s -> this.valueResolver.resolveStringValue(s))
				.toArray(String[]::new);
	}
	return new CompositeMessageCondition(new DestinationPatternsMessageCondition(destinations, this.pathMatcher));
}
 
Example #25
Source File: SendToMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<?> createInputMessage(String sessId, String subsId, String destinationPrefix,
           String destination, Principal principal) {

	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
	headerAccessor.setSessionId(sessId);
	headerAccessor.setSubscriptionId(subsId);
	if (destination != null && destinationPrefix != null) {
		headerAccessor.setDestination(destinationPrefix + destination);
		headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination);
	}
	if (principal != null) {
		headerAccessor.setUser(principal);
	}
	return MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
}
 
Example #26
Source File: MethodMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Comparator<String> getMappingComparator(final Message<?> message) {
	return new Comparator<String>() {
		@Override
		public int compare(String info1, String info2) {
			DestinationPatternsMessageCondition cond1 = new DestinationPatternsMessageCondition(info1);
			DestinationPatternsMessageCondition cond2 = new DestinationPatternsMessageCondition(info2);
			return cond1.compareTo(cond2, message);
		}
	};
}
 
Example #27
Source File: SimpMessageMappingInfo.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public DestinationPatternsMessageCondition getDestinationConditions() {
	return this.destinationConditions;
}
 
Example #28
Source File: SimpMessageMappingInfo.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public SimpMessageMappingInfo(SimpMessageTypeMessageCondition messageTypeMessageCondition,
		DestinationPatternsMessageCondition destinationConditions) {

	this.messageTypeMessageCondition = messageTypeMessageCondition;
	this.destinationConditions = destinationConditions;
}
 
Example #29
Source File: SimpAnnotationMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private SimpMessageMappingInfo createSubscribeMappingCondition(String[] destinations) {
	String[] resolvedDestinations = resolveEmbeddedValuesInDestinations(destinations);
	return new SimpMessageMappingInfo(SimpMessageTypeMessageCondition.SUBSCRIBE,
			new DestinationPatternsMessageCondition(resolvedDestinations, this.pathMatcher));
}
 
Example #30
Source File: SimpAnnotationMethodMessageHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
private SimpMessageMappingInfo createMessageMappingCondition(String[] destinations) {
	String[] resolvedDestinations = resolveEmbeddedValuesInDestinations(destinations);
	return new SimpMessageMappingInfo(SimpMessageTypeMessageCondition.MESSAGE,
			new DestinationPatternsMessageCondition(resolvedDestinations, this.pathMatcher));
}