001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.validator.routines;
019
020import java.io.Serializable;
021
022/**
023 * <p><b>InetAddress</b> validation and conversion routines (<code>java.net.InetAddress</code>).</p>
024 *
025 * <p>This class provides methods to validate a candidate IP address.
026 *
027 * <p>
028 * This class is a Singleton; you can retrieve the instance via the {@link #getInstance()} method.
029 * </p>
030 *
031 * @version $Revision: 1227719 $
032 * @since Validator 1.4
033 */
034public class InetAddressValidator implements Serializable {
035
036    private static final long serialVersionUID = -919201640201914789L;
037
038    private static final String IPV4_REGEX =
039            "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
040
041    /**
042     * Singleton instance of this class.
043     */
044    private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
045
046    /** IPv4 RegexValidator */
047    private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX);
048
049    /**
050     * Returns the singleton instance of this validator.
051     * @return the singleton instance of this validator
052     */
053    public static InetAddressValidator getInstance() {
054        return VALIDATOR;
055    }
056
057    /**
058     * Checks if the specified string is a valid IP address.
059     * @param inetAddress the string to validate
060     * @return true if the string validates as an IP address
061     */
062    public boolean isValid(String inetAddress) {
063        return isValidInet4Address(inetAddress);
064    }
065
066    /**
067     * Validates an IPv4 address. Returns true if valid.
068     * @param inet4Address the IPv4 address to validate
069     * @return true if the argument contains a valid IPv4 address
070     */
071    public boolean isValidInet4Address(String inet4Address) {
072        // verify that address conforms to generic IPv4 format
073        String[] groups = ipv4Validator.match(inet4Address);
074
075        if (groups == null) return false;
076
077        // verify that address subgroups are legal
078        for (int i = 0; i <= 3; i++) {
079            String ipSegment = groups[i];
080            if (ipSegment == null || ipSegment.length() <= 0) {
081                return false;
082            }
083
084            int iIpSegment = 0;
085
086            try {
087                iIpSegment = Integer.parseInt(ipSegment);
088            } catch(NumberFormatException e) {
089                return false;
090            }
091
092            if (iIpSegment > 255) {
093                return false;
094            }
095
096        }
097
098        return true;
099    }
100}