Skip to content

Commit

Permalink
Add prefix check before update properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nianma authored and Andrew Woods committed Nov 29, 2015
1 parent ab14a0b commit 8057124
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
@@ -0,0 +1,47 @@
/**
* 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.http.commons.exceptionhandlers;

import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static org.slf4j.LoggerFactory.getLogger;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.fcrepo.kernel.api.exception.InvalidPrefixException;

import org.slf4j.Logger;

/**
* For invalid namespace exceptions on CRUD actions for nodes/datastreams
*
* @author nianma
* @since November 2, 2015
*/
@Provider
public class InvalidPrefixExceptionMapper implements ExceptionMapper<InvalidPrefixException> {

private static final Logger LOGGER = getLogger(InvalidPrefixExceptionMapper.class);

@Override
public Response toResponse(final InvalidPrefixException e) {
LOGGER.trace("FedoraInvalidPrefixExceptionMapper caught an exception: {}", e.getMessage());
return status(BAD_REQUEST).entity(e.getMessage()).build();
}

}
@@ -0,0 +1,48 @@
/**
* 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.api.exception;


/**
* Indicates a prefix used in a CRUD request has existed in the repository
*
* @author nianma
* @since Nov 2, 2015
*/
public class InvalidPrefixException extends RepositoryRuntimeException {

private static final long serialVersionUID = 1L;

/**
* Ordinary constructor
*
* @param msg the message
*/
public InvalidPrefixException(final String msg) {
super(msg);
}

/**
* Ordinary constructor
*
* @param msg the message
* @param rootCause the root cause
*/
public InvalidPrefixException(final String msg, final Throwable rootCause) {
super(msg, rootCause);
}

}
Expand Up @@ -37,6 +37,7 @@
import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isFrozenNode;
import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isInternalNode;
import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isInternalType;
import static org.fcrepo.kernel.modeshape.utils.NamespaceTools.getNamespaceRegistry;
import static org.fcrepo.kernel.modeshape.utils.UncheckedFunction.uncheck;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -68,6 +69,7 @@
import javax.jcr.nodetype.NodeType;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.NamespaceRegistry;

import com.google.common.base.Converter;
import com.google.common.collect.Iterators;
Expand All @@ -78,6 +80,7 @@
import org.fcrepo.kernel.api.models.FedoraBinary;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.exception.ConstraintViolationException;
import org.fcrepo.kernel.api.exception.InvalidPrefixException;
import org.fcrepo.kernel.api.exception.MalformedRdfException;
import org.fcrepo.kernel.api.exception.PathNotFoundRuntimeException;
import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
Expand Down Expand Up @@ -448,6 +451,25 @@ public void updateProperties(final IdentifierConverter<Resource, FedoraResource>

final Collection<IllegalArgumentException> errors = checkInvalidPredicates(request);

final NamespaceRegistry namespaceRegistry = getNamespaceRegistry(getSession());

request.getPrefixMapping().getNsPrefixMap().forEach(
(k,v) -> {
try {
LOGGER.debug("Prefix mapping is key:{} -> value:{}", k, v);
if (Arrays.asList(namespaceRegistry.getPrefixes()).contains(k)
&& !v.equals(namespaceRegistry.getURI(k))) {

final String namespaceURI = namespaceRegistry.getURI(k);
LOGGER.debug("Prefix has already been defined: {}:{}", k, namespaceURI);
throw new InvalidPrefixException("Prefix already exists as: " + k + " -> " + namespaceURI);
}

} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
});

if (!errors.isEmpty()) {
throw new IllegalArgumentException(errors.stream().map(Exception::getMessage).collect(joining(",\n")));
}
Expand Down
Expand Up @@ -80,6 +80,7 @@

import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.api.exception.InvalidChecksumException;
import org.fcrepo.kernel.api.exception.InvalidPrefixException;
import org.fcrepo.kernel.api.exception.MalformedRdfException;
import org.fcrepo.kernel.api.models.NonRdfSourceDescription;
import org.fcrepo.kernel.api.models.Container;
Expand Down Expand Up @@ -499,6 +500,23 @@ public void testInvalidSparqlUpdateValidation() throws RepositoryException {
new RdfStream());
}

@Test (expected = InvalidPrefixException.class)
public void testInvalidPrefixSparqlUpdateValidation() throws RepositoryException {
final String pid = UUID.randomUUID().toString();
final FedoraResource object =
containerService.findOrCreate(session, pid);
object.updateProperties(
subjects,
"PREFIX pcdm: <http://pcdm.org/models#>\n"
+ "INSERT { <> a pcdm:Object}\n"
+ "WHERE { }", new RdfStream());
object.updateProperties(
subjects,
"PREFIX pcdm: <http://garbage.org/models#>\n"
+ "INSERT { <> a pcdm:Garbage}\n"
+ "WHERE { }", new RdfStream());
}

@Test
public void testValidSparqlUpdateWithLiteralTrailingSlash() throws RepositoryException {
final String pid = UUID.randomUUID().toString();
Expand Down

0 comments on commit 8057124

Please sign in to comment.