Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changed FedoraDatastreams to use injected Session
  • Loading branch information
ajs6f committed May 8, 2013
1 parent e78fca8 commit 46bdbf9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 85 deletions.
148 changes: 77 additions & 71 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraDatastreams.java
Expand Up @@ -41,8 +41,10 @@
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.session.InjectedSession;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
Expand All @@ -52,9 +54,13 @@
import com.sun.jersey.multipart.MultiPart;

@Component
@Scope("prototype")
@Path("/rest/{path: .*}/fcr:datastreams")
public class FedoraDatastreams extends AbstractResource {

@InjectedSession
private Session session;

private final Logger logger = getLogger(FedoraDatastreams.class);

@Autowired
Expand All @@ -79,21 +85,19 @@ public class FedoraDatastreams extends AbstractResource {
public ObjectDatastreams getDatastreams(@PathParam("path")
final List<PathSegment> pathList) throws RepositoryException, IOException {

final Session session = getAuthenticatedSession();

try {
final String path = toPath(pathList);
logger.info("getting datastreams of {}", path);
final ObjectDatastreams objectDatastreams = new ObjectDatastreams();
try {
final String path = toPath(pathList);
logger.info("getting datastreams of {}", path);
final ObjectDatastreams objectDatastreams = new ObjectDatastreams();

objectDatastreams.datastreams =
copyOf(transform(datastreamService
.getDatastreamsForPath(session, path), ds2dsElement));
objectDatastreams.datastreams =
copyOf(transform(datastreamService.getDatastreamsForPath(
session, path), ds2dsElement));

return objectDatastreams;
} finally {
session.logout();
}
return objectDatastreams;
} finally {
session.logout();
}

}

Expand All @@ -104,7 +108,6 @@ public Response modifyDatastreams(@PathParam("path")
final List<String> dsidList, final MultiPart multipart)
throws RepositoryException, IOException, InvalidChecksumException {

final Session session = getAuthenticatedSession();
final String path = toPath(pathList);
try {
for (final String dsid : dsidList) {
Expand All @@ -117,7 +120,7 @@ public Response modifyDatastreams(@PathParam("path")
part.getContentDisposition().getParameters()
.get("name");
logger.debug("Adding datastream: " + dsid);
final String dsPath = path + "/" + dsid;
final String dsPath = path + "/" + dsid;
final Object obj = part.getEntity();
InputStream src = null;
if (obj instanceof BodyPartEntity) {
Expand All @@ -137,19 +140,17 @@ public Response modifyDatastreams(@PathParam("path")
session.logout();
}
}

@DELETE
@Timed
public Response deleteDatastreams(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("dsid") final List<String> dsidList
) throws RepositoryException {
final Session session = getAuthenticatedSession();
public Response deleteDatastreams(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("dsid")
final List<String> dsidList) throws RepositoryException {
try {
String path = toPath(pathList);
final String path = toPath(pathList);
for (final String dsid : dsidList) {
logger.debug("purging datastream {}", path + "/" + dsid);
datastreamService.purgeDatastream(session, path + "/" + dsid);
logger.debug("purging datastream {}", path + "/" + dsid);
datastreamService.purgeDatastream(session, path + "/" + dsid);
}
session.save();
return noContent().build();
Expand All @@ -163,39 +164,40 @@ public Response deleteDatastreams(
@Produces("multipart/mixed")
@Timed
public Response getDatastreamsContents(@PathParam("path")
List<PathSegment> pathList, @QueryParam("dsid")
final List<PathSegment> pathList, @QueryParam("dsid")
final List<String> dsids) throws RepositoryException, IOException {

final Session session = getAuthenticatedSession();

try {
String path = toPath(pathList);
if (dsids.isEmpty()) {
final NodeIterator ni = objectService.getObject(session, path).getNode().getNodes();
while (ni.hasNext()) {
dsids.add(ni.nextNode().getName());
}
}

final MultiPart multipart = new MultiPart();

final Iterator<String> i = dsids.iterator();
while (i.hasNext()) {
final String dsid = i.next();

try {
final Datastream ds =
datastreamService.getDatastream(session, path + "/" + dsid);
multipart.bodyPart(ds.getContent(), MediaType.valueOf(ds
.getMimeType()));
} catch (final PathNotFoundException e) {

}
}
return Response.ok(multipart, MULTIPART_FORM_DATA).build();
} finally {
session.logout();
}
try {
final String path = toPath(pathList);
if (dsids.isEmpty()) {
final NodeIterator ni =
objectService.getObject(session, path).getNode()
.getNodes();
while (ni.hasNext()) {
dsids.add(ni.nextNode().getName());
}
}

final MultiPart multipart = new MultiPart();

final Iterator<String> i = dsids.iterator();
while (i.hasNext()) {
final String dsid = i.next();

try {
final Datastream ds =
datastreamService.getDatastream(session, path +
"/" + dsid);
multipart.bodyPart(ds.getContent(), MediaType.valueOf(ds
.getMimeType()));
} catch (final PathNotFoundException e) {

}
}
return Response.ok(multipart, MULTIPART_FORM_DATA).build();
} finally {
session.logout();
}
}

/**
Expand All @@ -215,22 +217,22 @@ public Response getDatastreamsContents(@PathParam("path")
@Timed
@Produces({TEXT_XML, APPLICATION_JSON})
public DatastreamHistory getDatastreamHistory(@PathParam("path")
List<PathSegment> pathList, @PathParam("dsid")
final List<PathSegment> pathList, @PathParam("dsid")
final String dsId) throws RepositoryException, IOException {
final Session session = getAuthenticatedSession();

try {
String path = toPath(pathList);
// TODO implement this after deciding on a versioning model
final Datastream ds = datastreamService.getDatastream(session, path + "/" + dsId);
final DatastreamHistory dsHistory =
new DatastreamHistory(singletonList(getDSProfile(ds)));
dsHistory.dsID = dsId;
dsHistory.pid = pathList.get(pathList.size() - 1).getPath();
return dsHistory;
} finally {
session.logout();
}

try {
final String path = toPath(pathList);
// TODO implement this after deciding on a versioning model
final Datastream ds =
datastreamService.getDatastream(session, path + "/" + dsId);
final DatastreamHistory dsHistory =
new DatastreamHistory(singletonList(getDSProfile(ds)));
dsHistory.dsID = dsId;
dsHistory.pid = pathList.get(pathList.size() - 1).getPath();
return dsHistory;
} finally {
session.logout();
}
}

private DatastreamProfile getDSProfile(final Datastream ds)
Expand Down Expand Up @@ -263,7 +265,7 @@ public DatastreamElement apply(final Datastream ds) {
return new DatastreamElement(ds.getDsId(),
ds.getDsId(), ds.getMimeType());
} catch (final RepositoryException e) {
throw new IllegalStateException(e);
throw new RuntimeException(e);
}
}
};
Expand All @@ -284,4 +286,8 @@ public void setLlStoreService(final LowLevelStorageService llStoreService) {
this.llStoreService = llStoreService;
}

public void setSession(final Session session) {
this.session = session;
}

}
Expand Up @@ -88,7 +88,7 @@ public void setUp() throws LoginException, RepositoryException {
when(mockSession.getUserID()).thenReturn(mockUser);
when(mockSecurityContext.getUserPrincipal()).thenReturn(mockPrincipal);
when(mockPrincipal.getName()).thenReturn(mockUser);

testObj.setSession(mockSession);
testObj.setUriInfo(TestHelpers.getUriInfoImpl());
}

Expand All @@ -104,8 +104,10 @@ public void testGetDatastreams() throws RepositoryException, IOException {
final String dsid = "testDS";
final DatastreamIterator mockIter =
TestHelpers.mockDatastreamIterator(pid, dsid, "asdf");
when(mockDatastreams.getDatastreamsForPath(mockSession, path)).thenReturn(mockIter);
final ObjectDatastreams actual = testObj.getDatastreams(createPathList("objects",pid));
when(mockDatastreams.getDatastreamsForPath(mockSession, path))
.thenReturn(mockIter);
final ObjectDatastreams actual =
testObj.getDatastreams(createPathList("objects", pid));
verify(mockDatastreams).getDatastreamsForPath(mockSession, path);
verify(mockSession, never()).save();
assertEquals(1, actual.datastreams.size());
Expand All @@ -123,8 +125,8 @@ public void testModifyDatastreams() throws RepositoryException,
atts.put(dsId2, "sdfg");
final MultiPart multipart = TestHelpers.getStringsAsMultipart(atts);
final Response actual =
testObj.modifyDatastreams(createPathList(pid), Arrays.asList(new String[] {
dsId1, dsId2}), multipart);
testObj.modifyDatastreams(createPathList(pid), Arrays
.asList(new String[] {dsId1, dsId2}), multipart);
assertEquals(Status.CREATED.getStatusCode(), actual.getStatus());
verify(mockDatastreams).createDatastreamNode(any(Session.class),
eq("/" + pid + "/" + dsId1), anyString(),
Expand All @@ -141,10 +143,13 @@ public void testDeleteDatastreams() throws RepositoryException, IOException {
final String path = "/" + pid;
final List<String> dsidList =
Arrays.asList(new String[] {"ds1", "ds2"});
final Response actual = testObj.deleteDatastreams(createPathList(pid), dsidList);
final Response actual =
testObj.deleteDatastreams(createPathList(pid), dsidList);
assertEquals(Status.NO_CONTENT.getStatusCode(), actual.getStatus());
verify(mockDatastreams).purgeDatastream(mockSession, path + "/" + "ds1");
verify(mockDatastreams).purgeDatastream(mockSession, path + "/" + "ds2");
verify(mockDatastreams)
.purgeDatastream(mockSession, path + "/" + "ds1");
verify(mockDatastreams)
.purgeDatastream(mockSession, path + "/" + "ds2");
verify(mockSession).save();
}

Expand All @@ -153,10 +158,12 @@ public void testGetDatastreamsContents() throws RepositoryException,
IOException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "testDS";
final String path = "/" + pid + "/" + dsId;
final String path = "/" + pid + "/" + dsId;
final String dsContent = "asdf";
final Datastream mockDs = TestHelpers.mockDatastream(pid, dsId, dsContent);
when(mockDatastreams.getDatastream(mockSession, path)).thenReturn(mockDs);
final Datastream mockDs =
TestHelpers.mockDatastream(pid, dsId, dsContent);
when(mockDatastreams.getDatastream(mockSession, path)).thenReturn(
mockDs);

final Response resp =
testObj.getDatastreamsContents(createPathList(pid), Arrays
Expand All @@ -176,15 +183,15 @@ public void testGetDatastreamHistory() throws RepositoryException,
IOException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "testDS";
final String path = "/" + pid + "/" + dsId;
final String path = "/" + pid + "/" + dsId;
final Datastream mockDs = TestHelpers.mockDatastream(pid, dsId, null);
when(mockDatastreams.getDatastream(mockSession, path)).thenReturn(mockDs);
when(mockDatastreams.getDatastream(mockSession, path)).thenReturn(
mockDs);
final DatastreamHistory actual =
testObj.getDatastreamHistory(createPathList(pid), dsId);
assertNotNull(actual);
verify(mockDatastreams).getDatastream(mockSession, path);
verify(mockSession, never()).save();
}


}

0 comments on commit 46bdbf9

Please sign in to comment.