Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2012, Francis Galiegue <fgaliegue@gmail.com>
   *
   * 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 <http://www.gnu.org/licenses/>.
  */
 
 package com.github.fge.jsonschema.uri;
 
 
 import java.net.URI;
 import java.util.Map;

Class to fetch JSON documents

This uses a map of URIDownloader instances to fetch the contents of a URI as an java.io.InputStream, then tries and turns this content into JSON using an com.fasterxml.jackson.databind.ObjectMapper.

Normally, you will never use this class directly.

 
 public final class URIManager
 {
    
 
     private static final ObjectMapper MAPPER = JacksonUtils.getMapper();

    
Map of downloaders (schemes as keys, URIDownloader instances as values)
 
     private final Map<StringURIDownloaderdownloaders = 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<URIURIURIRedirections = Maps.newHashMap();
 
     public URIManager()
     {
         .put("http", DefaultURIDownloader.getInstance());
         .put("https", DefaultURIDownloader.getInstance());
         .put("ftp", DefaultURIDownloader.getInstance());
         .put("file", DefaultURIDownloader.getInstance());
         .put("jar", DefaultURIDownloader.getInstance());
         .put("resource", ResourceURIDownloader.getInstance());
     }

    
Register a new downloader for a given URI scheme

Parameters:
scheme the scheme
downloader the URIDownloader instance
Throws:
java.lang.NullPointerException scheme is null
java.lang.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
                + '"');
        }
        .put(schemedownloader);
    }

    
Unregister a downloader for a given scheme

Parameters:
scheme the scheme
Throws:
java.lang.NullPointerException scheme is null
    public void unregisterScheme(final String scheme)
    {
        Preconditions.checkNotNull(scheme"scheme is null");
        .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.

Parameters:
from the original URI
to an URI which is reachable
Throws:
java.lang.NullPointerException from or to is null
java.lang.IllegalArgumentException either from or to are invalid URIs, or are not absolute JSON References
    public void addRedirection(final String fromfinal String to)
    {
        Preconditions.checkNotNull(from"source URI is null");
        Preconditions.checkNotNull(to"destination URI is null");
        final URI sourceURIdestURI;
        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);
        }
        .put(sourceURIdestURI);
    }

    
Get the content at a given URI as a com.fasterxml.jackson.databind.JsonNode

Parameters:
uri the URI
Returns:
the content
Throws:
com.github.fge.jsonschema.main.JsonSchemaException scheme is not registered, failed to get content, or content is not JSON
    public JsonNode getContent(final URI uri)
        throws JsonSchemaException
    {
        Preconditions.checkNotNull(uri"null URI");
        final URI target = .containsKey(uri)
            ? .get(uri) : uri;
        /*
         * All errors at this level are fatal
         */
        final Message.Builder msg = ..newMessage()
            .setKeyword("N/A").addInfo("uri"target).setFatal(true);
        if (!target.isAbsolute())
            throw new JsonSchemaException(msg.setMessage("URI is not absolute")
                .build());
        final String scheme = target.getScheme();
        final URIDownloader downloader = .get(scheme);
        if (downloader == null) {
            msg.setMessage("cannot handle scheme").addInfo("scheme"scheme);
            throw new JsonSchemaException(msg.build());
        }
        final InputStream in;
        try {
            in = downloader.fetch(target);
        } catch (IOException e) {
            msg.setMessage("cannot fetch content from URI");
            throw JsonSchemaException.wrap(msg.build(), e);
        }
        try {
            // Note: ObjectMapper's .readTree() closes the InputStream after it
            // is done with it!
            return .readTree(in);
        } catch (IOException e) {
            msg.setMessage("content fetched from URI is not valid JSON");
            throw JsonSchemaException.wrap(msg.build(), e);
        }
    }
New to GrepCode? Check out our FAQ X