Search in sources :

Example 21 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class UserController method deleteUserAuthorities.

@RestAccessControl(permission = Permission.MANAGE_USERS)
@RequestMapping(value = "/{target:.+}/authorities", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SimpleRestResponse> deleteUserAuthorities(@ModelAttribute("user") UserDetails user, @PathVariable String target) throws ApsSystemException {
    logger.debug("user {} requesting delete authorities for username {}", user.getUsername(), target);
    DataBinder binder = new DataBinder(target);
    BindingResult bindingResult = binder.getBindingResult();
    // field validations
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
    // business validations
    getUserValidator().validateUpdateSelf(target, user.getUsername(), bindingResult);
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
    this.getUserService().deleteUserAuthorities(target);
    return new ResponseEntity<>(new SimpleRestResponse<>(new ArrayList<>()), HttpStatus.OK);
}
Also used : BindingResult(org.springframework.validation.BindingResult) ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) DataBinder(org.springframework.validation.DataBinder) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class UserValidator method createDeleteAdminError.

public static BindingResult createDeleteAdminError() {
    Map<String, String> map = new HashMap<>();
    map.put("username", "admin");
    BindingResult bindingResult = new MapBindingResult(map, "username");
    bindingResult.reject(UserValidator.ERRCODE_DELETE_ADMIN, new String[] {}, "user.admin.cant.delete");
    return bindingResult;
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) HashMap(java.util.HashMap) MapBindingResult(org.springframework.validation.MapBindingResult)

Example 23 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class EntityAttributeDto method fillEntityAttribute.

public void fillEntityAttribute(AttributeInterface attribute, BindingResult bindingResult) {
    if (attribute instanceof ITextAttribute) {
        ITextAttribute textAttribute = (ITextAttribute) attribute;
        if (attribute.isMultilingual() && this.getValues() != null && !this.getValues().isEmpty()) {
            this.getValues().keySet().stream().forEach(langCode -> textAttribute.setText(this.getValues().get(langCode).toString(), langCode));
        } else if (null != this.getValue()) {
            textAttribute.setText(this.getValue().toString(), null);
        }
    }
    if (attribute instanceof NumberAttribute && (null != this.getValue())) {
        BigDecimal number = new BigDecimal(this.getValue().toString());
        ((NumberAttribute) attribute).setValue(number);
    }
    if (attribute instanceof BooleanAttribute) {
        ((BooleanAttribute) attribute).setBooleanValue((Boolean) this.getValue());
    }
    if (attribute instanceof DateAttribute && (null != this.getValue())) {
        Date date = null;
        String dateValue = null;
        try {
            date = DateConverter.parseDate(this.getValue().toString(), SystemConstants.API_DATE_FORMAT);
        } catch (Exception e) {
            dateValue = this.getValue().toString();
        }
        ((DateAttribute) attribute).setDate(date);
        ((DateAttribute) attribute).setFailedDateString(dateValue);
    }
    if (attribute instanceof CompositeAttribute && (null != this.getCompositeElements())) {
        this.getCompositeElements().stream().forEach(i -> {
            AttributeInterface compositeElement = ((CompositeAttribute) attribute).getAttribute(i.getCode());
            i.fillEntityAttribute(compositeElement, bindingResult);
        });
    } else if (attribute instanceof MonoListAttribute && (null != this.getElements())) {
        this.getElements().stream().forEach(i -> {
            AttributeInterface prototype = ((MonoListAttribute) attribute).addAttribute();
            prototype.setName(((MonoListAttribute) attribute).getName());
            i.fillEntityAttribute(prototype, bindingResult);
        });
    } else if (attribute instanceof ListAttribute && (null != this.getListElements())) {
        ((ListAttribute) attribute).getAttributeListMap().clear();
        this.getListElements().keySet().stream().forEach(langCode -> {
            List<EntityAttributeDto> list = this.getListElements().get(langCode);
            list.stream().forEach(i -> {
                AttributeInterface prototype = ((ListAttribute) attribute).addAttribute(langCode);
                prototype.setName(((ListAttribute) attribute).getName());
                i.fillEntityAttribute(prototype, bindingResult);
            });
        });
    }
}
Also used : ITextAttribute(com.agiletec.aps.system.common.entity.model.attribute.ITextAttribute) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) NumberAttribute(com.agiletec.aps.system.common.entity.model.attribute.NumberAttribute) Logger(org.slf4j.Logger) BooleanAttribute(com.agiletec.aps.system.common.entity.model.attribute.BooleanAttribute) Date(java.util.Date) SystemConstants(com.agiletec.aps.system.SystemConstants) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BindingResult(org.springframework.validation.BindingResult) CompositeAttribute(com.agiletec.aps.system.common.entity.model.attribute.CompositeAttribute) TextAttribute(com.agiletec.aps.system.common.entity.model.attribute.TextAttribute) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) DateAttribute(com.agiletec.aps.system.common.entity.model.attribute.DateAttribute) BigDecimal(java.math.BigDecimal) AbstractMap(java.util.AbstractMap) List(java.util.List) AttributeInterface(com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface) ListAttribute(com.agiletec.aps.system.common.entity.model.attribute.ListAttribute) Map(java.util.Map) MonoListAttribute(com.agiletec.aps.system.common.entity.model.attribute.MonoListAttribute) DateConverter(com.agiletec.aps.util.DateConverter) ITextAttribute(com.agiletec.aps.system.common.entity.model.attribute.ITextAttribute) BooleanAttribute(com.agiletec.aps.system.common.entity.model.attribute.BooleanAttribute) BigDecimal(java.math.BigDecimal) Date(java.util.Date) MonoListAttribute(com.agiletec.aps.system.common.entity.model.attribute.MonoListAttribute) CompositeAttribute(com.agiletec.aps.system.common.entity.model.attribute.CompositeAttribute) NumberAttribute(com.agiletec.aps.system.common.entity.model.attribute.NumberAttribute) AttributeInterface(com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface) ListAttribute(com.agiletec.aps.system.common.entity.model.attribute.ListAttribute) MonoListAttribute(com.agiletec.aps.system.common.entity.model.attribute.MonoListAttribute) DateAttribute(com.agiletec.aps.system.common.entity.model.attribute.DateAttribute)

