Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Set Location header when creating a version
  • Loading branch information
escowles authored and Andrew Woods committed May 26, 2014
1 parent f223fad commit 46533d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
Expand Up @@ -51,9 +51,10 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.util.Collections;
import java.util.Collection;
import java.util.List;

import static java.util.Collections.singleton;
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
Expand Down Expand Up @@ -199,12 +200,14 @@ private Response addVersion(final String path, final String label) throws Reposi
try {
final FedoraResource resource =
nodeService.getObject(session, path);
versionService.createVersion(session.getWorkspace(),
Collections.singleton(path));
final Collection<String> versions = versionService.createVersion(session.getWorkspace(),
singleton(path));
if (label != null) {
resource.addVersionLabel(label);
}
return noContent().build();
final String version = (label != null) ? label : versions.iterator().next();
return noContent().header("Location", nodeTranslator().getSubject(
path) + "/fcr:versions/" + version).build();
} finally {
session.logout();
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
import static org.fcrepo.kernel.RdfLexicon.VERSIONING_POLICY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -638,7 +639,14 @@ public void postDsVersion(final String pid, final String dsId) throws IOExceptio
public void postVersion(final String path, final String label) throws IOException {
logger.debug("Posting version");
final HttpPost postVersion = postObjMethod(path + "/fcr:versions" + (label == null ? "" : "/" + label));
assertEquals(NO_CONTENT.getStatusCode(), getStatus(postVersion));
final HttpResponse response = client.execute(postVersion);
assertEquals(NO_CONTENT.getStatusCode(), response.getStatusLine().getStatusCode() );
final String locationHeader = response.getFirstHeader("Location").getValue();
assertNotNull( "No version location header found", locationHeader );
if ( label != null ) {
assertEquals( "Version location header doesn't match requested version label",
serverAddress + path + "/fcr:versions/" + label, locationHeader );
}
}

private void revertToVersion(final String objId, final String versionLabel) throws IOException {
Expand Down
Expand Up @@ -59,8 +59,7 @@ void nodeUpdated(Session session, String absPath)
* @param paths absolute paths to the nodes within the workspace
* @throws RepositoryException
*/
void createVersion(Workspace workspace, Collection<String> paths)
throws RepositoryException;
Collection<String> createVersion(Workspace workspace, Collection<String> paths) throws RepositoryException;

/**
* Reverts the node to the version identified by the label. This method
Expand Down
Expand Up @@ -33,6 +33,7 @@
import javax.jcr.version.VersionIterator;
import javax.jcr.version.VersionManager;
import java.util.Collection;
import java.util.HashSet;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.fcrepo.kernel.services.TransactionServiceImpl.getCurrentTransactionId;
Expand Down Expand Up @@ -105,15 +106,17 @@ public void nodeUpdated(final Node n) throws RepositoryException {
* @throws RepositoryException
*/
@Override
public void createVersion(final Workspace workspace,
public Collection<String> createVersion(final Workspace workspace,
final Collection<String> paths) throws RepositoryException {
final Collection<String> versions = new HashSet<>();
for (final String absPath : paths) {
final Node node = workspace.getSession().getNode(absPath);
if (!isVersioningEnabled(node)) {
enableVersioning(node);
}
checkpoint(workspace, absPath);
versions.add( checkpoint(workspace, absPath) );
}
return versions;
}

@Override
Expand Down Expand Up @@ -205,9 +208,10 @@ private void queueOrCommitCheckpoint(final Session session, final String absPath
}
}

private static void checkpoint(final Workspace workspace, final String absPath) throws RepositoryException {
private String checkpoint(final Workspace workspace, final String absPath) throws RepositoryException {
LOGGER.trace("Setting implicit version checkpoint set for {}", absPath);
workspace.getVersionManager().checkpoint(absPath);
final Version v = workspace.getVersionManager().checkpoint(absPath);
return ( v == null ) ? null : v.getFrozenNode().getIdentifier();
}

private void queueCheckpoint(final Session session, final String absPath) throws TransactionMissingException {
Expand Down

0 comments on commit 46533d0

Please sign in to comment.