Navigation Menu

Skip to content

Commit

Permalink
Update WorkspaceRdfContext to use GraphSubjects instead of UriInfo, a…
Browse files Browse the repository at this point in the history
…dd additional triples, and look up the default workspace from the repository configuration
  • Loading branch information
cbeer committed Dec 11, 2013
1 parent 10a89f2 commit c142d84
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 36 deletions.
Expand Up @@ -79,11 +79,13 @@ public class FedoraRepositoryWorkspaces extends AbstractResource {
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, RDF_XML, RDF_JSON, NTRIPLES,
TEXT_HTML})
@HtmlTemplate("jcr:workspaces")
public RdfStream getWorkspaces(@Context final UriInfo uriInfo)
public RdfStream getWorkspaces()
throws RepositoryException {

return JcrRdfTools.withContext(null, session).getWorkspaceTriples(
uriInfo).session(session);
final GraphSubjects subjects =
new HttpGraphSubjects(session, FedoraNodes.class, uriInfo);

return JcrRdfTools.withContext(null, session).getWorkspaceTriples(subjects).session(session);

}

Expand Down
Expand Up @@ -16,11 +16,13 @@

package org.fcrepo.http.api.repository;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.fcrepo.http.commons.test.util.TestHelpers.getUriInfoImpl;
import static org.fcrepo.http.commons.test.util.TestHelpers.mockRepository;
import static org.fcrepo.http.commons.test.util.TestHelpers.setField;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -36,6 +38,7 @@

import com.sun.jersey.api.NotFoundException;

