Skip to content

Commit

Permalink
Refactor versions to make more sense to cbeer.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Sep 26, 2014
1 parent 4eabea2 commit 29d100a
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 119 deletions.
Expand Up @@ -21,6 +21,7 @@
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import org.apache.jena.riot.Lang;
import org.fcrepo.http.api.versioning.VersionAwareHttpIdentifierTranslator;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.HttpIdentifierTranslator;
import org.fcrepo.http.commons.domain.Prefer;
Expand Down Expand Up @@ -92,11 +93,12 @@ public abstract class ContentExposingResource extends AbstractResource {
@Context protected UriInfo uriInfo;

protected FedoraResource resource;
protected HttpIdentifierTranslator identifierTranslator;

private static long MAX_BUFFER_SIZE = 10240000;

abstract Session session();
abstract HttpIdentifierTranslator translator();

abstract void addResourceHttpHeaders(FedoraResource resource);

protected Response getContent(final Prefer prefer,
Expand Down Expand Up @@ -310,31 +312,44 @@ protected URI getUri(final FedoraResource resource) {

protected FedoraResource resource() {
if (resource == null) {
try {
final boolean metadata = pathList().get(pathList().size() - 1).getPath().equals("fcr:metadata");
resource = getResourceFromPath();
}

return resource;
}

final Node node = session().getNode(path());
protected FedoraResource getResourceFromPath() {
final FedoraResource resource;
try {
final boolean metadata = pathList().get(pathList().size() - 1).getPath().equals("fcr:metadata");

if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);
final Node node = session().getNode(path());

if (metadata) {
resource = datastream;
} else {
resource = datastream.getBinary();
}
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);

if (metadata) {
resource = datastream;
} else {
resource = new FedoraObjectImpl(node);
resource = datastream.getBinary();
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
} else {
resource = new FedoraObjectImpl(node);
}
return resource;
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}

return resource;
}

protected HttpIdentifierTranslator translator() {
if (identifierTranslator == null) {
identifierTranslator = new VersionAwareHttpIdentifierTranslator(session(), FedoraLdp.class, uriInfo);
}

return identifierTranslator;
}

}
11 changes: 0 additions & 11 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraLdp.java
Expand Up @@ -25,7 +25,6 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.jena.riot.Lang;
import org.fcrepo.http.commons.api.rdf.HttpIdentifierTranslator;
import org.fcrepo.http.commons.domain.ContentLocation;
import org.fcrepo.http.commons.domain.PATCH;
import org.fcrepo.http.commons.domain.Prefer;
Expand Down Expand Up @@ -131,8 +130,6 @@ public class FedoraLdp extends ContentExposingResource {

protected String path;

private HttpIdentifierTranslator identifierTranslator;


/**
* Default JAX-RS entry point
Expand Down Expand Up @@ -604,14 +601,6 @@ Session session() {
return session;
}

protected HttpIdentifierTranslator translator() {
if (identifierTranslator == null) {
identifierTranslator = new HttpIdentifierTranslator(session, this.getClass(), uriInfo);
}

return identifierTranslator;
}

private void handleProblems(final Dataset properties) {

final Model problems = properties.getNamedModel(PROBLEMS_MODEL_NAME);
Expand Down
Expand Up @@ -219,8 +219,7 @@ public RdfStream getVersionList() {
* A translator suitable for subjects that represent nodes.
*/
protected VersionAwareHttpIdentifierTranslator translator() {
return new VersionAwareHttpIdentifierTranslator(session,
sessionFactory.getInternalSession(), FedoraNodes.class,
return new VersionAwareHttpIdentifierTranslator(session, FedoraNodes.class,
uriInfo);
}

Expand Down
121 changes: 49 additions & 72 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraVersions.java
Expand Up @@ -17,16 +17,14 @@

import com.google.common.annotations.VisibleForTesting;
import org.fcrepo.http.commons.domain.PATCH;
import org.fcrepo.http.api.versioning.VersionAwareHttpIdentifierTranslator;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.http.commons.domain.Prefer;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.impl.DatastreamImpl;
import org.fcrepo.kernel.impl.FedoraBinaryImpl;
import org.fcrepo.kernel.impl.FedoraObjectImpl;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import javax.annotation.PostConstruct;
Expand All @@ -35,19 +33,16 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.version.VersionException;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.NotFoundException;
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.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -75,26 +70,20 @@
* @author awoods
*/
@Scope("request")
@Path("/{path: .*}/fcr:versions/{label:.+}")
@Path("/{path: .*}/fcr:versions/{labelAndOptionalPathIntoVersion: .*}")
public class FedoraVersions extends ContentExposingResource {

@Inject
protected Session session;

@Autowired
private SessionFactory sessionFactory = null;

private static final Logger LOGGER = getLogger(FedoraVersions.class);


@Context protected Request request;
@Context protected HttpServletResponse servletResponse;
@Context protected UriInfo uriInfo;

@PathParam("path") protected List<PathSegment> pathList;
@PathParam("label") protected String label;
@PathParam("labelAndOptionalPathIntoVersion") protected List<PathSegment> pathListIntoVersion;

protected String path;
protected String label;
protected String pathIntoVersion;

protected FedoraResource resource;

Expand All @@ -110,14 +99,17 @@ public FedoraVersions() {
* @param path
*/
@VisibleForTesting
public FedoraVersions(final String path, final String label) {
public FedoraVersions(final String path, final String label, final String pathIntoVersion) {
this.path = path;
this.label = label;
this.pathIntoVersion = pathIntoVersion;
}

@PostConstruct
private void postConstruct() {
this.path = toPath(pathList);
this.label = pathListIntoVersion.get(0).getPath();
this.pathIntoVersion = "." + toPath(pathListIntoVersion.subList(1, pathListIntoVersion.size()));
}

/**
Expand Down Expand Up @@ -190,83 +182,68 @@ public Response removeVersion() throws RepositoryException {
* @throws RepositoryException
*/
@GET
@Produces({TURTLE, N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X,
TEXT_HTML, APPLICATION_XHTML_XML, JSON_LD})
public Response getVersion() throws RepositoryException, IOException {
@Produces({TURTLE + ";qs=10", JSON_LD + ";qs=8",
N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X,
TEXT_HTML, APPLICATION_XHTML_XML, "*/*"})
public Response getVersion(@HeaderParam("Prefer") final Prefer prefer,
@HeaderParam("Range") final String rangeValue) throws RepositoryException, IOException {
LOGGER.trace("Getting version profile for: {} at version: {}", path,
label);
checkCacheControlHeaders(request, servletResponse, resource(), session);
final RdfStream rdfStream = new RdfStream().session(session).topic(
translator().getSubject(resource().getPath()).asNode());
return getContent(null, null, rdfStream);
}

/**
* A translator suitable for subjects that represent nodes.
*/
protected VersionAwareHttpIdentifierTranslator translator() {
return new VersionAwareHttpIdentifierTranslator(session,
sessionFactory.getInternalSession(), FedoraNodes.class,
uriInfo);
translator().getSubject(unversionedResource().getPath()).asNode());
return getContent(prefer, rangeValue, rdfStream);
}


@VisibleForTesting
protected FedoraResource unversionedResource() {
final FedoraResource resource;
return getResourceFromPath();
}

try {
final boolean metadata = pathList().get(pathList().size() - 1).getPath().equals("fcr:metadata");
@Override
protected FedoraResource resource() {

final Node node = session().getNode(path());
try {
if (resource == null) {
final FedoraResource fedoraResource = unversionedResource();

if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);
final Node frozenNode = fedoraResource.getNodeVersion(label);

if (metadata) {
resource = datastream;
} else {
resource = datastream.getBinary();
if (frozenNode == null) {
throw new NotFoundException();
}
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
} else {
resource = new FedoraObjectImpl(node);
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
return resource;
}

@Override
protected FedoraResource resource() {
final Node node;

if (resource == null) {
final Node node = translator().getNodeFromGraphSubjectForVersionNode(uriInfo.getRequestUri().toString());
if (node == null) {
throw new NotFoundException();
}
if (pathIntoVersion != null) {
node = frozenNode.getNode(pathIntoVersion);
} else {
node = frozenNode;
}

final boolean metadata = pathList().get(pathList().size() - 1).getPath().equals("fcr:metadata");
final boolean metadata = pathListIntoVersion != null &&
pathListIntoVersion.get(pathListIntoVersion.size() - 1).getPath().equals("fcr:metadata");

if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);
if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);

if (metadata) {
resource = datastream;
if (metadata) {
resource = datastream;
} else {
resource = datastream.getBinary();
}
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
} else {
resource = datastream.getBinary();
resource = new FedoraObjectImpl(node);
}
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
} else {
resource = new FedoraObjectImpl(node);
}
}

return resource;
return resource;

} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

@Override
Expand Down
Expand Up @@ -73,16 +73,14 @@ public class VersionAwareHttpIdentifierTranslator extends HttpIdentifierTranslat
* other expensive operations for nodes that appear to be historic versions.
*
* @param session the current session (may contain transaction information)
* @param internalSession a session with read access to all content
* @param relativeTo a class whose path annotation will be used as the
* base for all relative paths
* @param uris a UriInfo implementation with information about the request
* URI.
*/
public VersionAwareHttpIdentifierTranslator(final Session session,
final Session internalSession, final Class<?> relativeTo, final UriInfo uris) {
public VersionAwareHttpIdentifierTranslator(final Session session, final Class<?> relativeTo, final UriInfo uris) {
super(session, relativeTo, uris);
this.internalSession = internalSession;
internalSession = session;
}

@Override
Expand Down

0 comments on commit 29d100a

Please sign in to comment.