|
| 1 | +# Custom URIFetcher |
| 2 | + |
1 | 3 | The default `URIFetcher` implementation uses JDK connection/socket without handling network exceptions. It works in most of the cases; however, if you want to have a customized implementation, you can do so. One user has his implementation with urirest to handle the timeout. A detailed discussion can be found in this [issue](https://github.com/networknt/json-schema-validator/issues/240)
|
| 4 | + |
| 5 | +## Example implementation |
| 6 | + |
| 7 | +The default URIFetcher can be overwritten in order to customize its behaviour in regards of authorization or error handling. |
| 8 | +Therefore the _URIFetcher_ interface must implemented and the method _fetch_ must be overwritten. |
| 9 | + |
| 10 | +``` |
| 11 | +public class CustomUriFetcher implements URIFetcher { |
| 12 | +
|
| 13 | + private static final Logger LOGGER = LoggerFactory.getLogger(CustomUriFetcher.class); |
| 14 | +
|
| 15 | + private final String authorizationToken; |
| 16 | +
|
| 17 | + private final HttpClient client; |
| 18 | +
|
| 19 | + public CustomUriFetcher(String authorizationToken) { |
| 20 | + this.authorizationToken = authorizationToken; |
| 21 | + this.client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); |
| 22 | + } |
| 23 | +
|
| 24 | + @Override |
| 25 | + public InputStream fetch(URI uri) throws IOException { |
| 26 | + HttpRequest request = HttpRequest.newBuilder().uri(uri).header("Authorization", authorizationToken).build(); |
| 27 | + try { |
| 28 | + HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 29 | + if ((200 > response.statusCode()) || (response.statusCode() > 299)) { |
| 30 | + String errorMessage = String.format("Could not get data from schema endpoint. The following status %d was returned.", response.statusCode()); |
| 31 | + LOGGER.error(errorMessage); |
| 32 | + } |
| 33 | +
|
| 34 | + return new ByteArrayInputStream(response.body().getBytes(StandardCharsets.UTF_8)); |
| 35 | + } catch (InterruptedException e) { |
| 36 | + throw new RuntimeException(e); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Within the _JsonSchemaFactory_ the custom URIFetcher can be referenced. |
| 43 | +This also works for schema references ($ref) inside the schema. |
| 44 | + |
| 45 | +``` |
| 46 | +... |
| 47 | +CustomUriFetcher uriFetcher = new CustomUriFetcher(authorizationToken); |
| 48 | +
|
| 49 | +JsonSchemaFactory schemaFactory = JsonSchemaFactory.builder() |
| 50 | + .uriFetcher(uriFetcher, "http") |
| 51 | + .addMetaSchema(JsonMetaSchema.getV7()) |
| 52 | + .defaultMetaSchemaURI(JsonMetaSchema.getV7().getUri()) |
| 53 | + .build(); |
| 54 | +JsonSchema jsonSchema = schemaFactory.getSchema(schemaUri); |
| 55 | +for (ValidationMessage validationMessage : jsonSchema.validate(jsonNodeRecord)) { |
| 56 | + // handle the validation messages |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +**_NOTE:_** |
| 61 | +Within `.uriFetcher(uriFetcher, "http")` your URI must be mapped to the related protocol like http, ftp, ... |
0 commit comments