org.apache.commons.validator.routines.DomainValidator Java Examples

The following examples show how to use org.apache.commons.validator.routines.DomainValidator. 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: DefaultCertificateClient.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a CSR builder that can be used to creates a Certificate signing
 * request.
 *
 * @return CertificateSignRequest.Builder
 */
@Override
public CertificateSignRequest.Builder getCSRBuilder()
    throws CertificateException {
  CertificateSignRequest.Builder builder =
      new CertificateSignRequest.Builder()
      .setConfiguration(securityConfig.getConfiguration());
  try {
    DomainValidator validator = DomainValidator.getInstance();
    // Add all valid ips.
    OzoneSecurityUtil.getValidInetsForCurrentHost().forEach(
        ip -> {
          builder.addIpAddress(ip.getHostAddress());
          if(validator.isValid(ip.getCanonicalHostName())) {
            builder.addDnsName(ip.getCanonicalHostName());
          }
        });
  } catch (IOException e) {
    throw new CertificateException("Error while adding ip to CSR builder",
        e, CSR_ERROR);
  }
  return builder;
}
 
Example #2
Source File: ADMValidator.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the authority contains a valid hostname without "."
 * characters and an optional port number
 * 
 * @param authority
 * @return <code>true</code> if the authority is valid
 */
private boolean isValidAuthorityHostNoDot(String authority) {
    Perl5Util authorityMatcher = new Perl5Util();
    if (authority != null
            && authorityMatcher.match(
                    "/^([a-zA-Z\\d\\-\\.]*)(:\\d*)?(.*)?/", authority)) {
        String hostIP = authorityMatcher.group(1);
        if (hostIP.indexOf('.') < 0) {
            // the hostname contains no dot, add domain validation to check invalid hostname like "g08fnstd110825-"
            DomainValidator domainValidator = DomainValidator.getInstance(true);
            if(!domainValidator.isValid(hostIP)) {
                return false;
            }
            String port = authorityMatcher.group(2);
            if (!isValidPort(port)) {
                return false;
            }
            String extra = authorityMatcher.group(3);
            return GenericValidator.isBlankOrNull(extra);
        } else {
            return false;
        }
    } else {
        return false;
    }
}
 
Example #3
Source File: ADMValidator.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the authority contains a hostname without top level domain
 * and an optional port number
 * 
 * @param authority
 * @return <code>true</code> if the authority is valid
 */
private boolean isValidAuthorityHostNoTld(String authority) {
    Perl5Util authorityMatcher = new Perl5Util();
    if (authority != null
            && authorityMatcher.match(
                    "/^([a-zA-Z\\d\\-\\.]*)(:\\d*)?(.*)?/", authority)) {
        String host = authorityMatcher.group(1);
        if (host.indexOf('.') > 0) {
            DomainValidator domainValidator = DomainValidator.getInstance();
            // Make the host have a valid TLD, so that the "no TLD" host can pass the domain validation.
            String patchedHost = host + ".com";
            if(!domainValidator.isValid(patchedHost)) {
                return false;
            }
            String port = authorityMatcher.group(2);
            if (!isValidPort(port)) {
                return false;
            }
            String extra = authorityMatcher.group(3);
            return GenericValidator.isBlankOrNull(extra);
        } else {
            return false;
        }
    } else {
        return false;
    }
}
 
Example #4
Source File: L3NetworkApiInterceptor.java    From zstack with Apache License 2.0 5 votes vote down vote up
private void validate(APICreateL3NetworkMsg msg) {
    if (!L3NetworkType.hasType(msg.getType())) {
        throw new ApiMessageInterceptionException(argerr("unsupported l3network type[%s]", msg.getType()));
    }

    if (msg.getDnsDomain() != null) {
        DomainValidator validator = DomainValidator.getInstance();
        if (!validator.isValid(msg.getDnsDomain())) {
            throw new ApiMessageInterceptionException(argerr("%s is not a valid domain name", msg.getDnsDomain()));
        }
    }

    List<L3NetworkCategory> validNetworkCategory = Arrays.asList(L3NetworkCategory.values());
    for (L3NetworkCategory category : validNetworkCategory) {
        if (category.toString().equalsIgnoreCase(msg.getCategory())) {
            msg.setCategory(category.toString());
            break;
        }
    }

    if (L3NetworkCategory.checkSystemAndCategory(msg.isSystem(), L3NetworkCategory.valueOf(msg.getCategory()))) {
        return;
    } else {
        throw new ApiMessageInterceptionException(argerr("not valid combination of system and category," +
                "only %s are valid", L3NetworkCategory.validCombination));
    }
}
 
