30
30
import java .util .stream .Collectors ;
31
31
import java .util .stream .IntStream ;
32
32
33
+ import org .apache .http .client .HttpClient ;
33
34
import org .apache .http .impl .client .HttpClients ;
34
35
import org .everit .jsonvalidator .ArraySchema ;
35
36
import org .everit .jsonvalidator .BooleanSchema ;
50
51
import org .json .JSONObject ;
51
52
52
53
/**
53
- * Javadoc.
54
- *
54
+ * Loads a JSON schema's JSON representation into schema validator instances.
55
55
*/
56
56
public class SchemaLoader {
57
57
@@ -82,17 +82,20 @@ public class SchemaLoader {
82
82
COMBINED_SUBSCHEMA_PROVIDERS .put ("oneOf" , CombinedSchema ::oneOf );
83
83
}
84
84
85
+ public static Schema load (final JSONObject schemaJson ) {
86
+ return load (schemaJson , HttpClients .createDefault ());
87
+ }
88
+
85
89
/**
86
90
* Creates Schema instance from its JSON representation.
87
91
*/
88
- public static Schema load (final JSONObject schemaJson ) {
92
+ public static Schema load (final JSONObject schemaJson , final HttpClient httpClient ) {
89
93
String schemaId = schemaJson .optString ("id" );
90
- SchemaLoader loader = new SchemaLoader (schemaId , schemaJson , schemaJson , null );
94
+ SchemaLoader loader = new SchemaLoader (schemaId , schemaJson , schemaJson , null , httpClient );
91
95
Schema .Builder <?> schemaBuilder = loader .load ();
92
96
Schema schema = schemaBuilder .build ();
93
97
loader .rootReferences .forEach (ref -> ref .build ().setReferredSchema (schema ));
94
98
return schema ;
95
-
96
99
}
97
100
98
101
/**
@@ -242,10 +245,10 @@ public OnTypeConsumer<JSONObject> ifObject() {
242
245
public void orElse (final Consumer <Object > orElseConsumer ) {
243
246
@ SuppressWarnings ("unchecked" )
244
247
Consumer <Object > consumer = (Consumer <Object >) actions .keySet ().stream ()
245
- .filter (clazz -> clazz .isAssignableFrom (obj .getClass ()))
246
- .findFirst ()
247
- .map (actions ::get )
248
- .orElse (orElseConsumer ::accept );
248
+ .filter (clazz -> clazz .isAssignableFrom (obj .getClass ()))
249
+ .findFirst ()
250
+ .map (actions ::get )
251
+ .orElse (orElseConsumer ::accept );
249
252
consumer .accept (obj );
250
253
251
254
}
@@ -278,32 +281,36 @@ TypeBasedMultiplexer typeMultiplexer(final String keyOfObj, final Object obj) {
278
281
279
282
private final JSONObject rootSchemaJson ;
280
283
284
+ private final HttpClient httpClient ;
285
+
281
286
/**
282
287
* Constructor.
283
288
*/
284
289
public SchemaLoader (final String id , final JSONObject schemaJson ,
285
- final JSONObject rootSchemaJson , final Collection <ReferenceSchema .Builder > rootReferences ) {
290
+ final JSONObject rootSchemaJson , final Collection <ReferenceSchema .Builder > rootReferences ,
291
+ final HttpClient httpClient ) {
286
292
this .schemaJson = Objects .requireNonNull (schemaJson , "schemaJson cannot be null" );
287
293
this .rootSchemaJson = Objects .requireNonNull (rootSchemaJson , "rootSchemaJson cannot be null" );
288
294
this .id = id ;
289
295
this .rootReferences = rootReferences == null ? new ArrayList <>() : rootReferences ;
296
+ this .httpClient = Objects .requireNonNull (httpClient , "httpClient cannot be null" );
290
297
}
291
298
292
299
private void addDependencies (final Builder builder , final JSONObject deps ) {
293
300
Arrays .stream (JSONObject .getNames (deps ))
294
- .forEach (ifPresent -> addDependency (builder , ifPresent , deps .get (ifPresent )));
301
+ .forEach (ifPresent -> addDependency (builder , ifPresent , deps .get (ifPresent )));
295
302
}
296
303
297
304
private void addDependency (final Builder builder , final String ifPresent , final Object deps ) {
298
305
typeMultiplexer (deps )
299
- .ifObject ().then (obj -> {
300
- builder .schemaDependency (ifPresent , loadChild (obj ).build ());
301
- })
302
- .ifIs (JSONArray .class ).then (propNames -> {
303
- IntStream .range (0 , propNames .length ())
304
- .mapToObj (i -> propNames .getString (i ))
305
- .forEach (dependency -> builder .propertyDependency (ifPresent , dependency ));
306
- }).requireAny ();
306
+ .ifObject ().then (obj -> {
307
+ builder .schemaDependency (ifPresent , loadChild (obj ).build ());
308
+ })
309
+ .ifIs (JSONArray .class ).then (propNames -> {
310
+ IntStream .range (0 , propNames .length ())
311
+ .mapToObj (i -> propNames .getString (i ))
312
+ .forEach (dependency -> builder .propertyDependency (ifPresent , dependency ));
313
+ }).requireAny ();
307
314
}
308
315
309
316
private CombinedSchema .Builder buildAnyOfSchemaForMultipleTypes () {
@@ -326,15 +333,15 @@ private ArraySchema.Builder buildArraySchema() {
326
333
ifPresent ("uniqueItems" , Boolean .class , builder ::uniqueItems );
327
334
if (schemaJson .has ("additionalItems" )) {
328
335
typeMultiplexer ("additionalItems" , schemaJson .get ("additionalItems" ))
329
- .ifIs (Boolean .class ).then (builder ::additionalItems )
330
- .ifObject ().then (jsonObj -> builder .schemaOfAdditionalItems (loadChild (jsonObj ).build ()))
331
- .requireAny ();
336
+ .ifIs (Boolean .class ).then (builder ::additionalItems )
337
+ .ifObject ().then (jsonObj -> builder .schemaOfAdditionalItems (loadChild (jsonObj ).build ()))
338
+ .requireAny ();
332
339
}
333
340
if (schemaJson .has ("items" )) {
334
341
typeMultiplexer ("items" , schemaJson .get ("items" ))
335
- .ifObject ().then (itemSchema -> builder .allItemSchema (loadChild (itemSchema ).build ()))
336
- .ifIs (JSONArray .class ).then (arr -> buildTupleSchema (builder , arr ))
337
- .requireAny ();
342
+ .ifObject ().then (itemSchema -> builder .allItemSchema (loadChild (itemSchema ).build ()))
343
+ .ifIs (JSONArray .class ).then (arr -> buildTupleSchema (builder , arr ))
344
+ .requireAny ();
338
345
}
339
346
return builder ;
340
347
}
@@ -361,20 +368,20 @@ private ObjectSchema.Builder buildObjectSchema() {
361
368
if (schemaJson .has ("properties" )) {
362
369
JSONObject propertyDefs = schemaJson .getJSONObject ("properties" );
363
370
Arrays .stream (Optional .ofNullable (JSONObject .getNames (propertyDefs )).orElse (new String [0 ]))
364
- .forEach (key -> builder .addPropertySchema (key ,
365
- loadChild (propertyDefs .getJSONObject (key )).build ()));
371
+ .forEach (key -> builder .addPropertySchema (key ,
372
+ loadChild (propertyDefs .getJSONObject (key )).build ()));
366
373
}
367
374
if (schemaJson .has ("additionalProperties" )) {
368
375
typeMultiplexer ("additionalProperties" , schemaJson .get ("additionalProperties" ))
369
- .ifIs (Boolean .class ).then (builder ::additionalProperties )
370
- .ifObject ().then (def -> builder .schemaOfAdditionalProperties (loadChild (def ).build ()))
371
- .requireAny ();
376
+ .ifIs (Boolean .class ).then (builder ::additionalProperties )
377
+ .ifObject ().then (def -> builder .schemaOfAdditionalProperties (loadChild (def ).build ()))
378
+ .requireAny ();
372
379
}
373
380
if (schemaJson .has ("required" )) {
374
381
JSONArray requiredJson = schemaJson .getJSONArray ("required" );
375
382
IntStream .range (0 , requiredJson .length ())
376
- .mapToObj (requiredJson ::getString )
377
- .forEach (builder ::addRequiredProperty );
383
+ .mapToObj (requiredJson ::getString )
384
+ .forEach (builder ::addRequiredProperty );
378
385
}
379
386
if (schemaJson .has ("patternProperties" )) {
380
387
JSONObject patternPropsJson = schemaJson .getJSONObject ("patternProperties" );
@@ -418,8 +425,8 @@ private StringSchema.Builder buildStringSchema() {
418
425
private void buildTupleSchema (final ArraySchema .Builder builder , final JSONArray itemSchema ) {
419
426
for (int i = 0 ; i < itemSchema .length (); ++i ) {
420
427
typeMultiplexer (itemSchema .get (i ))
421
- .ifObject ().then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
422
- .requireAny ();
428
+ .ifObject ().then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
429
+ .requireAny ();
423
430
}
424
431
}
425
432
@@ -473,13 +480,13 @@ private EnumSchema.Builder buildEnumSchema() {
473
480
Set <Object > possibleValues = new HashSet <>();
474
481
JSONArray arr = schemaJson .getJSONArray ("enum" );
475
482
IntStream .range (0 , arr .length ())
476
- .mapToObj (arr ::get )
477
- .forEach (possibleValues ::add );
483
+ .mapToObj (arr ::get )
484
+ .forEach (possibleValues ::add );
478
485
return EnumSchema .builder ().possibleValues (possibleValues );
479
486
}
480
487
481
488
private Schema .Builder <?> loadChild (final JSONObject childJson ) {
482
- return new SchemaLoader (id , childJson , rootSchemaJson , rootReferences ).load ();
489
+ return new SchemaLoader (id , childJson , rootSchemaJson , rootReferences , httpClient ).load ();
483
490
}
484
491
485
492
private Schema .Builder <?> loadForExplicitType (final String typeString ) {
@@ -517,11 +524,11 @@ private Schema.Builder<?> lookupReference(final String relPointerString) {
517
524
if (absPointerString .startsWith ("#" )) {
518
525
pointer = JSONPointer .forDocument (rootSchemaJson , absPointerString );
519
526
} else {
520
- pointer = JSONPointer .forURL (HttpClients . createDefault () , absPointerString );
527
+ pointer = JSONPointer .forURL (httpClient , absPointerString );
521
528
}
522
529
QueryResult result = pointer .query ();
523
530
return new SchemaLoader (id , result .getQueryResult (), result .getContainingDocument (),
524
- rootReferences ).load ();
531
+ rootReferences , httpClient ).load ();
525
532
}
526
533
527
534
private boolean schemaHasAnyOf (final Collection <String > propNames ) {
0 commit comments