Skip to content

Commit b22feae

Browse files
committed
making the HttpClient to be used during schema loading configurable
1 parent 1f99f6d commit b22feae

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed

core/src/main/java/org/everit/jsonvalidator/loader/SchemaLoader.java

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.stream.Collectors;
3131
import java.util.stream.IntStream;
3232

33+
import org.apache.http.client.HttpClient;
3334
import org.apache.http.impl.client.HttpClients;
3435
import org.everit.jsonvalidator.ArraySchema;
3536
import org.everit.jsonvalidator.BooleanSchema;
@@ -50,8 +51,7 @@
5051
import org.json.JSONObject;
5152

5253
/**
53-
* Javadoc.
54-
*
54+
* Loads a JSON schema's JSON representation into schema validator instances.
5555
*/
5656
public class SchemaLoader {
5757

@@ -82,17 +82,20 @@ public class SchemaLoader {
8282
COMBINED_SUBSCHEMA_PROVIDERS.put("oneOf", CombinedSchema::oneOf);
8383
}
8484

85+
public static Schema load(final JSONObject schemaJson) {
86+
return load(schemaJson, HttpClients.createDefault());
87+
}
88+
8589
/**
8690
* Creates Schema instance from its JSON representation.
8791
*/
88-
public static Schema load(final JSONObject schemaJson) {
92+
public static Schema load(final JSONObject schemaJson, final HttpClient httpClient) {
8993
String schemaId = schemaJson.optString("id");
90-
SchemaLoader loader = new SchemaLoader(schemaId, schemaJson, schemaJson, null);
94+
SchemaLoader loader = new SchemaLoader(schemaId, schemaJson, schemaJson, null, httpClient);
9195
Schema.Builder<?> schemaBuilder = loader.load();
9296
Schema schema = schemaBuilder.build();
9397
loader.rootReferences.forEach(ref -> ref.build().setReferredSchema(schema));
9498
return schema;
95-
9699
}
97100

98101
/**
@@ -242,10 +245,10 @@ public OnTypeConsumer<JSONObject> ifObject() {
242245
public void orElse(final Consumer<Object> orElseConsumer) {
243246
@SuppressWarnings("unchecked")
244247
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);
249252
consumer.accept(obj);
250253

251254
}
@@ -278,32 +281,36 @@ TypeBasedMultiplexer typeMultiplexer(final String keyOfObj, final Object obj) {
278281

279282
private final JSONObject rootSchemaJson;
280283

284+
private final HttpClient httpClient;
285+
281286
/**
282287
* Constructor.
283288
*/
284289
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) {
286292
this.schemaJson = Objects.requireNonNull(schemaJson, "schemaJson cannot be null");
287293
this.rootSchemaJson = Objects.requireNonNull(rootSchemaJson, "rootSchemaJson cannot be null");
288294
this.id = id;
289295
this.rootReferences = rootReferences == null ? new ArrayList<>() : rootReferences;
296+
this.httpClient = Objects.requireNonNull(httpClient, "httpClient cannot be null");
290297
}
291298

292299
private void addDependencies(final Builder builder, final JSONObject deps) {
293300
Arrays.stream(JSONObject.getNames(deps))
294-
.forEach(ifPresent -> addDependency(builder, ifPresent, deps.get(ifPresent)));
301+
.forEach(ifPresent -> addDependency(builder, ifPresent, deps.get(ifPresent)));
295302
}
296303

297304
private void addDependency(final Builder builder, final String ifPresent, final Object deps) {
298305
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();
307314
}
308315

309316
private CombinedSchema.Builder buildAnyOfSchemaForMultipleTypes() {
@@ -326,15 +333,15 @@ private ArraySchema.Builder buildArraySchema() {
326333
ifPresent("uniqueItems", Boolean.class, builder::uniqueItems);
327334
if (schemaJson.has("additionalItems")) {
328335
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();
332339
}
333340
if (schemaJson.has("items")) {
334341
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();
338345
}
339346
return builder;
340347
}
@@ -361,20 +368,20 @@ private ObjectSchema.Builder buildObjectSchema() {
361368
if (schemaJson.has("properties")) {
362369
JSONObject propertyDefs = schemaJson.getJSONObject("properties");
363370
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()));
366373
}
367374
if (schemaJson.has("additionalProperties")) {
368375
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();
372379
}
373380
if (schemaJson.has("required")) {
374381
JSONArray requiredJson = schemaJson.getJSONArray("required");
375382
IntStream.range(0, requiredJson.length())
376-
.mapToObj(requiredJson::getString)
377-
.forEach(builder::addRequiredProperty);
383+
.mapToObj(requiredJson::getString)
384+
.forEach(builder::addRequiredProperty);
378385
}
379386
if (schemaJson.has("patternProperties")) {
380387
JSONObject patternPropsJson = schemaJson.getJSONObject("patternProperties");
@@ -418,8 +425,8 @@ private StringSchema.Builder buildStringSchema() {
418425
private void buildTupleSchema(final ArraySchema.Builder builder, final JSONArray itemSchema) {
419426
for (int i = 0; i < itemSchema.length(); ++i) {
420427
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();
423430
}
424431
}
425432

@@ -473,13 +480,13 @@ private EnumSchema.Builder buildEnumSchema() {
473480
Set<Object> possibleValues = new HashSet<>();
474481
JSONArray arr = schemaJson.getJSONArray("enum");
475482
IntStream.range(0, arr.length())
476-
.mapToObj(arr::get)
477-
.forEach(possibleValues::add);
483+
.mapToObj(arr::get)
484+
.forEach(possibleValues::add);
478485
return EnumSchema.builder().possibleValues(possibleValues);
479486
}
480487

481488
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();
483490
}
484491

485492
private Schema.Builder<?> loadForExplicitType(final String typeString) {
@@ -517,11 +524,11 @@ private Schema.Builder<?> lookupReference(final String relPointerString) {
517524
if (absPointerString.startsWith("#")) {
518525
pointer = JSONPointer.forDocument(rootSchemaJson, absPointerString);
519526
} else {
520-
pointer = JSONPointer.forURL(HttpClients.createDefault(), absPointerString);
527+
pointer = JSONPointer.forURL(httpClient, absPointerString);
521528
}
522529
QueryResult result = pointer.query();
523530
return new SchemaLoader(id, result.getQueryResult(), result.getContainingDocument(),
524-
rootReferences).load();
531+
rootReferences, httpClient).load();
525532
}
526533

527534
private boolean schemaHasAnyOf(final Collection<String> propNames) {

core/src/test/java/org/everit/jsonvalidator/loader/SchemaLoaderTest.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.io.InputStream;
1919
import java.util.Map;
2020

21+
import org.apache.http.client.HttpClient;
22+
import org.apache.http.impl.client.HttpClients;
2123
import org.everit.jsonvalidator.ArraySchema;
2224
import org.everit.jsonvalidator.BooleanSchema;
2325
import org.everit.jsonvalidator.CombinedSchema;
@@ -41,34 +43,38 @@ public class SchemaLoaderTest {
4143

4244
private static JSONObject ALL_SCHEMAS;
4345

46+
private final HttpClient httpClient = HttpClients.createDefault();
47+
4448
@Test
4549
public void typeBasedMultiplexerTest() {
46-
SchemaLoader loader = new SchemaLoader(null, new JSONObject(), new JSONObject(), null);
50+
SchemaLoader loader = new SchemaLoader(null, new JSONObject(), new JSONObject(), null,
51+
httpClient);
4752
loader.typeMultiplexer(new JSONObject())
48-
.ifObject().then(jsonObj -> {
49-
})
50-
.ifIs(JSONArray.class).then(jsonArr -> {
51-
})
52-
.orElse(obj -> {
53-
});
53+
.ifObject().then(jsonObj -> {
54+
})
55+
.ifIs(JSONArray.class).then(jsonArr -> {
56+
})
57+
.orElse(obj -> {
58+
});
5459

5560
loader.typeMultiplexer(new JSONObject())
56-
.ifObject().then(jsonObj -> {
57-
})
58-
.ifIs(JSONArray.class).then(jsonArr -> {
59-
})
60-
.requireAny();
61+
.ifObject().then(jsonObj -> {
62+
})
63+
.ifIs(JSONArray.class).then(jsonArr -> {
64+
})
65+
.requireAny();
6166
}
6267

6368
@Test(expected = SchemaException.class)
6469
public void typeBasedMultiplexerFailure() {
65-
SchemaLoader loader = new SchemaLoader(null, new JSONObject(), new JSONObject(), null);
70+
SchemaLoader loader = new SchemaLoader(null, new JSONObject(), new JSONObject(), null,
71+
httpClient);
6672
loader.typeMultiplexer("foo")
67-
.ifObject().then(o -> {
68-
})
69-
.ifIs(JSONArray.class).then(o -> {
70-
})
71-
.requireAny();
73+
.ifObject().then(o -> {
74+
})
75+
.ifIs(JSONArray.class).then(o -> {
76+
})
77+
.requireAny();
7278
}
7379

7480
@BeforeClass

0 commit comments

Comments
 (0)