Java Code Examples for org.sonatype.nexus.rest.ValidationErrorsException#withError()

The following examples show how to use org.sonatype.nexus.rest.ValidationErrorsException#withError() . 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: ValidationErrorsResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@PUT
@Path("/manual/multiple")
@Consumes({APPLICATION_XML, APPLICATION_JSON})
@Produces({APPLICATION_XML, APPLICATION_JSON})
public UserXO putWithMultipleManualValidations(final UserXO user) {
  log.info("PUT user: {}", user);

  final ValidationErrorsException validationErrors = new ValidationErrorsException();
  if (user.getName() == null) {
    validationErrors.withError("name", "Name cannot be null");
  }
  if (user.getDescription() == null) {
    validationErrors.withError("description", "Description cannot be null");
  }

  if (validationErrors.hasValidationErrors()) {
    throw validationErrors;
  }

  return user;
}
 
Example 2
Source File: UserApiResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void validateRoles(final Set<String> roleIds) {
  ValidationErrorsException errors = new ValidationErrorsException();

  Set<String> localRoles;
  try {
    localRoles = securitySystem.listRoles(UserManager.DEFAULT_SOURCE).stream().map(r -> r.getRoleId())
        .collect(Collectors.toSet());
    for (String roleId : roleIds) {
      if (!localRoles.contains(roleId)) {
        errors.withError("roles", "Unable to locate roleId: " + roleId);
      }
    }
    if (errors.hasValidationErrors()) {
      throw errors;
    }
  }
  catch (NoSuchAuthorizationManagerException e) {
    log.error("Unable to locate default user manager", e);
    throw createWebException(Status.INTERNAL_SERVER_ERROR, "Unable to locate default user manager");
  }
}
 
Example 3
Source File: OrientRoutingRuleStore.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@VisibleForTesting
static void validate(final RoutingRule rule) { // NOSONAR
  ValidationErrorsException exception = new ValidationErrorsException();

  if (Strings2.isBlank(rule.name())) {
    exception.withError("name", "A non-empty value must be specified");
  }
  else if (!rule.name().matches(NamePatternConstants.REGEX)) {
    exception.withError("name",
        "Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot.");
  }
  else if (rule.name().equalsIgnoreCase(NONE)) {
    exception.withError("name", "Rule must not be named None");
  }

  if (rule.description() == null) {
    exception.withError("description", "A non-null value must be specified");
  }

  if (rule.mode() == null) {
    exception.withError("mode", "A non-empty value must be specified");
  }

  if (rule.matchers() == null || rule.matchers().isEmpty()) {
    exception.withError("matchers", "At least one rule must be specified");
  }
  else {
    int index = 0;
    for (String regex : rule.matchers()) {
      if (Strings2.isBlank(regex)) {
        exception.withError("matchers[" + index + "]", "Empty matchers are not allowed");
      }
      else {
        try {
          Pattern.compile(regex);
        }
        catch (PatternSyntaxException e) { // NOSONAR
          exception.withError("matchers[" + index + "]", "Invalid regex: " + e.getMessage());
        }
      }
      index++;
    }
  }

  if (!exception.getValidationErrors().isEmpty()) {
    throw exception;
  }
}
 
Example 4
Source File: RoutingRuleStoreImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@VisibleForTesting
static RoutingRule validate(final RoutingRule rule) { // NOSONAR
  ValidationErrorsException exception = new ValidationErrorsException();

  if (Strings2.isBlank(rule.name())) {
    exception.withError(NAME, "A non-empty value must be specified");
  }
  else if (!rule.name().matches(NamePatternConstants.REGEX)) {
    exception.withError(NAME,
        "Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot.");
  }
  else if (rule.name().equalsIgnoreCase(NONE)) {
    exception.withError(NAME, "Rule must not be named None");
  }

  if (rule.description() == null) {
    exception.withError(DESCRIPTION, "A non-null value must be specified");
  }

  if (rule.mode() == null) {
    exception.withError(MODE, "A non-empty value must be specified");
  }

  if (rule.matchers() == null || rule.matchers().isEmpty()) {
    exception.withError(MATCHERS, "At least one rule must be specified");
  }
  else {
    int index = 0;
    for (String regex : rule.matchers()) {
      if (Strings2.isBlank(regex)) {
        exception.withError(MATCHERS + "[" + index + "]", "Empty matchers are not allowed");
      }
      else {
        try {
          Pattern.compile(regex);
        }
        catch (PatternSyntaxException e) { // NOSONAR
          exception.withError(MATCHERS + "[" + index + "]", "Invalid regex: " + e.getMessage());
        }
      }
      index++;
    }
  }

  if (!exception.getValidationErrors().isEmpty()) {
    throw exception;
  }

  return rule;
}