Skip to content

Commit

Permalink
Works now, only used on fcr:describe and getDatastreams()
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 2, 2013
1 parent d217f22 commit 0c8cf26
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 157 deletions.
Expand Up @@ -42,7 +42,7 @@
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.session.SessionProvider.InjectedSession;
import org.fcrepo.session.InjectedSession;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down
64 changes: 37 additions & 27 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraDescribe.java
@@ -1,8 +1,11 @@

package org.fcrepo.api;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
Expand All @@ -29,6 +32,7 @@
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.session.InjectedSession;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -54,22 +58,27 @@ public class FedoraDescribe extends AbstractResource {
@GET
@Produces({TEXT_XML, APPLICATION_JSON, TEXT_HTML})
public Response describe(@PathParam("path")
final List<PathSegment> pathList) throws RepositoryException, IOException {

final String path = toPath(pathList);
logger.trace("getting profile for {}", path);
if ("/".equals(path)) {
return Response.ok(getRepositoryProfile()).build();
}
final Session session = getAuthenticatedSession();
Node node = session.getNode(path);
if (node.isNodeType("nt:file")) {
return Response.ok(getDatastreamProfile(node)).build();
final List<PathSegment> pathList, @InjectedSession
final Session session) throws RepositoryException, IOException {
try {
final String path = toPath(pathList);
logger.trace("getting profile for {}", path);
if ("/".equals(path)) {
return ok(getRepositoryProfile()).build();
}
final Node node = session.getNode(path);
if (node.isNodeType("nt:file")) {
return ok(getDatastreamProfile(node)).build();
}
if (node.isNodeType("nt:folder")) {
return ok(getObjectProfile(node)).build();
}
return status(406).entity(
"Unexpected node type: " + node.getPrimaryNodeType())
.build();
} finally {
session.logout();
}
if (node.isNodeType("nt:folder")) {
return Response.ok(getObjectProfile(node)).build();
}
return Response.status(406).entity("Unexpected node type: " + node.getPrimaryNodeType()).build();
}

/**
Expand All @@ -80,13 +89,14 @@ public Response describe(@PathParam("path")
* @throws RepositoryException
* @throws IOException
*/
public ObjectProfile getObjectProfile(Node node)
public ObjectProfile getObjectProfile(final Node node)
throws RepositoryException, IOException {

final String path = node.getPath();
logger.trace("getting object profile {}", path);
final ObjectProfile objectProfile = new ObjectProfile();
final FedoraObject obj = objectService.getObject(node.getSession(), path);
final FedoraObject obj =
objectService.getObject(node.getSession(), path);
objectProfile.pid = obj.getName();
objectProfile.objLabel = obj.getLabel();
objectProfile.objOwnerId = obj.getOwnerId();
Expand All @@ -105,15 +115,16 @@ public ObjectService getObjectService() {
return objectService;
}


public void setObjectService(ObjectService objectService) {
public void setObjectService(final ObjectService objectService) {
this.objectService = objectService;
}

public DatastreamProfile getDatastreamProfile(Node node) throws RepositoryException, IOException {
public DatastreamProfile getDatastreamProfile(final Node node)
throws RepositoryException, IOException {
final String path = node.getPath();
logger.trace("Executing getDatastream() with path: {}", path);
return getDatastreamProfile(datastreamService.getDatastream(node.getSession(), path));
return getDatastreamProfile(datastreamService.getDatastream(node
.getSession(), path));

}

Expand All @@ -135,17 +146,17 @@ private DatastreamProfile getDatastreamProfile(final Datastream ds)
dsProfile.dsMIME = ds.getMimeType();
dsProfile.dsSize = ds.getSize();
dsProfile.dsCreateDate = ds.getCreatedDate().toString();
dsProfile.dsStores = new DatastreamProfile.DSStores(ds,
llStoreService.getLowLevelCacheEntries(ds.getNode()));
dsProfile.dsStores =
new DatastreamProfile.DSStores(ds, llStoreService
.getLowLevelCacheEntries(ds.getNode()));
return dsProfile;
}

public DatastreamService getDatastreamService() {
return datastreamService;
}


public void setDatastreamService(DatastreamService datastreamService) {
public void setDatastreamService(final DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}

Expand All @@ -155,8 +166,7 @@ public DescribeRepository getRepositoryProfile() throws RepositoryException {
final DescribeRepository description = new DescribeRepository();
description.repositoryBaseURL = uriInfo.getBaseUri();
description.sampleOAIURL =
uriInfo.getBaseUriBuilder().path("/123/oai_dc")
.build();
uriInfo.getBaseUriBuilder().path("/123/oai_dc").build();
description.repositorySize = objectService.getRepositorySize();
description.numberOfObjects =
objectService.getRepositoryObjectCount(session);
Expand Down
Expand Up @@ -4,31 +4,23 @@
import static org.fcrepo.api.TestHelpers.getUriInfoImpl;
import static org.fcrepo.test.util.PathSegmentImpl.createPathList;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.security.Principal;
import java.util.HashSet;
import java.util.Set;

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

import org.fcrepo.Datastream;
import org.fcrepo.identifiers.UUIDPidMinter;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.session.SessionFactory;
import org.fcrepo.utils.LowLevelCacheEntry;
import org.junit.After;
import org.junit.Before;
Expand All @@ -40,56 +32,60 @@ public class FedoraDescribeTest {
FedoraDescribe testObj;

ObjectService mockObjects;

DatastreamService mockDatastreams;

LowLevelStorageService mockLow;
LowLevelStorageService mockLow;

Repository mockRepo;

Session mockSession;


@Before
public void setUp() throws LoginException, RepositoryException {
mockObjects = mock(ObjectService.class);
mockDatastreams = mock(DatastreamService.class);
mockLow = mock(LowLevelStorageService.class);
mockLow = mock(LowLevelStorageService.class);
testObj = new FedoraDescribe();
testObj.setObjectService(mockObjects);
testObj.setDatastreamService(mockDatastreams);
testObj.setLlStoreService(mockLow);
testObj.setLlStoreService(mockLow);
mockRepo = mock(Repository.class);
testObj.setUriInfo(getUriInfoImpl());
testObj.setPidMinter(new UUIDPidMinter());

mockSession = org.fcrepo.TestHelpers.mockSession(testObj);
mockSession = org.fcrepo.TestHelpers.mockSession(testObj);
}

@After
public void tearDown() {

}

@Test
public void testDescribeDatastream() throws RepositoryException, IOException {
public void testDescribeDatastream() throws RepositoryException,
IOException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "testDS";
final String path = "/" + pid + "/" + dsId;
final Datastream mockDs = org.fcrepo.TestHelpers.mockDatastream(pid, dsId, null);
when(mockDatastreams.getDatastream(mockSession, path)).thenReturn(mockDs);
Node mockNode = mock(Node.class);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockDs.getNode()).thenReturn(mockNode);
final String path = "/" + pid + "/" + dsId;
final Datastream mockDs =
org.fcrepo.TestHelpers.mockDatastream(pid, dsId, null);
when(mockDatastreams.getDatastream(mockSession, path)).thenReturn(
mockDs);
final Node mockNode = mock(Node.class);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockDs.getNode()).thenReturn(mockNode);
when(mockNode.getName()).thenReturn(dsId);
Node mockParent = mock(Node.class);
final Node mockParent = mock(Node.class);
when(mockParent.getPath()).thenReturn(path);
when(mockNode.getParent()).thenReturn(mockParent);
when(mockNode.getPath()).thenReturn(path);
when(mockNode.getPath()).thenReturn(path);
when(mockNode.isNodeType("nt:file")).thenReturn(true);
when(mockSession.getNode(path)).thenReturn(mockNode);
when(mockLow.getLowLevelCacheEntries(mockNode)).thenReturn(new HashSet<LowLevelCacheEntry>());
final Response actual = testObj.describe(createPathList(pid, dsId));
when(mockLow.getLowLevelCacheEntries(mockNode)).thenReturn(
new HashSet<LowLevelCacheEntry>());
final Response actual =
testObj.describe(createPathList(pid, dsId), mockSession);
assertNotNull(actual);
verify(mockDatastreams).getDatastream(mockSession, path);
verify(mockSession, never()).save();
Expand Down
2 changes: 1 addition & 1 deletion fcrepo-http-api/src/test/resources/spring-test/rest.xml
Expand Up @@ -13,6 +13,6 @@

<context:annotation-config />

<context:component-scan base-package="org.fcrepo.api, org.fcrepo.exceptionhandlers"/>
<context:component-scan base-package="org.fcrepo.api, org.fcrepo.exceptionhandlers, org.fcrepo.session"/>

</beans>
Expand Up @@ -15,7 +15,7 @@
<bean id="containerWrapper" class="org.fcrepo.test.util.ContainerWrapper" init-method="start" destroy-method="stop" >
<property name="port" value="${test.port:8080}"/>
<property name="contextConfigLocations" value="classpath:spring-test/master.xml" />
<property name="packagesToScan" value="org.fcrepo.api, org.fcrepo.exceptionhandlers" />
<property name="packagesToScan" value="org.fcrepo.api, org.fcrepo.exceptionhandlers, org.fcrepo.session" />
</bean>

</beans>
7 changes: 1 addition & 6 deletions fcrepo-http-commons/pom.xml
Expand Up @@ -58,18 +58,13 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
Expand Down

This file was deleted.

0 comments on commit 0c8cf26

Please sign in to comment.