28
28
29
29
import com .fasterxml .jackson .databind .JsonNode ;
30
30
import com .fasterxml .jackson .databind .ObjectMapper ;
31
- import com .networknt .schema .uri .ClasspathURIFetcher ;
32
- import com .networknt .schema .uri .DefaultURLFetcher ;
31
+ import com .networknt .schema .uri .ClasspathURLFactory ;
32
+ import com .networknt .schema .uri .ClasspathURLFetcher ;
33
+ import com .networknt .schema .uri .URIFactory ;
33
34
import com .networknt .schema .uri .URIFetcher ;
35
+ import com .networknt .schema .uri .URISchemeFactory ;
36
+ import com .networknt .schema .uri .URISchemeFetcher ;
37
+ import com .networknt .schema .uri .URLFactory ;
38
+ import com .networknt .schema .uri .URLFetcher ;
34
39
35
40
public class JsonSchemaFactory {
36
41
private static final Logger logger = LoggerFactory
@@ -39,40 +44,69 @@ public class JsonSchemaFactory {
39
44
40
45
public static class Builder {
41
46
private ObjectMapper objectMapper = new ObjectMapper ();
42
- private Map <String , URIFetcher > uriFetcherMap = new HashMap <String , URIFetcher >();
43
47
private String defaultMetaSchemaURI ;
44
- private Map <String , JsonMetaSchema > jsonMetaSchemas = new HashMap <String , JsonMetaSchema >();
45
- private Map <String , String > uriMap = new HashMap <String , String >();
48
+ private final Map <String , URIFactory > uriFactoryMap = new HashMap <String , URIFactory >();
49
+ private final Map <String , URIFetcher > uriFetcherMap = new HashMap <String , URIFetcher >();
50
+ private final Map <String , JsonMetaSchema > jsonMetaSchemas = new HashMap <String , JsonMetaSchema >();
51
+ private final Map <String , String > uriMap = new HashMap <String , String >();
46
52
47
53
public Builder () {
48
- // By default we add support for custom classpath URIs.
49
- final URIFetcher classpathUriFetcher = new ClasspathURIFetcher ();
50
- for (final String scheme : ClasspathURIFetcher .SUPPORTED_SCHEMES ) {
51
- this .uriFetcherMap .put (scheme , classpathUriFetcher );
52
- }
54
+ // Adds support for creating {@link URL}s.
55
+ final URIFactory urlFactory = new URLFactory ();
56
+ for (final String scheme : URLFactory .SUPPORTED_SCHEMES ) {
57
+ this .uriFactoryMap .put (scheme , urlFactory );
58
+ }
59
+
60
+ // Adds support for fetching with {@link URL}s.
61
+ final URIFetcher urlFetcher = new URLFetcher ();
62
+ for (final String scheme : URLFetcher .SUPPORTED_SCHEMES ) {
63
+ this .uriFetcherMap .put (scheme , urlFetcher );
64
+ }
65
+
66
+ // Adds support for creating and fetching with classpath {@link URL}s.
67
+ final URIFactory classpathURLFactory = new ClasspathURLFactory ();
68
+ final URIFetcher classpathURLFetcher = new ClasspathURLFetcher ();
69
+ for (final String scheme : ClasspathURLFactory .SUPPORTED_SCHEMES ) {
70
+ this .uriFactoryMap .put (scheme , classpathURLFactory );
71
+ this .uriFetcherMap .put (scheme , classpathURLFetcher );
72
+ }
53
73
}
54
74
55
75
public Builder objectMapper (final ObjectMapper objectMapper ) {
56
76
this .objectMapper = objectMapper ;
57
77
return this ;
58
78
}
59
79
80
+ public Builder defaultMetaSchemaURI (final String defaultMetaSchemaURI ) {
81
+ this .defaultMetaSchemaURI = defaultMetaSchemaURI ;
82
+ return this ;
83
+ }
84
+
85
+ /**
86
+ * Maps a number of schemes to a {@link URIFactory}.
87
+ * @param uriFactory the uri factory that will be used for the given schemes.
88
+ * @param scheme the scheme that the uri factory will be assocaited with.
89
+ * @return this builder.
90
+ */
91
+ public Builder uriFactory (final URIFactory uriFactory , final String ... schemes ) {
92
+ for (final String scheme : schemes )
93
+ {
94
+ this .uriFactoryMap .put (scheme , uriFactory );
95
+ }
96
+ return this ;
97
+ }
98
+
60
99
/**
61
- * Maps a number of scheme to a {@link URIFetcher}.
100
+ * Maps a number of schemes to a {@link URIFetcher}.
62
101
* @param uriFetcher the uri fetcher that will be used for the given schemes.
63
102
* @param scheme the scheme that the uri fetcher will be assocaited with.
64
103
* @return this builder.
65
104
*/
66
105
public Builder uriFetcher (final URIFetcher uriFetcher , final String ... schemes ) {
67
106
for (final String scheme : schemes )
68
- {
107
+ {
69
108
this .uriFetcherMap .put (scheme , uriFetcher );
70
- }
71
- return this ;
72
- }
73
-
74
- public Builder defaultMetaSchemaURI (final String defaultMetaSchemaURI ) {
75
- this .defaultMetaSchemaURI = defaultMetaSchemaURI ;
109
+ }
76
110
return this ;
77
111
}
78
112
@@ -97,37 +131,41 @@ public JsonSchemaFactory build() {
97
131
// create builtin keywords with (custom) formats.
98
132
return new JsonSchemaFactory (
99
133
objectMapper == null ? new ObjectMapper () : objectMapper ,
100
- uriFetcherMap ,
101
134
defaultMetaSchemaURI ,
135
+ new URISchemeFactory (uriFactoryMap ),
136
+ new URISchemeFetcher (uriFetcherMap ),
102
137
jsonMetaSchemas ,
103
138
uriMap
104
139
);
105
140
}
106
141
}
107
142
108
- private static final URIFetcher DEFAULT_URI_FETCHER = new DefaultURLFetcher ();
109
-
110
143
private final ObjectMapper mapper ;
111
- private final Map <String , URIFetcher > uriFetcherMap ;
112
144
private final String defaultMetaSchemaURI ;
145
+ private final URISchemeFactory uriFactory ;
146
+ private final URISchemeFetcher uriFetcher ;
113
147
private final Map <String , JsonMetaSchema > jsonMetaSchemas ;
114
148
private final Map <String , String > uriMap ;
115
149
116
150
private JsonSchemaFactory (
117
151
final ObjectMapper mapper ,
118
- final Map <String , URIFetcher > uriFetcherMap ,
119
152
final String defaultMetaSchemaURI ,
153
+ final URISchemeFactory uriFactory ,
154
+ final URISchemeFetcher uriFetcher ,
120
155
final Map <String , JsonMetaSchema > jsonMetaSchemas ,
121
156
final Map <String , String > uriMap ) {
122
157
if (mapper == null ) {
123
158
throw new IllegalArgumentException ("ObjectMapper must not be null" );
124
159
}
125
- if (uriFetcherMap == null ) {
126
- throw new IllegalArgumentException ("URLFetcher must not be null" );
127
- }
128
160
if (defaultMetaSchemaURI == null || defaultMetaSchemaURI .trim ().isEmpty ()) {
129
161
throw new IllegalArgumentException ("defaultMetaSchemaURI must not be null or empty" );
130
162
}
163
+ if (uriFactory == null ) {
164
+ throw new IllegalArgumentException ("URIFactory must not be null" );
165
+ }
166
+ if (uriFetcher == null ) {
167
+ throw new IllegalArgumentException ("URIFetcher must not be null" );
168
+ }
131
169
if (jsonMetaSchemas == null || jsonMetaSchemas .isEmpty ()) {
132
170
throw new IllegalArgumentException ("Json Meta Schemas must not be null or empty" );
133
171
}
@@ -139,7 +177,8 @@ private JsonSchemaFactory(
139
177
}
140
178
this .mapper = mapper ;
141
179
this .defaultMetaSchemaURI = defaultMetaSchemaURI ;
142
- this .uriFetcherMap = uriFetcherMap ;
180
+ this .uriFactory = uriFactory ;
181
+ this .uriFetcher = uriFetcher ;
143
182
this .jsonMetaSchemas = jsonMetaSchemas ;
144
183
this .uriMap = uriMap ;
145
184
}
@@ -174,7 +213,11 @@ public static Builder builder(final JsonSchemaFactory blueprint) {
174
213
.objectMapper (blueprint .mapper )
175
214
.addUriMappings (blueprint .uriMap );
176
215
177
- for (Map .Entry <String , URIFetcher > entry : blueprint .uriFetcherMap .entrySet ())
216
+ for (Map .Entry <String , URIFactory > entry : blueprint .uriFactory .getURIFactories ().entrySet ())
217
+ {
218
+ builder = builder .uriFactory (entry .getValue (), entry .getKey ());
219
+ }
220
+ for (Map .Entry <String , URIFetcher > entry : blueprint .uriFetcher .getURIFetchers ().entrySet ())
178
221
{
179
222
builder = builder .uriFetcher (entry .getValue (), entry .getKey ());
180
223
}
@@ -202,6 +245,10 @@ private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
202
245
}
203
246
return jsonMetaSchema ;
204
247
}
248
+
249
+ public URIFactory getURIFactory () {
250
+ return this .uriFactory ;
251
+ }
205
252
206
253
public JsonSchema getSchema (final String schema , final SchemaValidatorsConfig config ) {
207
254
try {
@@ -245,8 +292,7 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
245
292
throw new JsonSchemaException (e );
246
293
}
247
294
try {
248
- final URIFetcher uriFetcher = this .uriFetcherMap .getOrDefault (mappedUri .getScheme (), DEFAULT_URI_FETCHER );
249
- inputStream = uriFetcher .fetch (mappedUri );
295
+ inputStream = this .uriFetcher .fetch (mappedUri );
250
296
final JsonNode schemaNode = mapper .readTree (inputStream );
251
297
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema (schemaNode );
252
298
0 commit comments