
com.github.fge.jsonschema.load.URIManager Maven / Gradle / Ivy
/*
* Copyright (c) 2013, Francis Galiegue
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.github.fge.jsonschema.load;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.load.configuration.LoadingConfiguration;
import com.github.fge.jsonschema.messages.JsonSchemaCoreMessageBundle;
import com.github.fge.jsonschema.report.ProcessingMessage;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
/**
* Class to fetch JSON documents
*
* This uses a map of {@link URIDownloader} instances to fetch the contents
* of a URI as an {@link InputStream}, then tries and turns this content into
* JSON using an {@link ObjectMapper}.
*
* Normally, you will never use this class directly.
*
* @see DefaultDownloadersDictionary
* @see SchemaLoader
*/
public final class URIManager
{
private static final MessageBundle BUNDLE
= MessageBundles.getBundle(JsonSchemaCoreMessageBundle.class);
private final Map downloaders;
private final ObjectReader reader;
public URIManager()
{
this(LoadingConfiguration.byDefault());
}
public URIManager(final LoadingConfiguration cfg)
{
downloaders = cfg.getDownloaders().entries();
reader = cfg.getObjectReader();
}
/**
* Get the content at a given URI as a {@link JsonNode}
*
* @param uri the URI
* @return the content
* @throws NullPointerException provided URI is null
* @throws ProcessingException scheme is not registered, failed to get
* content, or content is not JSON
*/
public JsonNode getContent(final URI uri)
throws ProcessingException
{
BUNDLE.checkNotNull(uri, "jsonRef.nullURI");
if (!uri.isAbsolute())
throw new ProcessingException(new ProcessingMessage()
.setMessage(BUNDLE.getMessage("refProcessing.uriNotAbsolute"))
.put("uri", uri));
final String scheme = uri.getScheme();
final URIDownloader downloader = downloaders.get(scheme);
if (downloader == null)
throw new ProcessingException(new ProcessingMessage()
.setMessage(BUNDLE.getMessage("refProcessing.unhandledScheme"))
.putArgument("scheme", scheme).putArgument("uri", uri));
final InputStream in;
try {
in = downloader.fetch(uri);
return reader.readTree(in);
} catch (JsonProcessingException e) {
throw new ProcessingException(new ProcessingMessage()
.setMessage(BUNDLE.getMessage("refProcessing.uriNotJson"))
.putArgument("uri", uri)
.put("parsingMessage", e.getOriginalMessage()));
} catch (IOException e) {
throw new ProcessingException(new ProcessingMessage()
.setMessage(BUNDLE.getMessage("refProcessing.uriIOError"))
.putArgument("uri", uri)
.put("exceptionMessage", e.getMessage()));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy