Skip to content

Commit

Permalink
Replace toPath() with a call to the IdentifierConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 6, 2014
1 parent 076c689 commit 7fda27e
Show file tree
Hide file tree
Showing 36 changed files with 296 additions and 616 deletions.
Expand Up @@ -15,7 +15,10 @@
*/
package org.fcrepo.auth.integration;

import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
Expand All @@ -28,10 +31,8 @@
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

Expand All @@ -54,15 +55,15 @@ public class RootTestResource extends AbstractResource {
private static final Logger LOGGER = getLogger(RootTestResource.class);

@PUT
public Response put(@PathParam("path") final List<PathSegment> pathList) throws Exception {
final String path = toPath(pathList);
public Response put(@PathParam("path") final String externalPath) throws Exception {
final String path = toPath(translator(), externalPath);
LOGGER.trace("PUT: {}", path);
return doRequest(path);
}

@POST
public Response post(@PathParam("path") final List<PathSegment> pathList) throws Exception {
final String path = toPath(pathList);
public Response post(@PathParam("path") final String externalPath) throws Exception {
final String path = toPath(translator(), externalPath);
LOGGER.trace("POST: {}", path);
return doRequest(path);
}
Expand All @@ -74,4 +75,9 @@ private Response doRequest(final String path) throws RepositoryException {
return Response.created(location).build();
}

protected IdentifierConverter<Resource,Node> translator() {
return new UriAwareIdentifierConverter(session,
uriInfo.getBaseUriBuilder().clone().path(RootTestResource.class));
}

}
1 change: 0 additions & 1 deletion fcrepo-generator-dc/pom.xml
Expand Up @@ -49,7 +49,6 @@
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fcrepo</groupId>
Expand Down
Expand Up @@ -19,7 +19,6 @@
import static javax.ws.rs.core.Response.ok;

import java.io.InputStream;
import java.util.List;

import javax.inject.Inject;
import javax.jcr.PathNotFoundException;
Expand All @@ -29,11 +28,10 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;

import org.fcrepo.generator.dublincore.DublinCoreGenerators;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.api.FedoraBaseResource;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.generator.dublincore.DCGenerator;
import org.springframework.context.annotation.Scope;
Expand All @@ -46,7 +44,7 @@
*/
@Scope("prototype")
@Path("/{path: .*}/oai:dc")
public class DublinCoreGenerator extends AbstractResource {
public class DublinCoreGenerator extends FedoraBaseResource {

@Inject
DublinCoreGenerators dcgenerators;
Expand All @@ -56,19 +54,16 @@ public class DublinCoreGenerator extends AbstractResource {

/**
* Get Dublin Core XML for a node
* @param pathList
* @param externalPath
* @return response
* @throws RepositoryException
*/
@GET
@Produces(TEXT_XML)
public Response getObjectAsDublinCore(
@PathParam("path")
final List<PathSegment> pathList) throws RepositoryException {
public Response getObjectAsDublinCore(@PathParam("path") final String externalPath) throws RepositoryException {

try {
final String path = toPath(pathList);
final FedoraResource obj = nodeService.getObject(session, path);
final FedoraResource obj = getResourceFromPath(externalPath);

for (final DCGenerator indexer : dcgenerators) {
final InputStream inputStream =
Expand All @@ -86,4 +81,8 @@ public Response getObjectAsDublinCore(

}

@Override
protected Session session() {
return session;
}
}
Expand Up @@ -16,11 +16,9 @@
package org.fcrepo.generator;

import static java.util.Arrays.asList;
import static org.fcrepo.http.commons.test.util.PathSegmentImpl.createPathList;
import static org.fcrepo.http.commons.test.util.TestHelpers.mockSession;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.util.ReflectionTestUtils.setField;
Expand All @@ -35,7 +33,6 @@

import org.fcrepo.generator.dublincore.DCGenerator;
import org.fcrepo.generator.dublincore.DublinCoreGenerators;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.fcrepo.kernel.services.NodeService;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -61,10 +58,13 @@ public class DublinCoreGeneratorTest {
@Mock
private InputStream mockIS;

@Mock
private Node mockNode;

@Before
public void setUp() {
initMocks(this);
testObj = new DublinCoreGenerator();
testObj = spy(new DublinCoreGenerator());
setField(testObj, "nodeService", mockNodeService);
mockSession = mockSession(testObj);
setField(testObj, "session", mockSession);
Expand All @@ -73,20 +73,18 @@ public void setUp() {

@Test
public void testGetObjectAsDublinCore() throws RepositoryException {
final FedoraResourceImpl mockResource = mock(FedoraResourceImpl.class);
when(mockResource.getNode()).thenReturn(mock(Node.class));
when(mockNodeService.getObject(mockSession, "/objects/foo"))
.thenReturn(mockResource);
when(mockGenerator.getStream(any(Node.class))).thenReturn(mockIS);
testObj.getObjectAsDublinCore(createPathList("objects", "foo"));
when(mockSession.getNode("/objects/foo")).thenReturn(mockNode);
when(mockGenerator.getStream(mockNode)).thenReturn(mockIS);
testObj.getObjectAsDublinCore("objects/foo");

}

@Test
public void testNoGenerators() {
public void testNoGenerators() throws RepositoryException {
when(mockSession.getNode("/objects/foo")).thenReturn(mockNode);
testObj.dcgenerators = new DublinCoreGenerators(Collections.<DCGenerator>emptyList());
try {
testObj.getObjectAsDublinCore(createPathList("objects", "foo"));
testObj.getObjectAsDublinCore("objects/foo");
fail("Should have failed without a generator configured!");
} catch (final PathNotFoundException ex) {
// this is what we expect
Expand Down
Expand Up @@ -48,7 +48,6 @@
import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
Expand All @@ -58,7 +57,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.List;

import static com.google.common.collect.Iterators.concat;
import static com.google.common.collect.Iterators.transform;
Expand Down Expand Up @@ -89,8 +87,7 @@ public abstract class ContentExposingResource extends FedoraBaseResource {

private static long MAX_BUFFER_SIZE = 10240000;

abstract String path();
abstract List<PathSegment> pathList();
abstract String externalPath();

abstract void addResourceHttpHeaders(FedoraResource resource);

Expand Down Expand Up @@ -313,7 +310,7 @@ protected URI getUri(final FedoraResource resource) {

protected FedoraResource resource() {
if (resource == null) {
resource = getResourceFromPath(pathList(), path());
resource = getResourceFromPath(externalPath());
}

return resource;
Expand Down
Expand Up @@ -15,22 +15,18 @@
*/
package org.fcrepo.http.api;

import com.google.common.annotations.VisibleForTesting;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.DatastreamImpl;
import org.fcrepo.kernel.impl.FedoraBinaryImpl;
import org.fcrepo.kernel.impl.FedoraObjectImpl;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.PathSegment;

import java.util.List;

import static org.fcrepo.jcr.FedoraJcrTypes.FCR_METADATA;

Expand All @@ -53,31 +49,33 @@ protected IdentifierConverter<Resource,Node> translator() {
return identifierTranslator;
}

protected FedoraResource getResourceFromPath(final List<PathSegment> pathList, final String path) {
/**
* Get the FedoraResource for the resource at the external path
* @param externalPath
* @return
*/
@VisibleForTesting
public FedoraResource getResourceFromPath(final String externalPath) {
final FedoraResource resource;
try {
final boolean metadata = pathList != null
&& pathList.get(pathList.size() - 1).getPath().equals(FCR_METADATA);
final boolean metadata = externalPath != null
&& externalPath.endsWith(FCR_METADATA);

final Node node = session().getNode(path);
final Node node = translator().convert(translator().toDomain(externalPath));

if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);
if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);

if (metadata) {
resource = datastream;
} else {
resource = datastream.getBinary();
}
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
if (metadata) {
resource = datastream;
} else {
resource = new FedoraObjectImpl(node);
resource = datastream.getBinary();
}
return resource;
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
} else {
resource = new FedoraObjectImpl(node);
}
return resource;
}

}
32 changes: 10 additions & 22 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraBatch.java
Expand Up @@ -18,7 +18,7 @@
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.notAcceptable;
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
Expand All @@ -39,7 +39,6 @@
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
Expand All @@ -56,7 +55,6 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
Expand Down Expand Up @@ -103,9 +101,7 @@ public class FedoraBatch extends ContentExposingResource {

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

@PathParam("path") protected List<PathSegment> pathList;

protected String path;
@PathParam("path") protected String externalPath;


/**
Expand All @@ -117,16 +113,11 @@ public FedoraBatch() {

/**
* Create a new FedoraNodes instance for a given path
* @param path
* @param externalPath
*/
@VisibleForTesting
public FedoraBatch(final String path) {
this.path = path;
}

@PostConstruct
private void postConstruct() {
this.path = toPath(pathList);
public FedoraBatch(final String externalPath) {
this.externalPath = externalPath;
}

/**
Expand Down Expand Up @@ -173,6 +164,8 @@ private void postConstruct() {
public Response batchModify(final MultiPart multipart)
throws InvalidChecksumException, IOException, URISyntaxException {

final String path = toPath(translator(), externalPath);

// TODO: this is ugly, but it works.
final PathFactory pathFactory = new ExecutionContext().getValueFactories().getPathFactory();
final org.modeshape.jcr.value.Path jcrPath = pathFactory.create(path);
Expand Down Expand Up @@ -322,7 +315,7 @@ public Response batchModify(final MultiPart multipart)
throw new RepositoryRuntimeException(e);
}

return created(new URI(path)).build();
return noContent().build();

} finally {
session.logout();
Expand Down Expand Up @@ -448,12 +441,7 @@ void addResourceHttpHeaders(final FedoraResource resource) {
}

@Override
String path() {
return path;
}

@Override
List<PathSegment> pathList() {
return pathList;
String externalPath() {
return externalPath;
}
}

0 comments on commit 7fda27e

Please sign in to comment.