/*
 * RESTHeart - the Web API for MongoDB
 * Copyright (C) SoftInstigate Srl
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.restheart.handlers.schema;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.bson.BsonDocument;
import org.everit.json.schema.loader.SchemaClient;
import org.everit.json.schema.loader.internal.DefaultSchemaClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A json-schema client that loads the schema from the db if the url refer to a
 * schema store. If not it delegates to the DefaultSchemaClient.
 *
 * @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
 */
public class SchemaStoreClient implements SchemaClient {

    static final Logger LOGGER
            = LoggerFactory.getLogger(SchemaStoreClient.class);

    private static final SchemaClient WRAPPED = new DefaultSchemaClient();

    public SchemaStoreClient() {
    }

    @Override
    public InputStream get(String uri) {
        LOGGER.trace("@@@ loading schema {}", uri);

        if (isLocalSchemaStore(uri)) {
            try {
                SchemaStoreURL _uri = new SchemaStoreURL(uri);

                BsonDocument s = JsonSchemaCacheSingleton
                        .getInstance()
                        .getRaw(
                                _uri.getSchemaDb(),
                                _uri.getSchemaId());

                return new ByteArrayInputStream(s.toString().getBytes());
            } catch (JsonSchemaNotFoundException ex) {
                throw new RuntimeException(ex);
            }
        } else {
            return WRAPPED.get(uri);
        }
    }

    @Override
    public InputStream apply(String url) {
        return get(url);
    }

    /**
     *
     * @param id
     * @return true if the given id is a schema store of this server
     */
    private boolean isLocalSchemaStore(String id) {
        return SchemaStoreURL.isValid(id);
    }
}

@@ -72,18 +72,17 @@
 e Srl%0A *
- 
 %0A
+
  * This 
@@ -330,25 +330,24 @@
  version.%0A *
- 
 %0A * This pro
@@ -590,17 +590,16 @@
 ails.%0A *
- 
 %0A * You 
@@ -817,24 +817,24 @@
 nputStream;%0A
-
 import java.
@@ -845,24 +845,101 @@
 nputStream;%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.util.logging.Level;%0A
 import org.b
@@ -2159,16 +2159,23 @@
 etBytes(
+%22UTF-8%22
 ));%0A    
@@ -2219,16 +2219,47 @@
 ception 
+%7C UnsupportedEncodingException 
 ex) %7B%0A  

Fix reliance on default encoding