Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move weird object/datastream creation logic up to AbstractResource wh…
…ere it can be shared between the 3 classes that use it
- Loading branch information
Showing
8 changed files
with
241 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
fcrepo-http-api/src/main/java/org/fcrepo/api/repository/FedoraRepositoryUnnamedObjects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...-http-api/src/test/java/org/fcrepo/api/repository/FedoraRepositoryUnnamedObjectsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} | ||
} |
Oops, something went wrong.