Skip to content

Instantly share code, notes, and snippets.

@jyeary
Last active November 21, 2019 23:57
Show Gist options
  • Save jyeary/d06f9980eb620216ff3a to your computer and use it in GitHub Desktop.
Save jyeary/d06f9980eb620216ff3a to your computer and use it in GitHub Desktop.
An example of how to use Guava to validate hostnames, and IP addresses.
/*
* Copyright 2015-2016 Blue Lotus Software, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bluelotussoftware.example;
import com.google.common.net.InetAddresses;
import com.google.common.net.InternetDomainName;
import java.text.MessageFormat;
/**
*
* @author John Yeary <[email protected]>
* @version 1.0
*/
public class HostIPValidation {
public static void main(String[] args) {
String hostname = "www.bluelotussoftware.com";
boolean result = InternetDomainName.isValid(hostname);
System.out.println(MessageFormat.format("Is Internet Domain Name: {0} valid? {1}", hostname, result));
hostname = "bluelotussoftware.com";
result = InternetDomainName.isValid(hostname);
System.out.println(MessageFormat.format("Is Internet Domain Name: {0} valid? {1}", hostname, result));
hostname = "bluelotussoftware";
result = InternetDomainName.isValid(hostname);
System.out.println(MessageFormat.format("Is Internet Domain Name: {0} valid? {1}", hostname, result));
hostname = "256.0.0.0";
result = InternetDomainName.isValid(hostname);
System.out.println(MessageFormat.format("Is Internet Domain Name: {0} valid? {1}", hostname, result));
result = InetAddresses.isInetAddress(hostname);
System.out.println(MessageFormat.format("Is Internet Address: {0} valid? {1}", hostname, result));
hostname = "255.255.255.0";
result = InternetDomainName.isValid(hostname);
System.out.println(MessageFormat.format("Is Internet Domain Name: {0} valid? {1}", hostname, result));
result = InetAddresses.isInetAddress(hostname);
System.out.println(MessageFormat.format("Is Internet Address: {0} valid? {1}", hostname, result));
hostname = "_xyz.bluelotussoftware.com";
result = InternetDomainName.isValid(hostname);
System.out.println(MessageFormat.format("Is Internet Domain Name: {0} valid? {1}", hostname, result));
}
}
@tvhung83
Copy link

If you're lazy, here is the output (Guava 18.0):

Is Internet Domain Name: www.bluelotussoftware.com valid? true
Is Internet Domain Name: bluelotussoftware.com valid? true
Is Internet Domain Name: bluelotussoftware valid? true
Is Internet Domain Name: 256.0.0.0 valid? false
Is Internet Address: 256.0.0.0 valid? false
Is Internet Domain Name: 255.255.255.0 valid? false
Is Internet Address: 255.255.255.0 valid? true
Is Internet Domain Name: _xyz.bluelotussoftware.com valid? false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment