Package org.sonatype.configuration.validation

Examples of org.sonatype.configuration.validation.ValidationResponse


  public String generateId() {
    return Long.toHexString(System.nanoTime() + rand.nextInt(2008));
  }

  public ValidationResponse validateModel(ValidationRequest request) {
    ValidationResponse response = new ApplicationValidationResponse();

    response.setContext(new ApplicationValidationContext());

    Configuration model = (Configuration) request.getConfiguration();

    ApplicationValidationContext context = (ApplicationValidationContext) response.getContext();

    response.setContext(context);

    // global conn settings
    if (model.getGlobalConnectionSettings() != null) {
      response.append(validateRemoteConnectionSettings(context, model.getGlobalConnectionSettings()));
    }
    else {
      model.setGlobalConnectionSettings(new CRemoteConnectionSettings());

      response
          .addValidationWarning(
              "Global connection settings block, which is mandatory, was missing. Reset with defaults.");

      response.setModified(true);
    }

    // remote proxy settings (optional)
    final CRemoteProxySettings rps = model.getRemoteProxySettings();
    if (rps != null) {
      if (rps.getHttpProxySettings() != null) {
        response.append(validateRemoteHttpProxySettings(context, rps.getHttpProxySettings()));
      }
      if (rps.getHttpsProxySettings() != null) {
        response.append(validateRemoteHttpProxySettings(context, rps.getHttpsProxySettings()));
      }
    }

    // rest api
    if (model.getRestApi() != null) {
      response.append(validateRestApiSettings(context, model.getRestApi()));
    }

    // nexus built-in http proxy
    if (model.getHttpProxy() != null) {
      response.append(validateHttpProxySettings(context, model.getHttpProxy()));
    }
    else {
      model.setHttpProxy(new CHttpProxySettings());

      response.addValidationWarning("The HTTP Proxy section was missing from configuration, defaulted it.");

      response.setModified(true);
    }

    // routing
    if (model.getRouting() != null) {
      response.append(validateRouting(context, model.getRouting()));
    }
    else {
      model.setRouting(new CRouting());

      response.addValidationWarning("The routing section was missing from configuration, defaulted it.");

      response.setModified(true);
    }

    // check existing reposes and check their realms
    context.addExistingRepositoryIds();

    List<CRepository> reposes = model.getRepositories();

    for (CRepository repo : reposes) {
      response.append(validateRepository(context, repo));
    }

    // check groups (optional section)
    if (model.getRepositoryGrouping() != null) {
      response.append(validateRepositoryGrouping(context, model.getRepositoryGrouping()));
    }

    // check remote nexus instances (optional section)
    if (model.getRemoteNexusInstances() != null) {
      List<CRemoteNexusInstance> instances = model.getRemoteNexusInstances();

      for (CRemoteNexusInstance instance : instances) {
        response.append(validateRemoteNexusInstance(context, instance));
      }
    }

    // check repo targets (optional section)
    if (model.getRepositoryTargets() != null) {
      List<CRepositoryTarget> targets = model.getRepositoryTargets();

      for (CRepositoryTarget target : targets) {
        response.append(validateRepositoryTarget(context, target));
      }
    }

    // check tasks (optional section)
    if (model.getTasks() != null) {
      List<CScheduledTask> tasks = model.getTasks();

      for (CScheduledTask task : tasks) {
        response.append(validateScheduledTask(context, task));
      }
    }

    response.append(validateSmtpConfiguration(context, model.getSmtpConfiguration()));

    return response;
  }
