1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.config.tools;
19
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23
24
25
26
27
28
29
30
31
32 public class IpVerifier extends TextVerifier {
33
34
35
36
37 public static final String ID_ERROR = "InvalidAddress";
38
39
40
41
42 private static final int MAX_BYTE = 255;
43
44
45
46
47 public static final int IP_4 = 0;
48
49
50
51
52 public static final int IP_6 = 1;
53
54
55
56
57 public static final int IP_4_IP_6 = 2;
58
59
60
61
62 private static final String IP_4_PATTERN = "^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$";
63
64
65
66
67 private static final String IP_6_PATTERN =
68 "^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$";
69
70
71
72
73 private static final String IP_4_IP_6_PATTERN =
74 "^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)(\\.(\\d+)\\.(\\d+))?+$";
75
76
77
78
79 private int skipGroup;
80
81
82
83
84 private Pattern pattern = null;
85
86
87
88
89 private String errorInvalidIpId;
90
91
92
93
94
95
96
97 private String errorLoopbackId = "";
98
99
100
101
102 private int ipType = 0;
103
104
105
106
107 private boolean allowEmpty = false;
108
109
110
111
112
113
114 private boolean allowLoopbackAddress = true;
115
116
117
118
119
120
121
122 public IpVerifier(ErrorSupport support, String id) {
123 this(support, id, IP_4_IP_6);
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137 public IpVerifier(ErrorSupport support, String id, int validIp) {
138 super(support);
139 this.errorInvalidIpId = id;
140 this.ipType = validIp;
141 switch (validIp) {
142 case IpVerifier.IP_4:
143 this.pattern = Pattern.compile(IpVerifier.IP_4_PATTERN);
144 this.skipGroup = 0;
145 break;
146 case IpVerifier.IP_6:
147 this.pattern = Pattern.compile(IpVerifier.IP_6_PATTERN);
148 this.skipGroup = 0;
149 break;
150 default:
151 this.pattern = Pattern.compile(IpVerifier.IP_4_IP_6_PATTERN);
152 this.ipType = IpVerifier.IP_4_IP_6;
153 this.skipGroup = 5;
154 }
155 }
156
157
158
159
160
161
162 public void setEmptyAllowed(boolean value) {
163 this.allowEmpty = value;
164 }
165
166
167
168
169
170
171
172
173
174
175
176 public void setLoopbackAddressAllowed(boolean allowLoopbackAddress) {
177 this.allowLoopbackAddress = allowLoopbackAddress;
178 }
179
180
181
182
183
184
185 public void setErrorLoopbackId(String errorLoopbackId) {
186 this.errorLoopbackId = errorLoopbackId;
187 }
188
189
190
191 public boolean verify(String text) {
192 if (text == null || text.trim().length() == 0) {
193 return allowEmpty;
194 }
195
196 boolean valid = true;
197
198 Matcher matcher = pattern.matcher(text);
199 if (!matcher.matches()) {
200 valid = false;
201 }
202
203 int groupNumber = matcher.groupCount();
204 for (int i = 1; valid && i <= groupNumber; i++) {
205 try {
206 if (i != skipGroup && matcher.group(i) != null) {
207 int fieldValue = Integer.parseInt(matcher.group(i));
208
209
210
211 if (fieldValue > MAX_BYTE) {
212 valid = false;
213 }
214 }
215 } catch (Exception e) {
216 valid = false;
217 }
218 }
219
220 if (!valid) {
221 updateError(errorInvalidIpId, true);
222 return false;
223 } else {
224 updateError(errorInvalidIpId, false);
225 }
226
227 valid = this.allowLoopbackAddress || !isLoopbackAddress(text, this.ipType);
228 updateError(errorLoopbackId, !valid);
229 return valid;
230 }
231
232
233
234
235 public void cleanErrors() {
236 updateError(errorInvalidIpId, false);
237 if ((errorLoopbackId != null) && (errorLoopbackId.length() > 0)) {
238 updateError(errorInvalidIpId, false);
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251
252 public static boolean isLoopbackAddress(String ip, int ipVersion) {
253 if (ip == null) {
254 return false;
255 }
256 switch (ipVersion) {
257 case IP_4:
258 return ip.startsWith("127.");
259 case IP_6:
260 return "0.0.0.0.0.0".equals(ip) || "0.0.0.0.0.1".equals(ip);
261 case IP_4_IP_6:
262 return ip.startsWith("127.") || "0.0.0.0.0.0".equals(ip)
263 || "0.0.0.0.0.1".equals(ip);
264 default:
265 return false;
266 }
267 }
268 }