import org.fcrepo.kernel.RdfLexicon;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand Down Expand Up @@ -73,49 +76,41 @@ public class FedoraRepositoryWorkspacesTest {
@Mock
private Workspace mockOtherWorkspace;

@Mock
private Repository mockRepository;

@Before
public void setUp() throws Exception {
initMocks(this);
mockUriInfo = getUriInfoImpl();
workspaces = new FedoraRepositoryWorkspaces();
setField(workspaces, "session", mockSession);
setField(workspaces, "uriInfo", mockUriInfo);
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
when(mockWorkspace.getName()).thenReturn("default");
mockUriInfo = getUriInfoImpl();
when(mockSession.getRepository()).thenReturn(mockRepository);
}

@Test
public void testGetWorkspaces() throws Exception {
when(mockWorkspace.getAccessibleWorkspaceNames()).thenReturn(
new String[] {"xxx"});

when(mockWorkspace.getNamespaceRegistry()).thenReturn(
mockNamespaceRegistry);

when(mockNamespaceRegistry.getPrefixes()).thenReturn(new String[]{"yyy"});
when(mockNamespaceRegistry.getURI("yyy")).thenReturn("http://example.com");

when(mockUriInfo.getBaseUriBuilder()).thenReturn(mockUriBuilder);
when(mockUriBuilder.path(any(String.class))).thenReturn(mockUriBuilder);

final URI uri = new URI("http://base/workspaces");
when(mockUriBuilder.build()).thenReturn(uri);

// Do the test.
final Model result = workspaces.getWorkspaces(mockUriInfo).asModel();

final Resource resource = result.getResource(uri.toString());
final Model result = workspaces.getWorkspaces().asModel();

final String resourceName = resource.toString();
assertTrue(result.contains(createResource("http://localhost/fcrepo/"),
RdfLexicon.HAS_WORKSPACE,
createResource("http://localhost/fcrepo/workspace:xxx" )));

assertNotNull(resourceName);
assertEquals(uri.toString(), resourceName);
}

@Test
public void testCreateWorkspace() throws Exception {
final Repository mockRepository = mockRepository();
when(mockSession.getRepository()).thenReturn(mockRepository);
when(mockWorkspaceSession.getRepository()).thenReturn(mockRepository);
when(mockRepository.login("xxx")).thenReturn(mockWorkspaceSession);
when(mockWorkspaceSession.getWorkspace()).thenReturn(mockOtherWorkspace);
when(mockOtherWorkspace.getName()).thenReturn("xxx");
Expand Down
Expand Up @@ -38,6 +38,7 @@
import java.util.HashSet;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
Expand Down Expand Up @@ -74,14 +75,21 @@ public class HttpApiResourcesTest {
@Mock
private SerializerUtil mockSerializers;

@Mock
private Session mockSession;

@Mock
private Repository mockRepository;

@Before
public void setUp() throws NoSuchFieldException {
initMocks(this);
testObj = new HttpApiResources();
mockResource = new FedoraResource(mockNode);
uriInfo = getUriInfoImpl();
when(mockSession.getRepository()).thenReturn(mockRepository);
mockSubjects =
new HttpGraphSubjects(mock(Session.class), FedoraNodes.class,
new HttpGraphSubjects(mockSession, FedoraNodes.class,
uriInfo);
setField(testObj, "serializers", mockSerializers);
}
Expand Down
Expand Up @@ -20,7 +20,7 @@
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static java.util.UUID.randomUUID;
import static org.fcrepo.kernel.RdfLexicon.NOT_IMPLEMENTED;
import static org.fcrepo.kernel.RdfLexicon.WORKSPACE_TYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand All @@ -43,7 +43,7 @@ public void testGetWorkspaces() throws Exception {
final GraphStore result = getGraphStore(httpGet);
assertTrue(serverAddress + "workspace:default, not found!", result
.contains(ANY, createURI(serverAddress + "workspace:default"),
type.asNode(), NOT_IMPLEMENTED.asNode()));
type.asNode(), WORKSPACE_TYPE.asNode()));
}

@Test
Expand Down
Expand Up @@ -28,14 +28,17 @@
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import javax.jcr.Workspace;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import com.google.common.base.Function;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.services.functions.GetDefaultWorkspace;
import org.slf4j.Logger;
import com.hp.hpl.jena.rdf.model.Resource;

Expand All @@ -56,6 +59,10 @@ public class HttpGraphSubjects implements GraphSubjects {

private final Session session;

private final String defaultWorkspace;

private Function<Repository, String> getDefaultWorkspace = new GetDefaultWorkspace();

/**
* Build HTTP graph subjects relative to the given JAX-RS resource, using the UriInfo provided.
*
Expand All @@ -75,6 +82,7 @@ public HttpGraphSubjects(final Session session, final Class<?> relativeTo, final
this.basePath = basePath;
this.pathIx = basePath.length() - 1;
this.session = session;
this.defaultWorkspace = getDefaultWorkspace.apply(session.getRepository());
LOGGER.debug("Resolving graph subjects to a base URI of \"{}\"",
basePath);
}
Expand Down Expand Up @@ -221,7 +229,7 @@ private Map<String, String> getPathMap(final String absPath) throws RepositoryEx
if (txId != null) {
path = "tx:" + txId + "/" + path;
} else if (workspace != null &&
!workspace.getName().equals("default")) {
!workspace.getName().equals(defaultWorkspace)) {
path = "workspace:" + workspace.getName() + "/" + path;
}
}
Expand Down
Expand Up @@ -27,6 +27,7 @@

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
Expand Down Expand Up @@ -54,6 +55,9 @@ public class HttpGraphSubjectsTest {
@Mock
private Session mockSession;

@Mock
private Repository mockRepository;

@Mock
private Workspace mockWorkspace;

Expand All @@ -72,6 +76,7 @@ public void setUp() throws RepositoryException {
initMocks(this);
uriInfo = getUriInfoImpl(testPath);
when(mockSession.getValueFactory()).thenReturn(mockValueFactory);
when(mockSession.getRepository()).thenReturn(mockRepository);
testObj =
new HttpGraphSubjects(mockSession, MockNodeController.class,
uriInfo);
Expand Down
4 changes: 4 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/RdfLexicon.java
Expand Up @@ -295,6 +295,10 @@ public final class RdfLexicon {
public static final Property DC_TITLE =
createProperty("http://purl.org/dc/elements/1.1/title");

public static final Resource WORKSPACE_TYPE = createResource(JCR_NAMESPACE + "#Workspace");
public static final Property HAS_WORKSPACE = createProperty(REPOSITORY_NAMESPACE + "hasWorkspace");
public static final Property HAS_DEFAULT_WORKSPACE = createProperty(REPOSITORY_NAMESPACE + "hasDefaultWorkspace");

public static final Set<Property> managedProperties;

static {
Expand Down
Expand Up @@ -42,7 +42,6 @@
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.rdf.impl.DefaultGraphSubjects;
Expand Down Expand Up @@ -303,8 +302,8 @@ public RdfStream getNamespaceTriples() throws RepositoryException {
* @return
* @throws RepositoryException
*/
public RdfStream getWorkspaceTriples(final UriInfo uriInfo) throws RepositoryException {
return new WorkspaceRdfContext(session, uriInfo);
public RdfStream getWorkspaceTriples(final GraphSubjects subjects) throws RepositoryException {
return new WorkspaceRdfContext(session, subjects);
}

/**
Expand Down
Expand Up @@ -16,15 +16,21 @@

package org.fcrepo.kernel.rdf.impl;

import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static org.fcrepo.kernel.RdfLexicon.NOT_IMPLEMENTED;
import static org.fcrepo.kernel.RdfLexicon.DC_TITLE;
import static org.fcrepo.kernel.RdfLexicon.HAS_DEFAULT_WORKSPACE;
import static org.fcrepo.kernel.RdfLexicon.HAS_WORKSPACE;
import static org.fcrepo.kernel.RdfLexicon.WORKSPACE_TYPE;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.UriInfo;

import com.google.common.base.Function;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.services.functions.GetDefaultWorkspace;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;

Expand All @@ -41,24 +47,39 @@ public class WorkspaceRdfContext extends RdfStream {

private static Logger LOGGER = getLogger(WorkspaceRdfContext.class);

private Function<Repository, String> getDefaultWorkspace = new GetDefaultWorkspace();
/**
* @param session
* @param uriInfo
* @throws RepositoryException
*/
public WorkspaceRdfContext(final Session session, final UriInfo uriInfo)
public WorkspaceRdfContext(final Session session, final GraphSubjects subjects)
throws RepositoryException {

final String[] workspaces =
session.getWorkspace().getAccessibleWorkspaceNames();

final String defaultWorkspace = getDefaultWorkspace.apply(session.getRepository());
final Node repositorySubject = subjects.getGraphSubject("/").asNode();

for (final String workspace : workspaces) {
final Node resource =
createURI(uriInfo.getBaseUriBuilder().path(
"/workspace:" + workspace).build().toString());
final Node resource = subjects.getGraphSubject(
"/workspace:" + workspace).asNode();
LOGGER.debug("Discovered workspace: {}", resource);
concat(Triple.create(resource, type.asNode(), NOT_IMPLEMENTED

concat(Triple.create(resource, type.asNode(), WORKSPACE_TYPE
.asNode()));

concat(Triple.create(resource, DC_TITLE.asNode(), createLiteral(workspace)));


concat(Triple.create(repositorySubject, HAS_WORKSPACE.asNode(), resource));

if (defaultWorkspace.equals(workspace)) {
concat(Triple.create(repositorySubject, HAS_DEFAULT_WORKSPACE.asNode(), resource));
}


}
}

Expand Down
@@ -0,0 +1,41 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.kernel.services.functions;

import com.google.common.base.Function;

import javax.jcr.Repository;

/**
* Get the default workspace from the repository configuration
* (or "default", if no information is available)
*/
public class GetDefaultWorkspace implements Function<Repository, String> {

public static final String DEFAULT_WORKSPACE_NAME = "default";

@Override
public String apply(final Repository repository) {

if (repository == null) {
return DEFAULT_WORKSPACE_NAME;
} else if (repository instanceof org.modeshape.jcr.JcrRepository) {
return ((org.modeshape.jcr.JcrRepository)repository).getConfiguration().getDefaultWorkspaceName();
} else {
return DEFAULT_WORKSPACE_NAME;
}
}
}

0 comments on commit c142d84

Please sign in to comment.