Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 19 additions & 65 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.google.common.annotations.VisibleForTesting;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.sun.jersey.core.header.ContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
Expand Down Expand Up @@ -154,11 +155,7 @@ public Response head(@PathParam("path") final List<PathSegment> pathList,
@Context final Request request,
@Context final HttpServletResponse servletResponse,
@Context final UriInfo uriInfo) throws RepositoryException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("HEAD request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "HEAD");

final String path = toPath(pathList);
LOGGER.trace("Getting head for: {}", path);
Expand Down Expand Up @@ -198,12 +195,7 @@ public RdfStream describe(@PathParam("path") final List<PathSegment> pathList,
@Context final Request request,
@Context final HttpServletResponse servletResponse,
@Context final UriInfo uriInfo) throws RepositoryException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("MOVE request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}

throwIfPathIncludesJcr(pathList, "MOVE");

final String path = toPath(pathList);
LOGGER.trace("Getting profile for: {}", path);
Expand Down Expand Up @@ -362,11 +354,7 @@ public Response updateSparql(@PathParam("path")
final InputStream requestBodyStream,
@Context final Request request, @Context final HttpServletResponse servletResponse)
throws RepositoryException, IOException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("PATCH request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "PATCH");

final String path = toPath(pathList);
LOGGER.debug("Attempting to update path: {}", path);
Expand Down Expand Up @@ -431,11 +419,7 @@ public Response createOrReplaceObjectRdf(
@Context final Request request,
@Context final HttpServletResponse servletResponse) throws RepositoryException, ParseException,
IOException, InvalidChecksumException, URISyntaxException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("PUT request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "PUT");

final String path = toPath(pathList);
LOGGER.debug("Attempting to replace path: {}", path);
Expand Down Expand Up @@ -519,11 +503,7 @@ public Response createObject(@PathParam("path")
final UriInfo uriInfo, final InputStream requestBodyStream)
throws RepositoryException, ParseException, IOException,
InvalidChecksumException, URISyntaxException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("POST request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "POST");

String pid;
final String newObjectPath;
Expand Down Expand Up @@ -723,11 +703,7 @@ public Response createObjectFromFormPost(
@Context final UriInfo uriInfo,
@FormDataParam("file") final InputStream file
) throws RepositoryException, URISyntaxException, InvalidChecksumException, ParseException, IOException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("POST request with multipart attachment with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "POST with multipart attachment");

final MediaType effectiveContentType = file == null ? null : MediaType.APPLICATION_OCTET_STREAM_TYPE;
return createObject(pathList, mixin, null, null, effectiveContentType, slug, servletResponse, uriInfo, file);
Expand All @@ -746,11 +722,7 @@ public Response createObjectFromFormPost(
public Response deleteObject(@PathParam("path")
final List<PathSegment> pathList,
@Context final Request request) throws RepositoryException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("DELETE request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "DELETE");

try {

Expand Down Expand Up @@ -794,11 +766,7 @@ public Response deleteObject(@PathParam("path")
public Response copyObject(@PathParam("path") final List<PathSegment> path,
@HeaderParam("Destination") final String destinationUri)
throws RepositoryException, URISyntaxException {
if (checkPathJcrContent(path)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("COPY request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(path, "COPY");

try {

Expand Down Expand Up @@ -845,11 +813,7 @@ public Response moveObject(@PathParam("path") final List<PathSegment> pathList,
@HeaderParam("Destination") final String destinationUri,
@Context final Request request)
throws RepositoryException, URISyntaxException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("MOVE request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "MOVE");

try {

Expand Down Expand Up @@ -903,11 +867,7 @@ public Response moveObject(@PathParam("path") final List<PathSegment> pathList,
public Response options(@PathParam("path") final List<PathSegment> pathList,
@Context final HttpServletResponse servletResponse)
throws RepositoryException {
if (checkPathJcrContent(pathList)) {
final String requestPath = uriInfo.getPath();
LOGGER.trace("OPTIONS request with jcr namespace is not allowed: {} ", requestPath);
responseNotFound();
}
throwIfPathIncludesJcr(pathList, "OPTIONS");

addOptionsHttpHeaders(servletResponse);
return status(OK).build();
Expand All @@ -916,24 +876,18 @@ public Response options(@PathParam("path") final List<PathSegment> pathList,
/**
* Method to check for any jcr namespace element in the path
*/
public static boolean checkPathJcrContent(final List<PathSegment> pathList) {
final int pathSize = pathList.size();
if (pathList == null || pathSize == 0) {
return false;
@VisibleForTesting
protected void throwIfPathIncludesJcr(final List<PathSegment> pathList, final String msg) {
if (pathList == null || pathList.size() == 0) {
return;
}
final PathSegment pathSegment = pathList.get(pathSize - 1);
final PathSegment pathSegment = pathList.get(pathList.size() - 1);
final String[] tokens = pathSegment.getPath().split(":");
if (tokens.length == 2 && tokens[0].equalsIgnoreCase("jcr")) {
return true;
final String requestPath = uriInfo.getPath();
LOGGER.trace("{} request with jcr namespace is not allowed: {} ", msg, requestPath);
throw new WebApplicationException(notFound().build());
}
return false;
}

/**
* Send response not found
* @param path
*/
public static void responseNotFound() {
throw new WebApplicationException(notFound().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,11 @@ public void testHead() throws RepositoryException {
assertEquals(SC_OK, response.getStatus());
}

@Test
@Test(expected = WebApplicationException.class)
public void testCheckJcrNamespace() {
final List<PathSegment> pathList = createPathList("anypath");
pathList.add(createPathList("jcr:path").get(0));
assertTrue(FedoraNodes.checkPathJcrContent(pathList));
testObj.throwIfPathIncludesJcr(pathList, "TEST");
}

@Test(expected = WebApplicationException.class)
public void testResonseNotFound() {
FedoraNodes.responseNotFound();
}
}