Apache Commons Validator - Validate VPN (Sub)Domain

I work for a company that has a VPN set up. Computers on the (Windows ActiveDomain/LDAP) network can be accessed either by name (\\machine) or subdomain (\\machine.companyname.tld; eg: ahammerthief.acme.net).

I'm trying to use Apache Commons Validator to recognise machine.companyname.tld as a valid domain/subdomain, despite the fact that it's not available from outside the company in which I work.

Is it possible to do this or is Validator not meant to do this?

The code I have is as follows:

String domain = null, in = JOptionPane.showInputDialog(
    null, "Please enter the domain:", "NTLM/Samba Domain", JOptionPane.QUESTION_MESSAGE
if (null != in && !in.isEmpty()) {
  DomainValidator validator = DomainValidator.getInstance(true);
  // Always returns false. Why?
  if (validator.isValidGenericTld(in) || validator.isValidLocalTld(in)) {
    domain = in;
    UniAddress addr = null;
    try {
      addr = UniAddress.getByName(domain, true);
    } catch (UnknownHostException UHEx) {
      System.err.println("Unknown Host (\"" + domain + "\": " + UHEx.getMessage());
    // ... Ask user for credentials here. Never gets this far.
    // TODO: Use credentials to create/overwrite a jCIFS SMBFile on the network
  } else {
    System.err.println("Entered domain (" + in + ") is invalid!");
} else {
  System.err.println("Entered domain is null or empty!");

The text I'm entering when prompted is of the form subdomain.companyname.tld

I could use a regex to check that there are at least two '.' characters in the supplied string and that they're preceded by at least one character that isn't '.', but I am of the opinion that if Validator has classes for validating domains and URLs, I should be able to use it for this purpose.

UPDATE: I have subsequently looked at the following questions:

  1. How to check validity of a subdomain in Java
  2. How can you check whether domain exists or not in Java?. One of the answers suggests using isValid() from DomainValidator. I will try that to see if it works.

1 Answers Apache Commons Validator - Validate VPN (Sub)Domain

The correct method/function to use for validating a domain/subdomain is validator.isValid(String in), not validator.isValidGenericTld( String in) or validator.isValidLocalTld(String in). Those last two only validate the portion after the last period ('.').

In the above, validator is an instance of DomainValidator.

6 months ago