Skip to content

Commit

Permalink
Move modeshape-specific code out of the kernel-api
Browse files Browse the repository at this point in the history
  • Loading branch information
acoburn authored and Andrew Woods committed Nov 11, 2015
1 parent 973161b commit f04635c
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 128 deletions.
Expand Up @@ -22,11 +22,11 @@
import static org.apache.commons.lang3.StringUtils.replaceOnce;
import static org.fcrepo.kernel.api.FedoraJcrTypes.FCR_METADATA;
import static org.fcrepo.kernel.api.FedoraJcrTypes.FCR_VERSIONS;
import static org.fcrepo.kernel.api.utils.NamespaceTools.validatePath;
import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeConverter;
import static org.fcrepo.kernel.modeshape.services.TransactionServiceImpl.getCurrentTransactionId;
import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getClosestExistingAncestor;
import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isFrozenNode;
import static org.fcrepo.kernel.modeshape.utils.NamespaceTools.validatePath;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext;
Expand Down
4 changes: 0 additions & 4 deletions fcrepo-kernel-api/pom.xml
Expand Up @@ -61,10 +61,6 @@
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
</dependency>
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
</dependency>
<dependency>
<groupId>org.modeshape</groupId>
<artifactId>modeshape-jcr-api</artifactId>
Expand Down

This file was deleted.

Expand Up @@ -27,8 +27,8 @@

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import static org.fcrepo.kernel.api.RdfLexicon.jcrProperties;
import static org.fcrepo.kernel.modeshape.utils.NamespaceTools.getNamespaceRegistry;
import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.fcrepo.kernel.api.utils.NamespaceTools.getNamespaceRegistry;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand Down
Expand Up @@ -36,7 +36,7 @@
import static org.fcrepo.kernel.modeshape.rdf.JcrRdfTools.getRDFNamespaceForJcrNamespace;
import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getReferencePropertyOriginalName;
import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isInternalReferenceProperty;
import static org.fcrepo.kernel.api.utils.NamespaceTools.getNamespaceRegistry;
import static org.fcrepo.kernel.modeshape.utils.NamespaceTools.getNamespaceRegistry;
import static org.slf4j.LoggerFactory.getLogger;


Expand Down Expand Up @@ -94,7 +94,9 @@ public static String getPropertyNameFromPredicate(final Node node,
final Resource predicate,
final Map<String, String> namespaceMapping)
throws RepositoryException {
final NamespaceRegistry namespaceRegistry = getNamespaceRegistry.apply(node);

final NamespaceRegistry namespaceRegistry = (NamespaceRegistry)getNamespaceRegistry(node.getSession());

return getPropertyNameFromPredicate(namespaceRegistry,
predicate, namespaceMapping);
}
Expand Down
Expand Up @@ -16,7 +16,7 @@
package org.fcrepo.kernel.modeshape.services;

import static org.fcrepo.kernel.api.FedoraJcrTypes.FEDORA_TOMBSTONE;
import static org.fcrepo.kernel.api.utils.NamespaceTools.validatePath;
import static org.fcrepo.kernel.modeshape.utils.NamespaceTools.validatePath;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
Expand Down
@@ -0,0 +1,86 @@
/**
* Copyright 2015 DuraSpace, Inc.
*
* Licensed 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 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);
}
}
}
}
}
}
Expand Up @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.kernel.api.utils;
package org.fcrepo.kernel.modeshape.utils;

import static org.fcrepo.kernel.api.utils.NamespaceTools.getNamespaceRegistry;
import static org.fcrepo.kernel.api.utils.NamespaceTools.validatePath;
import static org.fcrepo.kernel.modeshape.utils.NamespaceTools.validatePath;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

Expand Down Expand Up @@ -59,11 +58,6 @@ public void setUp() throws RepositoryException {
when(mockSession.getWorkspace()).thenReturn(mockWork);
}

@Test
public void testFunction() {
getNamespaceRegistry.apply(mockNode);
}

@Test (expected = NullPointerException.class)
public void testNullNamespaceRegistry() {
validatePath(mockSession, "irrelevant");
Expand Down

0 comments on commit f04635c

Please sign in to comment.