/*
 * Licensed to DuraSpace under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * DuraSpace licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.kernel.modeshape.utils;

import static java.util.Objects.requireNonNull;
import static java.util.Arrays.stream;

import java.util.HashMap;
import java.util.Map;

import javax.jcr.NamespaceException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.kernel.api.exception.FedoraInvalidNamespaceException;
import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;

/**
 * Tools for working with the JCR Namespace Registry
 * @author Benjamin Armintor
 * @author acoburn
 * @author ajs6f
 * @since May 13, 2013
 */
public final class NamespaceTools {

    private NamespaceTools() {
    }

    /**
     * Return the {@link NamespaceRegistry} associated with the arg session.
     *
     * @param session containing the NamespaceRegistry
     * @return NamespaceRegistry
     */
    public static NamespaceRegistry getNamespaceRegistry(final Session session) {
        try {
            return requireNonNull(session.getWorkspace().getNamespaceRegistry(),
                    "Couldn't find namespace registry in repository!");
        } catch (final RepositoryException e) {
            throw new RepositoryRuntimeException(e);
        }
    }

    /**
     * Validate resource path for unregistered namespace prefixes
     *
     * @param session the JCR session to use
     * @param path the absolute path to the object
     * @throws org.fcrepo.kernel.api.exception.FedoraInvalidNamespaceException on unregistered namespaces
     * @throws org.fcrepo.kernel.api.exception.RepositoryRuntimeException if repository runtime exception occurred
     */
    public static void validatePath(final Session session, final String path) {

        final NamespaceRegistry namespaceRegistry = getNamespaceRegistry(session);
        final String[] pathSegments = path.replaceAll("^/+", "").replaceAll("/+$", "").split("/");
        for (final String segment : pathSegments) {
            final int colonPosition = segment.indexOf(':');
            if (segment.length() > 0 && colonPosition > -1) {
                final String prefix = segment.substring(0, colonPosition);
                if (!prefix.equals("fedora")) {
                    if (prefix.length() == 0) {
                        throw new FedoraInvalidNamespaceException("Empty namespace in " + segment);
                    }
                    try {
                        namespaceRegistry.getURI(prefix);
                    } catch (final NamespaceException e) {
                        throw new FedoraInvalidNamespaceException("Prefix " + prefix + " has not been registered", e);
                    } catch (final RepositoryException e) {
                        throw new RepositoryRuntimeException(e);
                    }
                }
            }
        }
    }

    /**
     * Retrieve the namespaces as a Map
     *
     * @param session the JCR session to use
     * @return a mapping of the prefix to URI
     */
    public static Map<String, String> getNamespaces(final Session session) {
        final NamespaceRegistry registry = getNamespaceRegistry(session);
        final Map<String, String> namespaces = new HashMap<>();

        try {
            stream(registry.getPrefixes()).filter(x -> !x.isEmpty()).forEach(x -> {
                try {
                    namespaces.put(x, registry.getURI(x));
                } catch (final RepositoryException e) {
                    throw new RepositoryRuntimeException(e);
                }
            });
        } catch (final RepositoryException e) {
            throw new RepositoryRuntimeException(e);
        }
        return namespaces;
    }
}

@@ -816,16 +816,73 @@
 utils;%0A%0A
+import static com.google.common.collect.ImmutableSet.of;%0A
 import s
@@ -964,55 +964,331 @@
 am;%0A
-%0Aimport java.util.HashMap;%0Aimport java.util.Map
+import static javax.jcr.NamespaceRegistry.PREFIX_EMPTY;%0Aimport static javax.jcr.NamespaceRegistry.PREFIX_JCR;%0Aimport static javax.jcr.NamespaceRegistry.PREFIX_MIX;%0Aimport static javax.jcr.NamespaceRegistry.PREFIX_NT;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.function.Predicate
 ;%0A%0Ai
@@ -1780,32 +1780,239 @@
 ools() %7B%0A    %7D%0A%0A
+    // Internal namespaces defined in the modeshape CND file%0A    private static final Set%3CString%3E INTERNAL_PREFIXES = of(PREFIX_EMPTY, PREFIX_JCR, PREFIX_MIX, PREFIX_NT,%0A            %22mode%22, %22sv%22, %22image%22);%0A%0A
     /**%0A     * R
@@ -4582,23 +4582,29 @@
 ter(
-x -%3E !x.isEmpty
+internalPrefix.negate
 ()).
@@ -4993,11 +4993,95 @@
 s;%0A    %7D
+%0A%0A    private static Predicate%3CString%3E internalPrefix = INTERNAL_PREFIXES::contains;
 %0A%7D%0A

Exclude internal (jcr, mode) prefixes from namespace map (#1105)