1
1
package org .everit .json .schema .internal ;
2
2
3
3
import java .net .InetAddress ;
4
+ import java .net .URI ;
5
+ import java .net .URISyntaxException ;
6
+ import java .text .ParseException ;
7
+ import java .text .SimpleDateFormat ;
4
8
import java .util .Objects ;
5
9
import java .util .Optional ;
6
10
7
11
import org .everit .json .schema .Format ;
8
12
import org .everit .json .schema .FormatValidator ;
9
13
10
14
import com .google .common .net .InetAddresses ;
15
+ import com .google .common .net .InternetDomainName ;
11
16
12
17
public class DefaultFormatValidator implements FormatValidator {
13
18
14
19
private static final int IPV4_LENGTH = 4 ;
15
20
16
21
private static final int IPV6_LENGTH = 16 ;
17
22
23
+ private static final String DATETIME_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ssXXX" ;
24
+
25
+ private static final String DATETIME_FORMAT_STRING_SECFRAC = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ;
26
+
18
27
private Optional <InetAddress > asInetAddress (final String subject ) {
19
- if (InetAddresses .isInetAddress (subject )) {
20
- return Optional .of (InetAddresses .forString (subject ));
21
- } else {
28
+ try {
29
+ if (InetAddresses .isInetAddress (subject )) {
30
+ return Optional .of (InetAddresses .forString (subject ));
31
+ } else {
32
+ return Optional .empty ();
33
+ }
34
+ } catch (NullPointerException e ) {
35
+ return Optional .empty ();
36
+ }
37
+ }
38
+
39
+ private Optional <String > checkDateTime (final String subject ) {
40
+ try {
41
+ new SimpleDateFormat (DATETIME_FORMAT_STRING ).parse (subject );
42
+ return Optional .empty ();
43
+ } catch (ParseException e ) {
44
+ try {
45
+ new SimpleDateFormat (DATETIME_FORMAT_STRING_SECFRAC ).parse (subject );
46
+ return Optional .empty ();
47
+ } catch (ParseException e1 ) {
48
+ return Optional .of (String .format ("[%s] is not a valid date-time" , subject ));
49
+ }
50
+ }
51
+ }
52
+
53
+ private Optional <String > checkHostname (final String subject ) {
54
+ try {
55
+ InternetDomainName .from (subject );
22
56
return Optional .empty ();
57
+ } catch (IllegalArgumentException | NullPointerException e ) {
58
+ return Optional .of (String .format ("[%s] is not a valid hostname" , subject ));
23
59
}
24
60
}
25
61
@@ -31,6 +67,15 @@ private Optional<String> checkIpAddress(final String subject, final int expected
31
67
.orElse (Optional .of (String .format (failureFormat , subject )));
32
68
}
33
69
70
+ private Optional <String > checkURI (final String subject ) {
71
+ try {
72
+ new URI (subject );
73
+ return Optional .empty ();
74
+ } catch (URISyntaxException | NullPointerException e ) {
75
+ return Optional .of (String .format ("[%s] is not a valid URI" , subject ));
76
+ }
77
+ }
78
+
34
79
@ Override
35
80
public Optional <String > validate (final String subject , final Format format ) {
36
81
Objects .requireNonNull (format , "format cannot be null" );
@@ -39,6 +84,12 @@ public Optional<String> validate(final String subject, final Format format) {
39
84
return checkIpAddress (subject , IPV4_LENGTH , "[%s] is not a valid ipv4 address" );
40
85
case IPV6 :
41
86
return checkIpAddress (subject , IPV6_LENGTH , "[%s] is not a valid ipv6 address" );
87
+ case HOSTNAME :
88
+ return checkHostname (subject );
89
+ case URI :
90
+ return checkURI (subject );
91
+ case DATE_TIME :
92
+ return checkDateTime (subject );
42
93
default :
43
94
throw new IllegalArgumentException ("unsupported format: " + format );
44
95
}
0 commit comments