View Full Code Here


  {
    if (context == null) {
      context = initializeContext();
    }

    ValidationResponse vr = validator.validateUser(context, user, roles, true);

    if (vr.isValid()) {
      deleteUser(user.getId());
      getConfiguration().addUser(user);
      this.createOrUpdateUserRoleMapping(this.buildUserRoleMapping(user.getId(), roles));
      logValidationWarnings(vr);
    }
View Full Code Here

    try {
      // this will throw a NoSuchRoleMappingException, if there isn't one
      readUserRoleMapping(userRoleMapping.getUserId(), userRoleMapping.getSource());

      ValidationResponse vr = new ValidationResponse();
      vr.addValidationError(new ValidationMessage("*", "User Role Mapping for user '"
          + userRoleMapping.getUserId() + "' already exists."));

      throw new InvalidConfigurationException(vr);
    }
    catch (NoSuchRoleMappingException e) {
      // expected
    }

    ValidationResponse vr = validator.validateUserRoleMapping(context, userRoleMapping, false);

    if (vr.getValidationErrors().size() > 0) {
      throw new InvalidConfigurationException(vr);
    }

    getConfiguration().addUserRoleMapping(userRoleMapping);
    logValidationWarnings(vr);
View Full Code Here

    if (context == null) {
      context = initializeContext();
    }

    if (readUserRoleMapping(userRoleMapping.getUserId(), userRoleMapping.getSource()) == null) {
      ValidationResponse vr = new ValidationResponse();
      vr.addValidationError(new ValidationMessage("*", "No User Role Mapping found for user '"
          + userRoleMapping.getUserId() + "'."));

      throw new InvalidConfigurationException(vr);
    }

    ValidationResponse vr = validator.validateUserRoleMapping(context, userRoleMapping, true);

    if (vr.getValidationErrors().size() > 0) {
      throw new InvalidConfigurationException(vr);
    }

    deleteUserRoleMapping(userRoleMapping.getUserId(), userRoleMapping.getSource());
    getConfiguration().addUserRoleMapping(userRoleMapping);
View Full Code Here

    }
  }

  protected ValidationResponse doValidateChanges(C changedConfiguration) {
    // This method should be overridden to perform validation on underlying model
    return new ValidationResponse();
  }
View Full Code Here

    return permission + ":" + method;
  }

  @Override
  public ValidationResponse validatePrivilege(CPrivilege privilege, SecurityValidationContext ctx, boolean update) {
    ValidationResponse response = super.validatePrivilege(privilege, ctx, update);

    if (!TYPE.equals(privilege.getType())) {
      return response;
    }

    // validate method
    // method is of form ('*' | 'read' | 'create' | 'update' | 'delete' [, method]* )
    // so, 'read' method is correct, but so is also 'create,update,delete'
    // '*' means ALL POSSIBLE value for this "field"
    String method = null;
    String permission = null;

    for (CProperty property : (List<CProperty>) privilege.getProperties()) {
      if (property.getKey().equals(ApplicationPrivilegeMethodPropertyDescriptor.ID)) {
        method = property.getValue();
      }
      else if (property.getKey().equals(ApplicationPrivilegePermissionPropertyDescriptor.ID)) {
        permission = property.getValue();
      }
    }

    if (StringUtils.isEmpty(permission)) {
      response.addValidationError("Permission cannot be empty on a privilege!");
    }

    if (StringUtils.isEmpty(method)) {
      response.addValidationError("Method cannot be empty on a privilege!");
    }
    else {
      String[] methods = null;

      if (method.contains(",")) {
        // it is a list of methods
        methods = method.split(",");
      }
      else {
        // it is a single method
        methods = new String[]{method};
      }

      boolean valid = true;

      for (String singlemethod : methods) {
        if (!"create".equals(singlemethod) && !"delete".equals(singlemethod)
            && !"read".equals(singlemethod) && !"update".equals(singlemethod) && !"*".equals(singlemethod)) {
          valid = false;

          break;
        }
      }

      if (!valid) {
        ValidationMessage message =
            new ValidationMessage("method", "Privilege ID '" + privilege.getId()
                + "' Method is wrong! (Allowed methods are: create, delete, read and update)",
                "Invalid method selected.");
        response.addValidationError(message);
      }
    }

    return response;
  }
