Skip to content

Commit

Permalink
move weird object/datastream creation logic up to AbstractResource wh…
Browse files Browse the repository at this point in the history
…ere it can be shared between the 3 classes that use it
  • Loading branch information
cbeer committed May 16, 2013
1 parent cdec32f commit 4075e2c
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 83 deletions.
21 changes: 1 addition & 20 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
Expand Up @@ -230,27 +230,8 @@ public Response createObject(@PathParam("path")
path + " is an existing resource").build();
}

if (FedoraJcrTypes.FEDORA_OBJECT.equals(mixin)) {
final FedoraObject result =
objectService.createObject(session, path);

if (requestBodyStream != null &&
requestContentType != null &&
requestContentType.toString().equals(
WebContent.contentTypeSPARQLUpdate)) {
result.updateGraph(IOUtils.toString(requestBodyStream));
}
createObjectOrDatastreamFromRequestContent(session, path, mixin, requestBodyStream, requestContentType, checksumType, checksum);

}
if (FedoraJcrTypes.FEDORA_DATASTREAM.equals(mixin)) {
final MediaType contentType =
requestContentType != null ? requestContentType
: APPLICATION_OCTET_STREAM_TYPE;

datastreamService.createDatastreamNode(session, path,
contentType.toString(), requestBodyStream,
checksumType, checksum);
}
session.save();
logger.debug("Finished creating {} with path: {}", mixin, path);
return created(uriInfo.getRequestUri()).entity(path).build();
Expand Down
@@ -1,26 +1,31 @@
package org.fcrepo.api;

import static javax.ws.rs.core.Response.created;
import static org.slf4j.LoggerFactory.getLogger;

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

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;

import org.apache.http.HttpStatus;
import org.fcrepo.AbstractResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableList;


