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 018 package org.apache.commons.validator.routines; 019 020 import 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: 594917 $ 032 * @since Validator 1.4 033 */ 034 public class InetAddressValidator implements Serializable { 035 036 private static final String IPV4_REGEX = 037 "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; 038 039 /** 040 * Singleton instance of this class. 041 */ 042 private static final InetAddressValidator VALIDATOR = new InetAddressValidator(); 043 044 /** IPv4 RegexValidator */ 045 private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX); 046 047 /** 048 * Returns the singleton instance of this validator. 049 * @return the singleton instance of this validator 050 */ 051 public static InetAddressValidator getInstance() { 052 return VALIDATOR; 053 } 054 055 /** 056 * Checks if the specified string is a valid IP address. 057 * @param inetAddress the string to validate 058 * @return true if the string validates as an IP address 059 */ 060 public boolean isValid(String inetAddress) { 061 return isValidInet4Address(inetAddress); 062 } 063 064 /** 065 * Validates an IPv4 address. Returns true if valid. 066 * @param inet4Address the IPv4 address to validate 067 * @return true if the argument contains a valid IPv4 address 068 */ 069 public boolean isValidInet4Address(String inet4Address) { 070 // verify that address conforms to generic IPv4 format 071 String[] groups = ipv4Validator.match(inet4Address); 072 073 if (groups == null) return false; 074 075 // verify that address subgroups are legal 076 for (int i = 0; i <= 3; i++) { 077 String ipSegment = groups[i]; 078 if (ipSegment == null || ipSegment.length() <= 0) { 079 return false; 080 } 081 082 int iIpSegment = 0; 083 084 try { 085 iIpSegment = Integer.parseInt(ipSegment); 086 } catch(NumberFormatException e) { 087 return false; 088 } 089 090 if (iIpSegment > 255) { 091 return false; 092 } 093 094 } 095 096 return true; 097 } 098 }