Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
  *
  * This software is dual-licensed under:
  *
  * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
  *   later version;
  * - the Apache Software License (ASL) version 2.0.
  *
 * The text of this file and of both licenses is available at the root of this
 * project or, if you have the jar distribution, in directory META-INF/, under
 * the names LGPL-3.0.txt and ASL-2.0.txt respectively.
 *
 * Direct link to the sources:
 *
 * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
 * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
 */
package com.github.fge.jsonschema.examples;
Sixth example: URI redirection

link to source code

In this example, the same schema file is used as in Example1. This time, though, it is assumed that the base URI used for addressing this schema is http://my.site/schemas/fstab.json#. But instead of trying to fetch it from the web directly, we want to use the local copy, which is located under URI resource:/org/eel/kitchen/jsonschema/examples/fstab.json#.

The solution here is to build a custom com.github.fge.jsonschema.core.load.uri.URITranslatorConfiguration, which allows to customize URI handling; in this case, a schema redirection using the com.github.fge.jsonschema.core.load.uri.URITranslatorConfigurationBuilder.addSchemaRedirect(java.lang.String,java.lang.String). We then inject this into a custom com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration.

The effect is that if you required a schema via URI http://my.site/schemas/fstab.json#, it will silently transform this URI into resource:/org/eel/kitchen/jsonschema/examples/fstab.json# internally.

Note that URIs must be absolute JSON references (see com.github.fge.jsonschema.core.ref.JsonRef).

public final class Example6
    private static final String FROM = "http://my.site/schemas/fstab.json#";
    private static final String TO
        = "resource:/com/github/fge/jsonschema/examples/fstab.json#";
    public static void main(final String... args)
        throws IOExceptionProcessingException
    {
        final JsonNode good = Utils.loadResource("/fstab-good.json");
        final JsonNode bad = Utils.loadResource("/fstab-bad.json");
        final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
        final URITranslatorConfiguration translatorCfg
            = URITranslatorConfiguration.newBuilder()
            .addSchemaRedirect().freeze();
        final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
            .setURITranslatorConfiguration(translatorCfg).freeze();
        final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
            .setLoadingConfiguration(cfg).freeze();
        final JsonSchema schema = factory.getJsonSchema();
        ProcessingReport report;
        report = schema.validate(good);
        ..println(report);
        report = schema.validate(bad);
        ..println(report);
        report = schema.validate(bad2);
        ..println(report);
    }
New to GrepCode? Check out our FAQ X