下面列出了org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl#org.springframework.beans.ConfigurablePropertyAccessor 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StringUtils.isBlank(value)) {
return true;
}
List<String> sentitiveWords = getContentChecker().check(value);
if (LangUtils.isEmpty(sentitiveWords)) {
return true;
}
ConstraintValidatorContextImpl ctx = (ConstraintValidatorContextImpl)context;
// 这种方法虽然可以动态显示错误信息,但是无法把错误信息放到i18n国际化资源文件里
// ctx.addExpressionVariable("invalidWords", StringUtils.join(sentitiveWords, ", "));
// ctx.buildConstraintViolationWithTemplate("${invalidWords}").addConstraintViolation();
Map<String, Object> newAttributes = Maps.newHashMap(ctx.getConstraintDescriptor().getAttributes());
newAttributes.put("invalidWords", StringUtils.join(sentitiveWords, ", "));
ConfigurablePropertyAccessor constraintDescriptor = SpringUtils.newPropertyAccessor(ctx.getConstraintDescriptor(), true);
constraintDescriptor.setPropertyValue("attributes", newAttributes);
return false;
}
/**
* Returns the DirectFieldAccessor that this instance uses.
* Creates a new one if none existed before.
* @see #createDirectFieldAccessor()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.directFieldAccessor == null) {
this.directFieldAccessor = createDirectFieldAccessor();
this.directFieldAccessor.setExtractOldValueForEditor(true);
this.directFieldAccessor.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
}
return this.directFieldAccessor;
}
/**
* Create a new DirectFieldAccessor for the underlying target object.
* @see #getTarget()
*/
protected ConfigurablePropertyAccessor createDirectFieldAccessor() {
if (this.target == null) {
throw new IllegalStateException("Cannot access fields on null target instance '" + getObjectName() + "'");
}
return PropertyAccessorFactory.forDirectFieldAccess(this.target);
}
/**
* Returns the {@link BeanWrapper} that this instance uses.
* Creates a new one if none existed before.
* @see #createBeanWrapper()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.beanWrapper == null) {
this.beanWrapper = createBeanWrapper();
this.beanWrapper.setExtractOldValueForEditor(true);
this.beanWrapper.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
this.beanWrapper.setAutoGrowCollectionLimit(this.autoGrowCollectionLimit);
}
return this.beanWrapper;
}
/**
* Returns the DirectFieldAccessor that this instance uses.
* Creates a new one if none existed before.
* @see #createDirectFieldAccessor()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.directFieldAccessor == null) {
this.directFieldAccessor = createDirectFieldAccessor();
this.directFieldAccessor.setExtractOldValueForEditor(true);
this.directFieldAccessor.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
}
return this.directFieldAccessor;
}
/**
* Create a new DirectFieldAccessor for the underlying target object.
* @see #getTarget()
*/
protected ConfigurablePropertyAccessor createDirectFieldAccessor() {
if (this.target == null) {
throw new IllegalStateException("Cannot access fields on null target instance '" + getObjectName() + "'");
}
return PropertyAccessorFactory.forDirectFieldAccess(this.target);
}
/**
* Returns the {@link BeanWrapper} that this instance uses.
* Creates a new one if none existed before.
* @see #createBeanWrapper()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.beanWrapper == null) {
this.beanWrapper = createBeanWrapper();
this.beanWrapper.setExtractOldValueForEditor(true);
this.beanWrapper.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
this.beanWrapper.setAutoGrowCollectionLimit(this.autoGrowCollectionLimit);
}
return this.beanWrapper;
}
/**
* Returns the DirectFieldAccessor that this instance uses.
* Creates a new one if none existed before.
* @see #createDirectFieldAccessor()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.directFieldAccessor == null) {
this.directFieldAccessor = createDirectFieldAccessor();
this.directFieldAccessor.setExtractOldValueForEditor(true);
this.directFieldAccessor.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
}
return this.directFieldAccessor;
}
/**
* Returns the {@link BeanWrapper} that this instance uses.
* Creates a new one if none existed before.
* @see #createBeanWrapper()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.beanWrapper == null) {
this.beanWrapper = createBeanWrapper();
this.beanWrapper.setExtractOldValueForEditor(true);
this.beanWrapper.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
this.beanWrapper.setAutoGrowCollectionLimit(this.autoGrowCollectionLimit);
}
return this.beanWrapper;
}
/**
* Returns the DirectFieldAccessor that this instance uses.
* Creates a new one if none existed before.
* @see #createDirectFieldAccessor()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.directFieldAccessor == null) {
this.directFieldAccessor = createDirectFieldAccessor();
this.directFieldAccessor.setExtractOldValueForEditor(true);
this.directFieldAccessor.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
}
return this.directFieldAccessor;
}
/**
* Returns the {@link BeanWrapper} that this instance uses.
* Creates a new one if none existed before.
* @see #createBeanWrapper()
*/
@Override
public final ConfigurablePropertyAccessor getPropertyAccessor() {
if (this.beanWrapper == null) {
this.beanWrapper = createBeanWrapper();
this.beanWrapper.setExtractOldValueForEditor(true);
this.beanWrapper.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
this.beanWrapper.setAutoGrowCollectionLimit(this.autoGrowCollectionLimit);
}
return this.beanWrapper;
}
public void mapToObject(Object obj) {
if(config==null || config.isEmpty()){
return ;
}
boolean hasPrefix = StringUtils.isNotBlank(prefix);
ConfigurablePropertyAccessor bw = beanAccessors.createAccessor(obj);
Enumeration<?> names = config.propertyNames();
while(names.hasMoreElements()){
String propertyName = names.nextElement().toString();
String value = config.getProperty(propertyName);
if(value==null){
continue;
}
if(StringUtils.isBlank(value) && ignoreBlankString){
continue;
}
if(hasPrefix){
if(propertyName.startsWith(prefix)){
propertyName = propertyName.substring(prefix.length());
setPropertyValue(obj, bw, propertyName, value);
}
}else{
setPropertyValue(obj, bw, propertyName, value);
}
}
}
protected void setPropertyValue(Object obj, ConfigurablePropertyAccessor bw, String propertyName, Object value){
if(!bw.isWritableProperty(propertyName)){
if(!ignoreNotFoundProperty){
throw new NoSuchElementException("no setter found for property: " + propertyName+", target: " + obj);
}
logger.debug("ignore property: {}={} ", propertyName, value);
return ;
}
bw.setPropertyValue(propertyName, value);
if(logger.isDebugEnabled()){
logger.debug("set property: {}={} ", propertyName, value);
}
}
public void mapToObject(Object obj) {
if(config==null || config.isEmpty()){
return ;
}
boolean hasPrefix = StringUtils.isNotBlank(prefix);
ConfigurablePropertyAccessor bw = beanAccessors.createAccessor(obj);
// Enumeration<?> names = config.propertyNames();
// while(names.hasMoreElements()){
for(Entry<String, Object> entry : config.entrySet()){
// String propertyName = names.nextElement().toString();
// String value = config.getProperty(propertyName);
if(entry.getValue()==null){
continue;
}
String propertyName = entry.getKey();
String text = entry.getValue().toString();
if(StringUtils.isBlank(text) && ignoreBlankString){
continue;
}
if(hasPrefix){
if(propertyName.startsWith(prefix)){
propertyName = propertyName.substring(prefix.length());
setPropertyValue(obj, bw, propertyName, text);
}
}else{
setPropertyValue(obj, bw, propertyName, text);
}
}
}
protected void setPropertyValue(Object obj, ConfigurablePropertyAccessor bw, String propertyName, Object value){
if(!bw.isWritableProperty(propertyName)){
if(!ignoreNotFoundProperty){
throw new NoSuchElementException("no setter found for property: " + propertyName+", target: " + obj);
}
logger.debug("ignore property: {}={} ", propertyName, value);
return ;
}
bw.setPropertyValue(propertyName, value);
if(logger.isDebugEnabled()){
logger.debug("set property: {}={} ", propertyName, value);
}
}
@Override
public <T> T enhanceTargeter(FeignTargetContext<T> ctx) {
FeignClientFactoryBean factory = ctx.getFeignClientfactory();
String serviceName = factory.getName();
if (feignProperties.getServices().containsKey(serviceName)) {
ServiceProps serviceProp = feignProperties.getServices().get(serviceName);
// factory.setUrl(serviceProp.getUrl()); // 这里设置太迟了
ConfigurablePropertyAccessor bw = SpringUtils.newPropertyAccessor(ctx.getHardeCodetarget(), true);
// bw.setPropertyValue("url", serviceProp.getUrl());
}
return ctx.createTargeter();
}
@Override
public <O extends ClientCredentialsTokenEndpointFilter> O postProcess(O filter) {
ConfigurablePropertyAccessor filterAccessor = SpringUtils.newPropertyAccessor(filter, true);
if(oauth2ExceptionRenderer!=null){
filterAccessor.setPropertyValue("authenticationEntryPoint.exceptionRenderer", oauth2ExceptionRenderer);
}
/*AuthenticationManager origin = (AuthenticationManager)filterAccessor.getPropertyValue("authenticationManager");
DelegateAuthenticationManager delegate = new DelegateAuthenticationManager(origin);
filter.setAuthenticationManager(delegate);*/
if(tokenEndpointFilterInterceptor!=null){
filter = Proxys.intercept(filter, tokenEndpointFilterInterceptor);
}
return filter;
}
/**
* Return the underlying PropertyAccessor of this binder's BindingResult.
*/
protected ConfigurablePropertyAccessor getPropertyAccessor() {
return getInternalBindingResult().getPropertyAccessor();
}
/**
* Return the underlying PropertyAccessor of this binder's BindingResult.
*/
protected ConfigurablePropertyAccessor getPropertyAccessor() {
return getInternalBindingResult().getPropertyAccessor();
}
/**
* Create a new DirectFieldAccessor for the underlying target object.
* @see #getTarget()
*/
protected ConfigurablePropertyAccessor createDirectFieldAccessor() {
Assert.state(this.target != null, "Cannot access fields on null target instance '" + getObjectName() + "'!");
return PropertyAccessorFactory.forDirectFieldAccess(this.target);
}
/**
* Return the underlying PropertyAccessor of this binder's BindingResult.
*/
protected ConfigurablePropertyAccessor getPropertyAccessor() {
return getInternalBindingResult().getPropertyAccessor();
}
/**
* Create a new DirectFieldAccessor for the underlying target object.
* @see #getTarget()
*/
protected ConfigurablePropertyAccessor createDirectFieldAccessor() {
Assert.state(this.target != null, "Cannot access fields on null target instance '" + getObjectName() + "'!");
return PropertyAccessorFactory.forDirectFieldAccess(this.target);
}
/**
* Return the underlying PropertyAccessor of this binder's BindingResult.
*/
protected ConfigurablePropertyAccessor getPropertyAccessor() {
return getInternalBindingResult().getPropertyAccessor();
}
@Override
public ConfigurablePropertyAccessor createAccessor(Object obj) {
ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forBeanPropertyAccess(obj);
accessor.setAutoGrowNestedPaths(true);
return accessor;
}
@Override
public ConfigurablePropertyAccessor createAccessor(Object obj) {
ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(obj);
accessor.setAutoGrowNestedPaths(true);
return accessor;
}
public static ConfigurablePropertyAccessor newPropertyAccessor(Object obj, boolean directFieldAccess){
ConfigurablePropertyAccessor bw = directFieldAccess?PropertyAccessorFactory.forDirectFieldAccess(obj):PropertyAccessorFactory.forBeanPropertyAccess(obj);
bw.setAutoGrowNestedPaths(true);
return bw;
}
private boolean isNullAndWritableProperty(ConfigurablePropertyAccessor accessor, String propertyName) {
return accessor.isWritableProperty(propertyName) && (accessor.isReadableProperty(propertyName) && accessor.getPropertyValue(propertyName) == null);
}
/**
* Provide the PropertyAccessor to work with, according to the
* concrete strategy of access.
* <p>Note that a PropertyAccessor used by a BindingResult should
* always have its "extractOldValueForEditor" flag set to "true"
* by default, since this is typically possible without side effects
* for model objects that serve as data binding target.
* @see ConfigurablePropertyAccessor#setExtractOldValueForEditor
*/
public abstract ConfigurablePropertyAccessor getPropertyAccessor();
/**
* Provide the PropertyAccessor to work with, according to the
* concrete strategy of access.
* <p>Note that a PropertyAccessor used by a BindingResult should
* always have its "extractOldValueForEditor" flag set to "true"
* by default, since this is typically possible without side effects
* for model objects that serve as data binding target.
* @see ConfigurablePropertyAccessor#setExtractOldValueForEditor
*/
public abstract ConfigurablePropertyAccessor getPropertyAccessor();
/**
* Provide the PropertyAccessor to work with, according to the
* concrete strategy of access.
* <p>Note that a PropertyAccessor used by a BindingResult should
* always have its "extractOldValueForEditor" flag set to "true"
* by default, since this is typically possible without side effects
* for model objects that serve as data binding target.
* @see ConfigurablePropertyAccessor#setExtractOldValueForEditor
*/
public abstract ConfigurablePropertyAccessor getPropertyAccessor();