Skip to content

Commit

Permalink
Add workspace rdf to fcr:workspace responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Woods committed Aug 8, 2013
1 parent 07a6d23 commit 6364d47
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 7 deletions.
Expand Up @@ -25,42 +25,86 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

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;
import com.hp.hpl.jena.vocabulary.RDF;
import org.fcrepo.AbstractResource;
import org.fcrepo.api.FedoraNodes;
import org.fcrepo.responses.HtmlTemplate;
import org.fcrepo.session.InjectedSession;
import org.fcrepo.utils.JcrRdfTools;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableMap;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static org.fcrepo.RdfLexicon.NOT_IMPLEMENTED;
import static org.fcrepo.http.RDFMediaType.N3;
import static org.fcrepo.http.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.RDFMediaType.N3_ALT2;
import static org.fcrepo.http.RDFMediaType.NTRIPLES;
import static org.fcrepo.http.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.RDFMediaType.RDF_XML;
import static org.fcrepo.http.RDFMediaType.TURTLE;
import static org.slf4j.LoggerFactory.getLogger;

/**
* This class exposes the JCR workspace functionality. It may be
* too JCR-y in the long run, but this lets us exercise the functionality.
*/
@Component
@Scope("prototype")
@Path("/fcr:workspaces")
public class FedoraRepositoryWorkspace extends AbstractResource {
public class FedoraRepositoryWorkspaces extends AbstractResource {

private static final Logger logger = getLogger(FedoraRepositoryWorkspaces.class);

@InjectedSession
protected Session session;

/**
* Get the list of accessible workspaces in this repository.
*
* TODO: serialize this as RDF (?)
* @return
* @throws RepositoryException
*/
@GET
public Response getWorkspaces() throws RepositoryException {
return Response
.ok(session.getWorkspace().getAccessibleWorkspaceNames())
.build();
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, RDF_XML, RDF_JSON, NTRIPLES,
TEXT_HTML})
@HtmlTemplate("jcr:workspaces")
public Dataset getWorkspaces() throws RepositoryException {

Model workspaceModel =
JcrRdfTools.withContext(null, session).getJcrPropertiesModel();

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

for (String workspace : workspaces) {
final Resource resource =
createResource(uriInfo.getBaseUriBuilder()
.path("/workspace:" + workspace)
.build()
.toString());

workspaceModel.add(resource, RDF.type, NOT_IMPLEMENTED);
}

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

/**
Expand Down
Expand Up @@ -20,6 +20,7 @@
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.RdfLexicon.HAS_FIXITY_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_NAMESPACE_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_WORKSPACE_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_SEARCH_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_SERIALIZATION;
import static org.fcrepo.RdfLexicon.HAS_SITEMAP;
Expand All @@ -41,6 +42,7 @@
import org.fcrepo.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.api.repository.FedoraRepositoryNamespaces;
import org.fcrepo.api.repository.FedoraRepositoryTransactions;
import org.fcrepo.api.repository.FedoraRepositoryWorkspaces;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.serialization.SerializerUtil;
import org.fcrepo.utils.FedoraJcrTypes;
Expand Down Expand Up @@ -136,6 +138,11 @@ private void addRepositoryStatements(UriInfo uriInfo, Model model,
model.add(s, HAS_NAMESPACE_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraRepositoryNamespaces.class)
.build().toASCIIString()));

// fcr:workspaces
model.add(s, HAS_WORKSPACE_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraRepositoryWorkspaces.class)
.build().toASCIIString()));
}

}
1 change: 1 addition & 0 deletions fcrepo-http-api/src/main/resources/views/common-header.vsl
Expand Up @@ -7,6 +7,7 @@
<ul class="nav">
<li><a href="$uriInfo.baseUriBuilder.build()">Home</a></li>
<li><a href="$uriInfo.baseUriBuilder.build()fcr:namespaces">Namespaces</a></li>
<li><a href="$uriInfo.baseUriBuilder.build()fcr:workspaces">Workspaces</a></li>

</ul>

Expand Down
31 changes: 31 additions & 0 deletions fcrepo-http-api/src/main/resources/views/jcr-workspaces.vsl
@@ -0,0 +1,31 @@
#* @vtlvariable name="rdf" type="com.hp.hpl.jena.sparql.core.DatasetGraph" *#
#* @vtlvariable name="subjects" type="com.hp.hpl.jena.rdf.model.ResIterator" *#
#* @vtlvariable name="nodeany" type="com.hp.hpl.jena.graph.Node" *#
#* @vtlvariable name="topic" type="com.hp.hpl.jena.graph.Node" *#
<!DOCTYPE html>
#parse("views/common.vsl")
<html>
<head>
<title>registered workspaces</title>
#parse("views/common-head.vsl")
</head>
<body>
<div id="main" class="container" resource="$uriInfo.baseUriBuilder.build()fcr:workspaces">
#parse("views/common-header.vsl")

<div class="row">
<div class="span9">
<h1>Workspaces</h1>

## output other nodes
#foreach($subject in $subjects)
<div class="well" resource="$subject.getURI()">
<h3>$subject.getURI()</h3>
#triples($subject.asNode())
</div>
#end
</div>

</div>
</body>
</html>
@@ -0,0 +1,94 @@
/**
* 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.api.repository;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Resource;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.modeshape.jcr.api.NamespaceRegistry;

import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import java.net.URI;

import static org.fcrepo.test.util.TestHelpers.setField;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

/**
* @author Andrew Woods
* Date: 8/7/13
*/
public class FedoraRepositoryWorkspacesTest {

private FedoraRepositoryWorkspaces workspaces;

@Mock
private Workspace mockWorkspace;

@Mock
private NamespaceRegistry mockNamespaceRegistry;

@Mock
private UriInfo mockUriInfo;

@Mock
private UriBuilder mockUriBuilder;

@Mock
private Session mockSession;

@Before
public void setUp() throws Exception {
initMocks(this);
workspaces = new FedoraRepositoryWorkspaces();
setField(workspaces, "session", mockSession);
setField(workspaces, "uriInfo", mockUriInfo);
}

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

when(mockWorkspace.getNamespaceRegistry()).thenReturn(
mockNamespaceRegistry);
when(mockNamespaceRegistry.getPrefixes()).thenReturn(new String[]{"yyy"});

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

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

// Do the test.
Dataset dataset = workspaces.getWorkspaces();

Resource resource = dataset.getDefaultModel()
.getResource(uri.toString());
String resourceName = resource.toString();

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

}
Expand Up @@ -17,11 +17,17 @@
package org.fcrepo.integration.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -32,6 +38,25 @@

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());

InputStream in = response.getEntity().getContent();
List<String> lines = IOUtils.readLines(in);
boolean found = false;
for (String line : lines) {
if (line.contains(serverAddress + "workspace:default")) {
found = true;
break;
}
}
assertTrue(serverAddress + "workspace:default, not found", found);
}