Example #5
Source File: UrlValidator.java    From jsastrawi with MIT License 5 votes vote down vote up
@Override
public boolean isValid(String s) {
    if (s.matches("^.*://.*$")) {
        return org.apache.commons.validator.routines.UrlValidator.getInstance().isValid(s);
    } else {
        String[] parts = s.split("/");

        if (parts.length > 1) {
            return DomainValidator.getInstance().isValid(parts[0]);
        }
    }

    return DomainValidator.getInstance().isValid(s);
}
 
Example #6
Source File: GenerateTLDLists.java    From ache with Apache License 2.0 5 votes vote down vote up
private void printMissing(String variableName, TreeSet<String> tlds, ArrayType tldType) {
    List<String> countryCode = Arrays.asList(DomainValidator.getTLDEntries(tldType));
    StringJoiner str = new StringJoiner(",");
    for (String tld : tlds) {
        if (!countryCode.contains(tld)) {
            str.add("\n\"" + tld + "\"");
        }
    }
    System.out.printf("String[] " + variableName + " = new String[]{" + str.toString() + "};\n\n");
}
 
Example #7
Source File: Validator.java    From mangooio with Apache License 2.0 5 votes vote down vote up
/**
 * Validates a field to be a valid IPv4 address
 *
 * @param name The field to check
 * @param message A custom error message instead of the default one
 */
public void expectDomainName(String name, String message) {
    String value = Optional.ofNullable(get(name)).orElse("");

    if (!DomainValidator.getInstance().isValid(value)) {
        addError(name, Optional.ofNullable(message).orElse(messages.get(Validation.DOMAIN_NAME_KEY.name(), name)));
    }
}
 
Example #8
Source File: ClientRegistrationService.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
public void setAdditionalTLDs(List<String> additionalTLDs) {
    // Support additional top level domains
    if (additionalTLDs != null && !additionalTLDs.isEmpty()) {
        try {
            LOG.info("Adding the following additional Top Level Domains: " + additionalTLDs);
            DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, additionalTLDs.toArray(new String[0]));
        } catch (IllegalStateException ex) {
            //
        }
    }
}
 
Example #9
Source File: CommonsURLValidator.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
public void setAdditionalTLDs(List<String> additionalTLDs) {
    // Support additional top level domains
    if (additionalTLDs != null && !additionalTLDs.isEmpty()) {
        try {
            String[] tldsToAddArray = additionalTLDs.toArray(new String[0]);
            LOG.info("Adding the following additional Top Level Domains: " + Arrays.toString(tldsToAddArray));
            DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, tldsToAddArray);
        } catch (IllegalStateException ex) {
            //
        }
    }
}
 
Example #10
Source File: DefaultProfile.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean validateGeneralName(int type, String value) {
  // TODO : We should add more validation for IP address, for example
  //  it matches the local network, and domain matches where the cluster
  //  exits.
  if (!isSupportedGeneralName(type)) {
    return false;
  }
  switch (type) {
  case GeneralName.iPAddress:

    // We need DatatypeConverter conversion, since the original CSR encodes
    // an IP address int a Hex String, for example 8.8.8.8 is encoded as
    // #08080808. Value string is always preceded by "#", we will strip
    // that before passing it on.

    // getByAddress call converts the IP address to hostname/ipAddress format.
    // if the hostname cannot determined then it will be /ipAddress.

    // TODO: Fail? if we cannot resolve the Hostname?
    try {
      final InetAddress byAddress = InetAddress.getByAddress(
          Hex.decodeHex(value.substring(1)));
      if (LOG.isDebugEnabled()) {
        LOG.debug("Host Name/IP Address : {}", byAddress.toString());
      }
      return true;
    } catch (UnknownHostException | DecoderException e) {
      return false;
    }
  case GeneralName.dNSName:
    return DomainValidator.getInstance().isValid(value);
  case GeneralName.otherName:
    // for other name its a general string, nothing to validate
    return true;
  default:
    // This should not happen, since it guarded via isSupportedGeneralName.
    LOG.error("Unexpected type in General Name (int value) : {}", type);
    return false;
  }
}
 
Example #11
Source File: HostnameFormatValidator.java    From json-schema with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<String> validate(final String subject) {
    return DomainValidator.getInstance(true).isValid(subject) && !subject.contains("_") ?
            Optional.empty() :
            Optional.of(String.format("[%s] is not a valid hostname", subject));
}
 
Example #12
Source File: HostnameValidator.java    From jsastrawi with MIT License 4 votes vote down vote up
@Override
public boolean isValid(String s) {
    return DomainValidator.getInstance().isValid(s);
}
 
Example #13
Source File: DomainValidation.java    From metron with Apache License 2.0 4 votes vote down vote up
@Override
public Predicate<Object> getPredicate() {
  return domain -> DomainValidator.getInstance().isValid(domain == null?null:domain.toString());
}