Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
FedoraWorkspaces now streams RDF
  • Loading branch information
ajs6f committed Nov 24, 2013
1 parent daac99d commit 4353130
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 52 deletions.
Expand Up @@ -16,8 +16,6 @@

package org.fcrepo.http.api.repository;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static com.sun.jersey.api.Responses.clientError;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.Response.created;
Expand All @@ -29,7 +27,6 @@
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
import static org.fcrepo.kernel.RdfLexicon.NOT_IMPLEMENTED;
import static org.slf4j.LoggerFactory.getLogger;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -58,15 +55,11 @@
import org.fcrepo.http.commons.session.InjectedSession;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.JcrRdfTools;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

/**
* This class exposes the JCR workspace functionality. It may be
* too JCR-y in the long run, but this lets us exercise the functionality.
Expand All @@ -91,30 +84,11 @@ public class FedoraRepositoryWorkspaces extends AbstractResource {
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, RDF_XML, RDF_JSON, NTRIPLES,
TEXT_HTML})
@HtmlTemplate("jcr:workspaces")
public Dataset getWorkspaces(@Context final UriInfo uriInfo)
public RdfStream getWorkspaces(@Context final UriInfo uriInfo)
throws RepositoryException {

final Model workspaceModel =
JcrRdfTools.withContext(null, session).getNamespaceTriples().asModel();

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

for (final String workspace : workspaces) {
final Resource resource =
createResource(uriInfo.getBaseUriBuilder()
.path("/workspace:" + workspace)
.build()
.toString());
logger.debug("Discovered workspace: {}", resource);
workspaceModel.add(resource, type, NOT_IMPLEMENTED);
}
return JcrRdfTools.withContext(null, session).getWorkspaceTriples(uriInfo);

try {
return DatasetFactory.create(workspaceModel);
} finally {
session.logout();
}
}

/**
Expand Down
Expand Up @@ -20,6 +20,7 @@
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.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -34,12 +35,13 @@
import javax.ws.rs.core.UriInfo;

import com.sun.jersey.api.NotFoundException;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.modeshape.jcr.api.NamespaceRegistry;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import org.modeshape.jcr.api.Repository;

Expand Down Expand Up @@ -100,14 +102,13 @@ public void testGetWorkspaces() throws Exception {
when(mockUriBuilder.build()).thenReturn(uri);

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

final Resource resource =
dataset.getDefaultModel().getResource(uri.toString());
final Resource resource = result.getResource(uri.toString());

final String resourceName = resource.toString();

org.junit.Assert.assertNotNull(resourceName);
assertNotNull(resourceName);
assertEquals(uri.toString(), resourceName);
}

Expand Down
Expand Up @@ -16,17 +16,17 @@

package org.fcrepo.integration.http.api;

import static com.hp.hpl.jena.graph.Node.ANY;
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.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

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

import javax.jcr.RepositoryException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -40,20 +40,10 @@ public class FedoraWorkspacesIT extends AbstractResourceIT {
@Test
public void testGetWorkspaces() throws Exception {
final HttpGet httpGet = new HttpGet(serverAddress + "fcr:workspaces");
httpGet.setHeader("Accept", "text/html");
final HttpResponse response = execute(httpGet);
assertEquals(200, response.getStatusLine().getStatusCode());

final InputStream in = response.getEntity().getContent();
final List<String> lines = IOUtils.readLines(in);
boolean found = false;
for (final String line : lines) {
if (line.contains(serverAddress + "workspace:default")) {
found = true;
break;
}
}
assertTrue(serverAddress + "workspace:default, not found", found);
final GraphStore result = getGraphStore(httpGet);
assertTrue(serverAddress + "workspace:default, not found!", result
.contains(ANY, createURI(serverAddress + "workspace:default"),
type.asNode(), NOT_IMPLEMENTED.asNode()));
}

@Test
Expand Down
12 changes: 12 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/rdf/JcrRdfTools.java
Expand Up @@ -42,6 +42,7 @@
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 All @@ -50,6 +51,7 @@
import org.fcrepo.kernel.rdf.impl.NamespaceRdfContext;
import org.fcrepo.kernel.rdf.impl.PropertiesRdfContext;
import org.fcrepo.kernel.rdf.impl.VersionsRdfContext;
import org.fcrepo.kernel.rdf.impl.WorkspaceRdfContext;
import org.fcrepo.kernel.services.LowLevelStorageService;
import org.fcrepo.kernel.services.functions.GetClusterConfiguration;
import org.fcrepo.kernel.utils.FixityResult;
Expand Down Expand Up @@ -299,6 +301,16 @@ public RdfStream getNamespaceTriples() throws RepositoryException {
return new NamespaceRdfContext(session);
}

/**
* Get an {@link RdfStream} of the registered JCR workspaces
*
* @return
* @throws RepositoryException
*/
public RdfStream getWorkspaceTriples(final UriInfo uriInfo) throws RepositoryException {
return new WorkspaceRdfContext(session, uriInfo);
}

/**
* Add the properties of a Node's parent and immediate children (as well as
* the jcr:content of children) to the given {@link RdfStream}
Expand Down
@@ -0,0 +1,65 @@
/**
* 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.rdf.impl;

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

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

import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;

/**
* Constructs RDF from the structure of {@link Workspace}s in the repository.
*
* @author ajs6f
* @date Nov 24, 2013
*/
public class WorkspaceRdfContext extends RdfStream {

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

/**
* @param session
* @param uriInfo
* @throws RepositoryException
*/
public WorkspaceRdfContext(final Session session, final UriInfo uriInfo)
throws RepositoryException {

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

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

}

0 comments on commit 4353130

Please sign in to comment.