@Component
Expand All @@ -29,48 +34,39 @@ public class FedoraUnnamedObjects extends AbstractResource {

private static final Logger logger = getLogger(FedoraUnnamedObjects.class);

@Autowired
FedoraDatastreams datastreamsResource;

@Autowired
FedoraNodes objectsResource;

/**
* Create an anonymous object with a newly minted name
* @param pathList
* @return 201
*/
@POST
public Response ingestAndMint(@PathParam("path")
final List<PathSegment> pathList) throws RepositoryException {
logger.debug("Creating a new unnamed object");
public Response ingestAndMint(@PathParam("path") final List<PathSegment> pathList,
@QueryParam("mixin") @DefaultValue(FedoraJcrTypes.FEDORA_OBJECT) String mixin,
@QueryParam("checksumType") final String checksumType,
@QueryParam("checksum") final String checksum,
@HeaderParam("Content-Type") final MediaType requestContentType,
final InputStream requestBodyStream) throws RepositoryException, IOException, InvalidChecksumException {
final String pid = pidMinter.mintPid();
PathSegment path = new PathSegment() {

@Override
public String getPath() {
return pid;
}
String path = toPath(pathList) + "/" + pid;

logger.debug("Attempting to ingest with path: {}", path);

@Override
public MultivaluedMap<String, String> getMatrixParameters() {
return null;
final Session session = getAuthenticatedSession();

try {
if (nodeService.exists(session, path)) {
return Response.status(HttpStatus.SC_CONFLICT).entity(path + " is an existing resource").build();
}

};
createObjectOrDatastreamFromRequestContent(session, path, mixin, requestBodyStream, requestContentType, checksumType, checksum);

ImmutableList.Builder<PathSegment> segments = ImmutableList.builder();
segments.addAll(pathList);
segments.add(path);
session.save();
logger.debug("Finished creating {} with path: {}", mixin, path);
return created(uriInfo.getRequestUri()).entity(path).build();

try {
return objectsResource.createObject(
segments.build(),
FedoraJcrTypes.FEDORA_OBJECT, null, null, null, null);
} catch (IOException e) {
throw new RepositoryException(e.getMessage(), e);
} catch (InvalidChecksumException e) {
throw new RepositoryException(e.getMessage(), e);
} finally {
session.logout();
}
}

Expand Down
@@ -0,0 +1,65 @@
package org.fcrepo.api.repository;

import org.apache.http.HttpStatus;
import org.fcrepo.AbstractResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;

import static javax.ws.rs.core.Response.created;
import static org.slf4j.LoggerFactory.getLogger;

@Component
@Path("/rest/fcr:new")
public class FedoraRepositoryUnnamedObjects extends AbstractResource {

private static final Logger logger = getLogger(FedoraRepositoryUnnamedObjects.class);

/**
* Create an anonymous object with a newly minted name
* @return 201
*/
@POST
public Response ingestAndMint(
@QueryParam("mixin") @DefaultValue(FedoraJcrTypes.FEDORA_OBJECT) String mixin,
@QueryParam("checksumType") final String checksumType,
@QueryParam("checksum") final String checksum,
@HeaderParam("Content-Type") final MediaType requestContentType,
final InputStream requestBodyStream) throws RepositoryException, IOException, InvalidChecksumException {
final String pid = pidMinter.mintPid();

String path = "/" + pid;

logger.debug("Attempting to ingest with path: {}", path);

final Session session = getAuthenticatedSession();

try {
if (nodeService.exists(session, path)) {
return Response.status(HttpStatus.SC_CONFLICT).entity(path + " is an existing resource").build();
}

createObjectOrDatastreamFromRequestContent(session, path, mixin, requestBodyStream, requestContentType, checksumType, checksum);

session.save();
logger.debug("Finished creating {} with path: {}", mixin, path);
return created(uriInfo.getRequestUri()).entity(path).build();

} finally {
session.logout();
}
}
}
Expand Up @@ -122,6 +122,7 @@ public void testCreateObject() throws RepositoryException, IOException,
assertNotNull(actual);
assertEquals(Status.CREATED.getStatusCode(), actual.getStatus());
assertTrue(actual.getEntity().toString().endsWith(pid));
verify(mockNodes).exists(mockSession, path);
verify(mockObjects).createObject(mockSession, path);
verify(mockSession).save();
}
Expand Down
Expand Up @@ -2,74 +2,71 @@
package org.fcrepo.api;

import static org.fcrepo.test.util.PathSegmentImpl.createPathList;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.InputStream;
import java.security.Principal;
import java.util.List;

import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.Response;

import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.identifiers.UUIDPidMinter;
import org.fcrepo.services.NodeService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.FedoraJcrTypes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.Repository;

public class FedoraUnnamedObjectsTest {

FedoraUnnamedObjects testObj;

FedoraNodes mockObjects;

Repository mockRepo;

Session mockSession;

SecurityContext mockSecurityContext;

HttpServletRequest mockServletRequest;

Principal mockPrincipal;

String mockUser = "testuser";
ObjectService mockObjects;
NodeService mockNodeService;

@Before
public void setUp() throws LoginException, RepositoryException {
mockObjects = mock(FedoraNodes.class);
public void setUp() throws RepositoryException {
mockObjects = mock(ObjectService.class);
mockNodeService = mock(NodeService.class);
testObj = new FedoraUnnamedObjects();
testObj.objectsResource = mockObjects;
mockSession = TestHelpers.mockSession(testObj);
testObj.setNodeService(mockNodeService);
testObj.setObjectService(mockObjects);
}

@After
public void tearDown() {

}

@SuppressWarnings("unchecked")
@Test
public void testIngestAndMint() throws RepositoryException, IOException,
InvalidChecksumException {
final UUIDPidMinter mockMint = mock(UUIDPidMinter.class);
testObj.setPidMinter(mockMint);
testObj.ingestAndMint(createPathList("objects", "fcr:new"));
verify(mockMint).mintPid();
verify(mockObjects).createObject(any(List.class),
eq(FedoraJcrTypes.FEDORA_OBJECT), isNull(String.class),
isNull(String.class), isNull(MediaType.class),
isNull(InputStream.class));
when(mockMint.mintPid()).thenReturn("uuid-123");

final Response actual =
testObj.ingestAndMint(createPathList("objects"),
FedoraJcrTypes.FEDORA_OBJECT, null, null, null, null);
assertNotNull(actual);
assertEquals(Response.Status.CREATED.getStatusCode(), actual.getStatus());
assertTrue(actual.getEntity().toString().endsWith("uuid-123"));
verify(mockObjects).createObject(mockSession, "/objects/uuid-123");
verify(mockNodeService).exists(mockSession, "/objects/uuid-123");
verify(mockSession).save();

}

}
@@ -0,0 +1,68 @@
package org.fcrepo.api.repository;

import org.fcrepo.api.FedoraUnnamedObjects;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.identifiers.UUIDPidMinter;
import org.fcrepo.services.NodeService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.FedoraJcrTypes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.Response;
import java.io.IOException;

import static org.fcrepo.test.util.PathSegmentImpl.createPathList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class FedoraRepositoryUnnamedObjectsTest {

FedoraRepositoryUnnamedObjects testObj;

Session mockSession;

ObjectService mockObjects;
NodeService mockNodeService;

@Before
public void setUp() throws RepositoryException {
mockObjects = mock(ObjectService.class);
mockNodeService = mock(NodeService.class);
testObj = new FedoraRepositoryUnnamedObjects();
mockSession = TestHelpers.mockSession(testObj);
testObj.setNodeService(mockNodeService);
testObj.setObjectService(mockObjects);
}

@After
public void tearDown() {

}

@Test
public void testIngestAndMint() throws RepositoryException, IOException,
InvalidChecksumException {
final UUIDPidMinter mockMint = mock(UUIDPidMinter.class);
testObj.setPidMinter(mockMint);
when(mockMint.mintPid()).thenReturn("uuid-123");

final Response actual =
testObj.ingestAndMint(FedoraJcrTypes.FEDORA_OBJECT, null, null, null, null);
assertNotNull(actual);
assertEquals(Response.Status.CREATED.getStatusCode(), actual.getStatus());
assertTrue(actual.getEntity().toString().endsWith("uuid-123"));
verify(mockObjects).createObject(mockSession, "/uuid-123");
verify(mockNodeService).exists(mockSession, "/uuid-123");
verify(mockSession).save();

}
}

0 comments on commit 4075e2c

Please sign in to comment.