Skip to content

Commit

Permalink
Add a method to create a version snapshot to the FedoraResource inter…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedurbin authored and Andrew Woods committed Mar 13, 2015
1 parent 494d942 commit 1a0731a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Expand Up @@ -22,6 +22,7 @@

import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -31,6 +32,7 @@
import java.util.Iterator;
import java.util.Set;

import org.apache.http.client.methods.HttpPost;
import org.apache.jena.atlas.lib.NotImplemented;

import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -186,7 +188,7 @@ public void updateProperties(final String sparqlUpdate) throws FedoraException {
} else {
LOGGER.error("error updating resource {}: {} {}", uri, status.getStatusCode(),
status.getReasonPhrase());
throw new FedoraException("error retrieving resource " + uri + ": " + status.getStatusCode() + " " +
throw new FedoraException("error updating resource " + uri + ": " + status.getStatusCode() + " " +
status.getReasonPhrase());
}

Expand Down Expand Up @@ -255,6 +257,40 @@ public boolean isWritable() {
return false;
}

@Override
public void createVersionSnapshot(final String label) throws FedoraException {
final HttpPost postVersion = httpHelper.createPostMethod(path + "/fcr:versions", null);
try {
postVersion.setHeader("Slug", label);
final HttpResponse response = httpHelper.execute(postVersion);
final StatusLine status = response.getStatusLine();
final String uri = postVersion.getURI().toString();

if ( status.getStatusCode() == SC_NO_CONTENT) {
LOGGER.debug("new version created for resource at {}", uri);
} else if ( status.getStatusCode() == SC_CONFLICT) {
LOGGER.debug("The label {} is in use by another version.", label);
throw new FedoraException("The label \"" + label + "\" is in use by another version.");
} else if ( status.getStatusCode() == SC_FORBIDDEN) {
LOGGER.error("updating resource {} is not authorized.", uri);
throw new ForbiddenException("updating resource " + uri + " is not authorized.");
} else if ( status.getStatusCode() == SC_NOT_FOUND) {
LOGGER.error("resource {} does not exist, cannot create version", uri);
throw new NotFoundException("resource " + uri + " does not exist, cannot create version");
} else {
LOGGER.error("error updating resource {}: {} {}", uri, status.getStatusCode(),
status.getReasonPhrase());
throw new FedoraException("error retrieving resource " + uri + ": " + status.getStatusCode() + " " +
status.getReasonPhrase());
}
} catch (IOException e) {
LOGGER.error("Error executing request", e);
throw new FedoraException(e);
} finally {
postVersion.releaseConnection();
}
}

/**
* Get the properties graph
*
Expand Down
Expand Up @@ -29,10 +29,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;

import org.fcrepo.client.FedoraException;
Expand Down Expand Up @@ -180,4 +182,19 @@ public void testUpdatePropertiesRDF() throws Exception {
verify(mockHelper).execute(put);
verify(mockHelper).loadProperties(resource);
}

@Test
public void testCreateVersionSnapshot() throws Exception {
final HttpResponse mockResponse = mock(HttpResponse.class);
final StatusLine mockStatus = mock(StatusLine.class);
final HttpPost post = new HttpPost(repositoryURL);
when(mockHelper.execute(any(HttpPost.class))).thenReturn(mockResponse);
when(mockResponse.getStatusLine()).thenReturn(mockStatus);
when(mockStatus.getStatusCode()).thenReturn(204);
when(mockHelper.createPostMethod(anyString(), any(Map.class))).thenReturn(post);
final String label = "examplelabel";
resource.createVersionSnapshot(label);
verify(mockHelper).execute(post);
assertEquals(label, post.getFirstHeader("Slug").getValue());
}
}
Expand Up @@ -105,4 +105,13 @@ public void updateProperties( InputStream updatedProperties,
* Check whether this Resource is writable.
**/
public boolean isWritable();

/**
* Creates a new version snapshot for this Resource with
* the given label.
* @param label the label for the version (must conform to naming
* requirements enforced by the repository).
**/
public void createVersionSnapshot( String label ) throws FedoraException;

}

0 comments on commit 1a0731a

Please sign in to comment.