Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Emit defined workspaces in service document
Workspace are container nodes under /sword/workspaces with a dc:title
property denoting the workspaces name
  • Loading branch information
claussni committed Sep 20, 2015
1 parent 545af74 commit 65d054e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
44 changes: 37 additions & 7 deletions src/main/java/org/fcrepo/sword/service/SWORDProviderService.java
@@ -1,12 +1,12 @@
/**
* Copyright 2015 DuraSpace, Inc.
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand All @@ -20,6 +20,7 @@
import org.apache.abdera.model.Service;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.api.models.Container;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.services.ContainerService;
import org.fcrepo.kernel.api.services.NodeService;
import org.modeshape.jcr.api.NamespaceRegistry;
Expand All @@ -31,6 +32,7 @@
import javax.annotation.PostConstruct;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import java.util.Map;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
Expand Down Expand Up @@ -66,6 +68,9 @@ public class SWORDProviderService {
@Autowired
private ContainerService containerService;

private Container workspacesContainer;
private Container collectionsContainer;

/**
* Service intitialization
*
Expand All @@ -77,8 +82,8 @@ private void init() {
registerNamespace(session);
final Container root = initializeContainer(session, SWORD_ROOT_PATH, "SWORD root");
ensureDefaultProperties(session, root);
initializeContainer(session, SWORD_WORKSPACES_PATH, "SWORD workspaces");
initializeContainer(session, SWORD_COLLECTIONS_PATH, "SWORD collections");
workspacesContainer = initializeContainer(session, SWORD_WORKSPACES_PATH, "SWORD workspaces");
collectionsContainer = initializeContainer(session, SWORD_COLLECTIONS_PATH, "SWORD collections");
}

private void ensureDefaultProperties(final Session session, final Container root) {
Expand Down Expand Up @@ -153,15 +158,40 @@ private void registerNamespace(final Session session) {
}

/**
* @return
* Build and return a SWORD service document.
*
* @return SWORD service document
*/
public Service serviceDocument() {
final Service service = abdera.newService();
service.addSimpleExtension(NS_SWORD_TERMS, "version", "sword", SWORD_VERSION);
service.addSimpleExtension(NS_SWORD_TERMS, "maxUploadSize", "sword",
String.valueOf(SWORD_MAX_UPLOAD_SIZE_KB));

workspacesContainer.getChildren().forEachRemaining(
fedoraResource -> {
addWorkspaces(service, fedoraResource);
});

return service;
}

private void addWorkspaces(Service service, FedoraResource fedoraResource) {
try {
Value[] dcTitles = fedoraResource.getProperty("dc:title").getValues();
if (dcTitles.length > 0) {
try {
String title = dcTitles[0].getString();
if (!title.isEmpty()) service.addWorkspace(title);
} catch (IndexOutOfBoundsException | NullPointerException e) {
log.warn("Found workspace container without dc:title property: "
+ fedoraResource.getPath());
}
}
} catch (RepositoryException e) {
log.warn("Found workspace container with invalid dc:title property: "
+ fedoraResource.getPath());
}
}

}
@@ -1,12 +1,12 @@
/**
* Copyright 2015 DuraSpace, Inc.
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand All @@ -21,7 +21,10 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.fcrepo.sword.service.SWORDProviderService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -53,7 +56,7 @@ public abstract class BaseProviderServiceIT {
protected HttpClient httpClient;

protected Service serviceDocumentFromStream(final InputStream content) {
final Abdera abdera = new Abdera();
final Abdera abdera = new Abdera();
final Document<Service> serviceDocument = abdera.getParser().parse(content);
return serviceDocument.getRoot();
}
Expand All @@ -65,7 +68,7 @@ public void setupHttpClient() {

@Test
public void fedoraRepositoryIsResponding() throws IOException {
final HttpGet get = new HttpGet(String.format("http://%s:%s/", HOSTNAME, SERVER_PORT));
final HttpGet get = new HttpGet(String.format("http://%s:%s/", HOSTNAME, SERVER_PORT));
final HttpResponse response = httpClient.execute(get);
assertStatusCode(200, response);
}
Expand All @@ -75,4 +78,16 @@ protected HttpResponse requestServiceDocument() throws IOException {
get.setHeader("Content-Type", "application/svc+xml");
return httpClient.execute(get);
}

protected HttpResponse createWorkspaceNode(String title) throws IOException {
final HttpPost post = new HttpPost(String.format("http://%s:%s/%s/",
HOSTNAME,
SERVER_PORT,
SWORDProviderService.SWORD_WORKSPACES_PATH));
post.setHeader("Content-Type", "text/turtle");
post.setEntity(new StringEntity(
String.format("PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
"<> dc:title \"%s\"", title)));
return httpClient.execute(post);
}
}
26 changes: 22 additions & 4 deletions src/test/java/org/fcrepo/sword/integration/ServiceDocumentIT.java
@@ -1,12 +1,12 @@
/**
* Copyright 2015 DuraSpace, Inc.
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand All @@ -17,13 +17,15 @@

import org.apache.abdera.model.Service;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.junit.Test;

import java.io.IOException;

import static org.fcrepo.sword.integration.Assert.assertContentType;
import static org.fcrepo.sword.integration.Assert.assertStatusCode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
Expand Down Expand Up @@ -63,5 +65,21 @@ public void specifiesMaxUploadSizeAsInteger() throws IOException {
}
}

@Test
public void canCreateNewWorkspace() throws IOException {
final HttpResponse response = createWorkspaceNode("Test Workspace");
assertEquals("Expect 201 Created response", HttpStatus.SC_CREATED, response.getStatusLine().getStatusCode());
}

@Test
public void definedWorkspaceGetsListetInServiceDocument() throws IOException {
String title = "Test Workspace";
createWorkspaceNode(title);
final HttpResponse response = requestServiceDocument();
final Service service = serviceDocumentFromStream(response.getEntity().getContent());
assertNotNull("Expect workspace to be defined.", service.getWorkspace(title));
assertEquals(title, service.getWorkspace(title).getTitle());
}

}

0 comments on commit 65d054e

Please sign in to comment.