View Full Code Here

  // ---------------
  // Public

  @Override
  public ValidationResponse validateRepository(ApplicationValidationContext ctx, CRepository repo) {
    ValidationResponse response = new ApplicationValidationResponse();

    if (ctx != null) {
      response.setContext(ctx);
    }

    ApplicationValidationContext context = (ApplicationValidationContext) response.getContext();

    if (StringUtils.isEmpty(repo.getId())) {
      response.addValidationError(new ValidationMessage("id", "Repository ID's may not be empty!"));
    }
    else if (!repo.getId().matches(REPOSITORY_ID_PATTERN)) {
      response.addValidationError(
          new ValidationMessage("id",
              "Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed in Repository ID"));
    }
    // if repo id isn't valid, nothing below here will validate properly
    else {
      if (StringUtils.isEmpty(repo.getName())) {
        repo.setName(repo.getId());

        response.addValidationWarning(new ValidationMessage("id", "Repository with ID='" + repo.getId()
            + "' has no name, defaulted to it's ID."));

        response.setModified(true);
      }

      if (!validateLocalStatus(repo.getLocalStatus())) {
        response.addValidationError(
            new ValidationMessage("id", "LocalStatus of repository with ID='" + repo.getId()) + "' is wrong "
                + repo.getLocalStatus() + "! (Allowed values are: '" + LocalStatus.IN_SERVICE + "' and '"
                + LocalStatus.OUT_OF_SERVICE + "')");
      }
      if (context.getExistingRepositoryIds() != null) {
        if (context.getExistingRepositoryIds().contains(repo.getId())) {
          response.addValidationError(
              new ValidationMessage("id", "Repository with ID=" + repo.getId() + " already exists!"));
        }

        context.getExistingRepositoryIds().add(repo.getId());
      }

      if (context.getExistingRepositoryShadowIds() != null) {
        if (context.getExistingRepositoryShadowIds().contains(repo.getId())) {
          response.addValidationError(new ValidationMessage("id", "Repository " + repo.getId()
              + " conflicts with existing Shadow with same ID='" + repo.getId() + "'!"));
        }
      }

      if (context.getExistingRepositoryGroupIds() != null) {
        if (context.getExistingRepositoryGroupIds().contains(repo.getId())) {
          response.addValidationError(new ValidationMessage("id", "Repository " + repo.getId()
              + " conflicts with existing Group with same ID='" + repo.getId() + "'!"));
        }
      }
    }
View Full Code Here

    catch (IncompatibleMasterRepositoryException e) {
      ValidationMessage message =
          new ValidationMessage("shadowOf", e.getMessage(),
              "The source nexus repository is of an invalid Format.");

      ValidationResponse response = new ApplicationValidationResponse();

      response.addValidationError(message);

      throw new InvalidConfigurationException(response);
    }
    catch (NoSuchRepositoryException e) {
      ValidationMessage message =
          new ValidationMessage("shadowOf", e.getMessage(), "The source nexus repository is not existing.");

      ValidationResponse response = new ApplicationValidationResponse();

      response.addValidationError(message);

      throw new InvalidConfigurationException(response);
    }
  }
View Full Code Here

        else {
          prepository.setRemoteStorage(null);
        }
      }
      catch (RemoteStorageException e) {
        ValidationResponse response = new ApplicationValidationResponse();

        ValidationMessage error = new ValidationMessage("remoteStorageUrl", e.getMessage(), e.getMessage());

        response.addValidationError(error);

        throw new InvalidConfigurationException(response);
      }
    }
  }
View Full Code Here

  }

  protected void validate(CPathMappingItem pathItem)
      throws InvalidConfigurationException
  {
    ValidationResponse response = this.validator.validateGroupsSettingPathMappingItem(null, pathItem);
    if (!response.isValid()) {
      throw new InvalidConfigurationException(response);
    }
  }
View Full Code Here

TOP

Related Classes of org.sonatype.configuration.validation.ValidationResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.