Example 24 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class ApiConsumerValidator method validateForUpdate.

public void validateForUpdate(String consumerKey, ApiConsumer consumer) {
    if (!consumer.getKey().equals(consumerKey)) {
        DataBinder dataBinder = new DataBinder(consumer);
        BindingResult bindingResult = dataBinder.getBindingResult();
        bindingResult.rejectValue("key", ERRCODE_URINAME_MISMATCH, new String[] { consumerKey, consumer.getKey() }, "api.consumer.key.mismatch");
        throw new ValidationGenericException(bindingResult);
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) DataBinder(org.springframework.validation.DataBinder) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException)

Example 25 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class ApiConsumerValidator method validateForCreate.

/**
 * The secret must be not null for creating the consumer. Instead it can be
 * null during the update.
 */
public void validateForCreate(ApiConsumer consumer) {
    if (StringUtils.isEmpty(consumer.getSecret())) {
        DataBinder dataBinder = new DataBinder(consumer);
        BindingResult bindingResult = dataBinder.getBindingResult();
        bindingResult.rejectValue("secret", "notBlank", new String[] { "secret" }, "string.notBlank");
        throw new ValidationGenericException(bindingResult);
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) DataBinder(org.springframework.validation.DataBinder) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException)

Aggregations

BindingResult (org.springframework.validation.BindingResult)140 Test (org.junit.Test)28 Test (org.junit.jupiter.api.Test)27 List (java.util.List)23 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 ResponseEntity (org.springframework.http.ResponseEntity)20 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)16 ObjectError (org.springframework.validation.ObjectError)16 Model (org.springframework.ui.Model)15 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 HashMap (java.util.HashMap)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13