
com.github.fge.jsonschema.processing.ref.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.processing.ref;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.keyword.NumericKeywordValidator;
import com.github.fge.jsonschema.main.JsonSchemaException;
import com.github.fge.jsonschema.processing.ProcessingException;
import com.github.fge.jsonschema.processing.ProcessingMessage;
import com.github.fge.jsonschema.ref.JsonRef;
import com.github.fge.jsonschema.schema.SchemaRegistry;
import com.github.fge.jsonschema.uri.DefaultURIDownloader;
import com.github.fge.jsonschema.uri.ResourceURIDownloader;
import com.github.fge.jsonschema.uri.URIDownloader;
import com.github.fge.jsonschema.util.JsonLoader;
import com.github.fge.jsonschema.util.jackson.JacksonUtils;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
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 SchemaRegistry
* @see JsonLoader
*/
public final class URIManager
{
/**
* Our object mapper
*
* Note that it uses {@link
* DeserializationFeature#USE_BIG_DECIMAL_FOR_FLOATS} to deserialize, for
* accuracy reasons.
*
* @see NumericKeywordValidator
*/
private static final ObjectMapper MAPPER = JacksonUtils.getMapper();
/**
* Map of downloaders (schemes as keys, {@link URIDownloader} instances
* as values)
*/
private final Map downloaders = Maps.newHashMap();
/**
* Map for URI redirections
*
* This map will be used if you wish for an absolute URI to be redirected
* to another URI of your choice, which must also be absolute.
*
* Note that this map requires URIs to be exact. Currently, you
* cannot, for example, redirect a whole namespace this way.
*/
private final Map URIRedirections = Maps.newHashMap();
public URIManager()
{
downloaders.put("http", DefaultURIDownloader.getInstance());
downloaders.put("ftp", DefaultURIDownloader.getInstance());
downloaders.put("file", DefaultURIDownloader.getInstance());
downloaders.put("jar", DefaultURIDownloader.getInstance());
downloaders.put("resource", ResourceURIDownloader.getInstance());
}
/**
* Register a new downloader for a given URI scheme
*
* @param scheme the scheme
* @param downloader the {@link URIDownloader} instance
* @throws NullPointerException scheme is null
* @throws IllegalArgumentException scheme is empty, or is already
* registered
*/
public void registerScheme(final String scheme,
final URIDownloader downloader)
{
Preconditions.checkNotNull(scheme, "scheme is null");
Preconditions.checkArgument(!scheme.isEmpty(), "scheme is empty");
try {
new URI(scheme, "x", "y");
} catch (URISyntaxException ignored) {
throw new IllegalArgumentException("illegal scheme \"" + scheme
+ '"');
}
downloaders.put(scheme, downloader);
}
/**
* Unregister a downloader for a given scheme
*
* @param scheme the scheme
* @throws NullPointerException scheme is null
*/
public void unregisterScheme(final String scheme)
{
Preconditions.checkNotNull(scheme, "scheme is null");
downloaders.remove(scheme);
}
/**
* Add a URI rediction
*
* The typical use case for this is if you have a local copy of a schema
* whose id is normally unreachable. You can transform all references to
* this schema's URI to another URI which is reachable by your application.
*
*
* Note that the given strings will be considered as JSON References, and
* that both arguments must be valid absolute JSON References. For the
* recall, there is more to it than URIs being absolute: their fragment part
* must also be empty or null.
*
* @param from the original URI
* @param to an URI which is reachable
* @throws NullPointerException {@code from} or {@code to} is null
* @throws IllegalArgumentException either {@code from} or {@code to} are
* invalid URIs, or are not absolute JSON References
*/
public void addRedirection(final String from, final String to)
{
Preconditions.checkNotNull(from, "source URI is null");
Preconditions.checkNotNull(to, "destination URI is null");
final URI sourceURI, destURI;
JsonRef ref;
String errmsg;
errmsg = "source URI " + from + " is not an absolute JSON reference";
try {
ref = JsonRef.fromString(from);
Preconditions.checkArgument(ref.isAbsolute(), errmsg);
sourceURI = ref.getLocator();
} catch (JsonSchemaException ignored) {
throw new IllegalArgumentException(errmsg);
}
errmsg = "destination URI " + to + " is not an absolute JSON reference";
try {
ref = JsonRef.fromString(to);
Preconditions.checkArgument(ref.isAbsolute(), errmsg);
destURI = ref.getLocator();
} catch (JsonSchemaException ignored) {
throw new IllegalArgumentException(errmsg);
}
URIRedirections.put(sourceURI, destURI);
}
/**
* Get the content at a given URI as a {@link JsonNode}
*
* @param uri the URI
* @return the content
* @throws ProcessingException scheme is not registered, failed to get
* content, or content is not JSON
*/
public JsonNode getContent(final URI uri)
throws ProcessingException
{
Preconditions.checkNotNull(uri, "null URI");
final URI target = URIRedirections.containsKey(uri)
? URIRedirections.get(uri) : uri;
final ProcessingMessage msg = new ProcessingMessage()
.put("uri", uri);
if (!target.isAbsolute())
throw new ProcessingException(msg.msg("URI is not absolute"));
final String scheme = target.getScheme();
final URIDownloader downloader = downloaders.get(scheme);
if (downloader == null)
throw new ProcessingException(msg.msg("cannot handle scheme")
.put("scheme", scheme));
final InputStream in;
try {
in = downloader.fetch(target);
return MAPPER.readTree(in);
} catch (JsonProcessingException e) {
throw new ProcessingException(msg
.msg("content at URI is not valid JSON")
.put("parsingMessage", e.getOriginalMessage()));
} catch (IOException e) {
throw new ProcessingException(msg
.msg("cannot dereference URI (IOException)")
.put("exceptionMessage", e.getMessage()));
}
}
}