@Test
public void shouldDemonstratePathsAndWorkspaces() throws IOException,
RepositoryException {
Expand Down
Expand Up @@ -24,6 +24,7 @@
import static org.fcrepo.RdfLexicon.HAS_SITEMAP;
import static org.fcrepo.RdfLexicon.HAS_TRANSACTION_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_VERSION_HISTORY;
import static org.fcrepo.RdfLexicon.HAS_WORKSPACE_SERVICE;
import static org.fcrepo.test.util.TestHelpers.getUriInfoImpl;
import static org.fcrepo.test.util.TestHelpers.mockSession;
import static org.fcrepo.test.util.TestHelpers.setField;
Expand Down Expand Up @@ -101,6 +102,7 @@ public void shouldDecorateModeRootNodesWithRepositoryWideLinks()
assertTrue(model.contains(graphSubject, HAS_SITEMAP));
assertTrue(model.contains(graphSubject, HAS_TRANSACTION_SERVICE));
assertTrue(model.contains(graphSubject, HAS_NAMESPACE_SERVICE));
assertTrue(model.contains(graphSubject, HAS_WORKSPACE_SERVICE));
}

@Test
Expand Down
Expand Up @@ -158,7 +158,8 @@ void init() throws IOException, RepositoryException {
}

List<String> otherTemplates =
ImmutableList.of("search:results", "jcr:namespaces", "node");
ImmutableList.of("search:results", "jcr:namespaces",
"jcr:workspaces", "node");

for (String key : otherTemplates) {
final Template template =
Expand Down
4 changes: 4 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/RdfLexicon.java
Expand Up @@ -89,6 +89,8 @@ public final class RdfLexicon {
createProperty("info:fedora/hasTransactionProvider");
public static final Property HAS_NAMESPACE_SERVICE =
createProperty("info:fedora/hasNamespaces");
public static final Property HAS_WORKSPACE_SERVICE =
createProperty("info:fedora/hasWorkspaces");
public static final Property HAS_SEARCH_SERVICE =
createProperty("http://www.whatwg.org/specs/web-apps/current-work/"
+ "#link-type-search");
Expand Down Expand Up @@ -116,6 +118,8 @@ public final class RdfLexicon {
"feed0");
public static final Property HAS_SUBSCRIPTION_SERVICE =
createProperty("http://microformats.org/wiki/rel-subscription");
public static final Property NOT_IMPLEMENTED =
createProperty(INTERNAL_NAMESPACE + "notImplemented");

// CONTENT
public static final Property HAS_CONTENT =
Expand Down

0 comments on commit 6364d47

Please sign in to comment.