For folks who may not be familiar with Guava, it is a framework of really helpful utilities that can be used for a variety of situations. Most folks who use Guava in my experience use the collections classes. However, there is a boon for anyone who digs a little deeper.
We will use two specific classes from the Guava framework to do our validations. The first is
InternetDomainName
which is used to validate the domain name. The other is InetAddresses
to check our IP address for validity.There are some caveats to the
InternetDomainName
class which are explained here: InternetDomainNameExplained.
In the code below, we see that it is very easy to use and it works very well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 <jyeary@bluelotussoftware.com> | |
* @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)); | |
} | |
} |
1 2 3 4 5 6 7 8 | 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 |
0 comments :
Post a Comment