All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.vt.middleware.password.PasswordValidator Maven / Gradle / Ivy

The newest version!
/*
  $Id: PasswordValidator.java 2704 2013-04-24 21:30:32Z dfisher $

  Copyright (C) 2003-2013 Virginia Tech.
  All rights reserved.

  SEE LICENSE FOR MORE INFORMATION

  Author:  Middleware Services
  Email:   [email protected]
  Version: $Revision: 2704 $
  Updated: $Date: 2013-04-24 17:30:32 -0400 (Wed, 24 Apr 2013) $
*/
package edu.vt.middleware.password;

import java.util.ArrayList;
import java.util.List;

/**
 * The central component for evaluating multiple password rules against a
 * candidate password.
 *
 * @author  Middleware Services
 * @version  $Revision: 2704 $ $Date: 2013-04-24 17:30:32 -0400 (Wed, 24 Apr 2013) $
 */

public class PasswordValidator implements Rule
{

  /** Password rules. */
  private final List passwordRules;

  /** Message resolver. */
  private final MessageResolver messageResolver;


  /**
   * Creates a new password validator with the default message resolver.
   *
   * @param  rules  to validate
   */
  public PasswordValidator(final List rules)
  {
    this(new MessageResolver(), rules);
  }


  /**
   * Creates a new password validator.
   *
   * @param  resolver  message resolver.
   * @param  rules  to validate
   */
  public PasswordValidator(
    final MessageResolver resolver,
    final List rules)
  {
    messageResolver = resolver;
    passwordRules = rules;
  }


  /**
   * Validates the supplied password data against the rules in this validator.
   *
   * @param  passwordData  to validate
   *
   * @return  rule result
   */
  public RuleResult validate(final PasswordData passwordData)
  {
    final RuleResult result = new RuleResult(true);
    for (Rule rule : passwordRules) {
      final RuleResult rr = rule.validate(passwordData);
      if (!rr.isValid()) {
        result.setValid(false);
        result.getDetails().addAll(rr.getDetails());
      }
    }
    return result;
  }


  /**
   * Returns a list of human-readable messages by iterating over the details in
   * a failed rule result.
   *
   * @param  result  failed rule result.
   *
   * @return  list of human-readable messages describing the reason(s) for
   * validation failure.
   */
  public List getMessages(final RuleResult result)
  {
    final List messages = new ArrayList();
    for (RuleResultDetail detail : result.getDetails()) {
      messages.add(messageResolver.resolve(detail));
    }
    return messages;
  }


  /** {@inheritDoc} */
  @Override
  public String toString()
  {
    return
      String.format(
        "%s@%h::passwordRules=%s,messageResolver=%s",
        getClass().getName(),
        hashCode(),
        passwordRules,
        messageResolver);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy