Allow Validator config in XML websocket namespace
This commit adds a new "validator" XML attribute to the `<websocket:message-broker/>` element. This allows configuring a specific Validator to be used for payload validation. Issue: SPR-13996
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -109,6 +109,9 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser {
|
||||
private static final boolean jackson2Present = ClassUtils.isPresent(
|
||||
"com.fasterxml.jackson.databind.ObjectMapper", MessageBrokerBeanDefinitionParser.class.getClassLoader());
|
||||
|
||||
private static final boolean javaxValidationPresent =
|
||||
ClassUtils.isPresent("javax.validation.Validator", MessageBrokerBeanDefinitionParser.class.getClassLoader());
|
||||
|
||||
|
||||
@Override
|
||||
public BeanDefinition parse(Element element, ParserContext context) {
|
||||
@@ -516,6 +519,11 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser {
|
||||
beanDef.getPropertyValues().add("pathMatcher", new RuntimeBeanReference(pathMatcherRef));
|
||||
}
|
||||
|
||||
RuntimeBeanReference validatorRef = getValidator(messageBrokerElement, source, context);
|
||||
if (validatorRef != null) {
|
||||
beanDef.getPropertyValues().add("validator", validatorRef);
|
||||
}
|
||||
|
||||
Element resolversElement = DomUtils.getChildElementByTagName(messageBrokerElement, "argument-resolvers");
|
||||
if (resolversElement != null) {
|
||||
values.add("customArgumentResolvers", extractBeanSubElements(resolversElement, context));
|
||||
@@ -529,6 +537,24 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser {
|
||||
registerBeanDef(beanDef, context, source);
|
||||
}
|
||||
|
||||
private RuntimeBeanReference getValidator(Element messageBrokerElement, Object source, ParserContext parserContext) {
|
||||
if (messageBrokerElement.hasAttribute("validator")) {
|
||||
return new RuntimeBeanReference(messageBrokerElement.getAttribute("validator"));
|
||||
}
|
||||
else if (javaxValidationPresent) {
|
||||
RootBeanDefinition validatorDef = new RootBeanDefinition(
|
||||
"org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean");
|
||||
validatorDef.setSource(source);
|
||||
validatorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
String validatorName = parserContext.getReaderContext().registerWithGeneratedName(validatorDef);
|
||||
parserContext.registerComponent(new BeanComponentDefinition(validatorDef, validatorName));
|
||||
return new RuntimeBeanReference(validatorName);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ManagedList<Object> extractBeanSubElements(Element parentElement, ParserContext parserContext) {
|
||||
ManagedList<Object> list = new ManagedList<Object>();
|
||||
list.setSource(parserContext.extractSource(parentElement));
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
A path that maps a particular request to a handler.
|
||||
Exact path mapping URIs (such as "/myPath") are supported as well as Ant-stype path patterns (such as /myPath/**).
|
||||
Exact path mapping URIs (such as "/myPath") are supported as well as Ant-type path patterns (such as /myPath/**).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
@@ -916,9 +916,16 @@
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The bean name of the UrlPathHelper to use for the HandlerMapping used to map handshake requests.
|
||||
]]></xsd:documentation>
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="validator" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The bean name of the Validator instance used for validating @Payload arguments.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
|
||||
Reference in New Issue
Block a user