KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > validators > IPAddressValidator


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.input.validators;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import com.sslexplorer.boot.CodedException;
28 import com.sslexplorer.boot.PropertyDefinition;
29 import com.sslexplorer.core.CoreException;
30 import com.sslexplorer.util.CIDRNetwork;
31
32 /**
33  * OK, I know that a regex expression would be quicker here. The old regex didn't work here and it was too hard to try and get it working.
34  * This was much quicker to write, if anyone wants to change it in the future, the validation rules should be very easy to understand.
35  */

36 public class IPAddressValidator extends StringValidator {
37
38     private static final String JavaDoc LOW_RANGE = "0.0.0.0";
39     private static final String JavaDoc HIGH_RANGE = "255.255.255.255";
40
41     @Override JavaDoc
42     public void validate(PropertyDefinition definition, String JavaDoc value, Properties JavaDoc properties) throws CodedException {
43         super.validate(definition, value, properties);
44         if (!isIpAddressExpressionValid(value)) {
45             throw new CoreException(ErrorConstants.ERR_STRING_ISNT_IP_ADDRESS, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, value);
46         }
47     }
48
49     /**
50      * @param ipAddress
51      * @return true
52      */

53     public static boolean isIpAddressExpressionValid(String JavaDoc ipAddress) {
54         if (LOW_RANGE.equals(ipAddress) || HIGH_RANGE.equals(ipAddress)) {
55             return false;
56         }
57         return isValidAddress(ipAddress) || isValidRegexAddress(ipAddress) || isValidCIDRAddress(ipAddress);
58     }
59
60     private static boolean isValidAddress(String JavaDoc ipAddress) {
61         try {
62             InetAddress.getByName(ipAddress);
63             return containsFourNumbericParts(ipAddress);
64         } catch (UnknownHostException JavaDoc e) {
65             return false;
66         }
67     }
68     
69     private static boolean containsFourNumbericParts(String JavaDoc ipAddress) {
70         int regionCount = 0;
71         for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(ipAddress, "."); tokenizer.hasMoreTokens();) {
72             try {
73                 String JavaDoc token = (String JavaDoc) tokenizer.nextToken();
74                 Integer.parseInt(token);
75                 regionCount++;
76             } catch (NumberFormatException JavaDoc e) {
77                 return false;
78             }
79         }
80         return 4 == regionCount;
81     }
82
83     private static boolean isValidRegexAddress(String JavaDoc ipAddress) {
84         String JavaDoc replacedIpAddress = ipAddress.replace("*", "0");
85         return isValidAddress(replacedIpAddress);
86     }
87
88     private static boolean isValidCIDRAddress(String JavaDoc ipAddress) {
89         try {
90             new CIDRNetwork(ipAddress);
91             return true;
92         } catch (Exception JavaDoc e) {
93             return false;
94         }
95     }
96 }
97
Popular Tags