/** * Build a {@link JsonSchema} instance * * @param uri the URI to load the schema from * @return a {@link JsonSchema} * @throws ProcessingException invalid URI, or URI did not resolve to a * JSON Schema * @throws NullPointerException URI is null */ JsonSchema buildJsonSchema(final String uri) throws ProcessingException { final JsonRef ref = JsonRef.fromString(uri); if (!ref.isLegal()) throw new JsonReferenceException(new ProcessingMessage() .setMessage(BUNDLE.getMessage("illegalJsonRef"))); final SchemaTree tree = loader.get(ref.getLocator()).setPointer(ref.getPointer()); if (tree.getNode().isMissingNode()) throw new JsonReferenceException(new ProcessingMessage() .setMessage(BUNDLE.getMessage("danglingRef"))); return new JsonSchemaImpl(processor, tree, reportProvider); }
/** * Package private constructor to build a factory out of a builder * * @param builder the builder * @see JsonSchemaFactoryBuilder#freeze() */ JsonSchemaFactory(final JsonSchemaFactoryBuilder builder) { reportProvider = builder.reportProvider; loadingCfg = builder.loadingCfg; validationCfg = builder.validationCfg; loader = new SchemaLoader(loadingCfg); final Processor<SchemaContext, ValidatorList> processor = buildProcessor(); validator = new JsonValidator(loader, new ValidationProcessor(validationCfg, processor), reportProvider); syntaxValidator = new SyntaxValidator(validationCfg); }
private FullData buildData(final JsonNode schema, final JsonNode instance, final boolean deepCheck) { BUNDLE.checkNotNull(schema, "nullSchema"); BUNDLE.checkNotNull(instance, "nullInstance"); final SchemaTree schemaTree = loader.load(schema); final JsonTree tree = new SimpleJsonTree(instance); return new FullData(schemaTree, tree, deepCheck); } }
@Test public void schemasAreFetchedOnceNotTwice() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader).freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(1)).fetch(uri); }
@Test public void URIsAreNormalizedBehindTheScenes() throws ProcessingException { final String location = "http://toto/a/../b"; final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); final SchemaTree tree = loader.get(URI.create(location)); assertEquals(tree.getLoadingRef().toURI(), URI.create("http://toto/b#")); }
@Test public void schemasCacheCanBeDisabled() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader) .setCacheSize(0) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(2)).fetch(uri); }
/** * Get the {@link SchemaTree} for the given {@code uri}. * * This is similar to {@link SchemaLoader#get(URI)}, but allows {@code uri} to contain a fragment. * * @param uri * @return * @throws ProcessingException */ // XXX: Should this be the default behavior of SchemaLoader#get()? @VisibleForTesting protected SchemaTree getSchema(SchemaLoader schemaLoader, URI uri) throws ProcessingException { String fragment = uri.getFragment(); if (fragment == null) { return schemaLoader.get(uri); } else { try { URI schemaTreeUri = new URI(uri.getScheme(), uri.getSchemeSpecificPart(), null); JsonPointer pointer = new JsonPointer(fragment); SchemaTree schema = schemaLoader.get(schemaTreeUri); return schema.setPointer(pointer); } catch (URISyntaxException|JsonPointerException e) { assert false : "Was a valid before, we split things up!"; throw new RuntimeException(e); } } } }
return new SchemaLoader(loadingConfigurationBuilder.freeze());
/** * Build a {@link JsonSchema} instance * * @param schema the schema * @param pointer the pointer into the schema * @return a new {@link JsonSchema} * @throws ProcessingException resolving the pointer against the schema * leads to a {@link MissingNode} * @throws NullPointerException the schema or pointer is null */ JsonSchema buildJsonSchema(final JsonNode schema, final JsonPointer pointer) throws ProcessingException { final SchemaTree tree = loader.load(schema).setPointer(pointer); if (tree.getNode().isMissingNode()) throw new JsonReferenceException(new ProcessingMessage() .setMessage(BUNDLE.getMessage("danglingRef"))); return new JsonSchemaImpl(processor, tree, reportProvider); }
@Test public void schemasCacheCanBeDisabledViaCacheSize() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader) .setCacheSize(0) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(2)).fetch(uri); } }
tree = loader.get(ref.getLocator());
@Test public void preloadedSchemasAreNotFetchedAgain() throws ProcessingException, IOException { final String location = "http://foo.bar/baz#"; final URI uri = URI.create(location); final URIDownloader mock = mock(URIDownloader.class); final LoadingConfigurationBuilder builder = LoadingConfiguration .newBuilder().addScheme("http", mock) .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()); LoadingConfiguration cfg; SchemaLoader registry; cfg = builder.freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); //even if cache is disabled cfg = builder.setCacheSize(0).freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); }
tree = loader.get(ref.getLocator());
@Test public void NonAbsoluteURIsAreRefused() { final SchemaLoader loader = new SchemaLoader(); final URI target = URI.create("moo#"); try { loader.get(target); fail("No exception thrown!"); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("refProcessing.uriNotAbsolute", target)) .hasLevel(LogLevel.FATAL).hasField("uri", target); } }
tree = loader.get(ref.getLocator());
final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); final JsonRef ref = JsonRef.fromURI(rootns.resolve(uri)); verify(downloader).fetch(rootns.resolve(ref.toURI()));