Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #158 from futures/RDFIterationForevva
Adds RdfStreamProvider for rendering RdfStreams as triples.
  • Loading branch information
cbeer committed Nov 19, 2013
2 parents 9eaac0f + 037d879 commit 760b842
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 87 deletions.
Expand Up @@ -16,16 +16,13 @@

package org.fcrepo.http.api;

import static com.google.common.util.concurrent.Futures.addCallback;
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
import static org.fcrepo.http.commons.domain.RDFMediaType.POSSIBLE_RDF_VARIANTS;
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
Expand All @@ -47,14 +44,12 @@
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;

import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.HttpGraphSubjects;
import org.fcrepo.http.commons.responses.RdfStreamStreamingOutput;
import org.fcrepo.http.commons.session.InjectedSession;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.utils.LogoutCallback;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -85,30 +80,19 @@ public class FedoraVersions extends AbstractResource {
*/
@GET
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, RDF_XML, RDF_JSON, NTRIPLES})
public Response getVersionList(@PathParam("path")
public RdfStream getVersionList(@PathParam("path")
final List<PathSegment> pathList,
@Context
final Request request,
@Context
final UriInfo uriInfo) throws RepositoryException {
final String path = toPath(pathList);

LOGGER.trace("getting versions list for {}", path);

final Variant bestPossibleResponse =
request.selectVariant(POSSIBLE_RDF_VARIANTS);
LOGGER.trace("Getting versions list for: {}", path);

final FedoraResource resource = nodeService.getObject(session, path);

final RdfStreamStreamingOutput streamOutput =
new RdfStreamStreamingOutput(resource
.getVersionTriples(new HttpGraphSubjects(session,
FedoraVersions.class, uriInfo)),
bestPossibleResponse.getMediaType());

addCallback(streamOutput, new LogoutCallback(session));

return ok(streamOutput).build();
return resource.getVersionTriples(translator());
}

/**
Expand Down Expand Up @@ -150,7 +134,7 @@ public Response addVersionLabel(@PathParam("path")
@Path("/{versionLabel}")
@GET
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, RDF_XML, RDF_JSON, NTRIPLES})
public Response getVersion(@PathParam("path")
public RdfStream getVersion(@PathParam("path")
final List<PathSegment> pathList,
@PathParam("versionLabel")
final String versionLabel,
Expand All @@ -159,7 +143,7 @@ public Response getVersion(@PathParam("path")
@Context
final UriInfo uriInfo) throws RepositoryException, IOException {
final String path = toPath(pathList);
LOGGER.trace("getting version profile for {} at version {}", path,
LOGGER.trace("Getting version profile for: {} at version: {}", path,
versionLabel);

final FedoraResource resource =
Expand All @@ -168,19 +152,12 @@ public Response getVersion(@PathParam("path")
if (resource == null) {
throw new WebApplicationException(status(NOT_FOUND).build());
} else {

final Variant bestPossibleResponse =
request.selectVariant(POSSIBLE_RDF_VARIANTS);

final RdfStreamStreamingOutput streamOutput =
new RdfStreamStreamingOutput(resource
.getTriples(new HttpGraphSubjects(session,
FedoraVersions.class, uriInfo)),
bestPossibleResponse.getMediaType());

addCallback(streamOutput, new LogoutCallback(session));

return ok(streamOutput).build();
return resource.getTriples(translator());
}
}

protected GraphSubjects translator() {
return new HttpGraphSubjects(session, this.getClass(), uriInfo);
}

}
Expand Up @@ -87,16 +87,17 @@ public void testGetVersionList() throws RepositoryException {
mockVariant);
when(mockNodes.getObject(any(Session.class), anyString())).thenReturn(
mockResource);
final RdfStream testRdfStream = new RdfStream();
when(mockResource.getVersionTriples(any(HttpGraphSubjects.class)))
.thenReturn(new RdfStream());
.thenReturn(testRdfStream);
when(mockVariant.getMediaType()).thenReturn(
new MediaType("text", "turtle"));

final Response response =
final RdfStream response =
testObj.getVersionList(createPathList(pid), mockRequest,
getUriInfoImpl());
assertNotNull(response);
assertEquals(200, response.getStatus());
assertEquals("Got wrong RdfStream!", testRdfStream, response);
}

@Test
Expand Down
Expand Up @@ -255,9 +255,8 @@ private Template getTemplate(final Dataset rdf, final Node subject,
@Override
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
return mediaType.equals(TEXT_HTML_TYPE) &&
(Dataset.class.isAssignableFrom(type) || Dataset.class
.isAssignableFrom(genericType.getClass()));
return mediaType.equals(TEXT_HTML_TYPE)
&& Dataset.class.isAssignableFrom(type) ;
}

@Override
Expand Down
Expand Up @@ -72,9 +72,7 @@ public boolean isWriteable(final Class<?> type, final Type genericType,
// we can return a result for any MIME type that Jena can serialize
final Boolean appropriateMimeType =
contentTypeToLang(mediaType.toString()) != null;
return appropriateMimeType &&
(Dataset.class.isAssignableFrom(type) || Dataset.class
.isAssignableFrom(genericType.getClass()));
return appropriateMimeType && Dataset.class.isAssignableFrom(type);
}

@Override
Expand Down
@@ -0,0 +1,72 @@
/**
* 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.http.commons.responses;

import static com.google.common.util.concurrent.Futures.addCallback;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import org.fcrepo.kernel.utils.LogoutCallback;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.springframework.stereotype.Component;


/**
* Provides serialization for streaming RDF results.
*
* @author ajs6f
* @date Nov 19, 2013
*/
@Provider
@Component
public class RdfStreamProvider implements MessageBodyWriter<RdfStream> {

@Override
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
return RdfStream.class.isAssignableFrom(type) ;
}

@Override
public long getSize(final RdfStream t, final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
// We do not know how long the stream is
return -1;
}

@Override
public void writeTo(final RdfStream rdfStream, final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream entityStream) throws IOException,
WebApplicationException {
final RdfStreamStreamingOutput streamOutput =
new RdfStreamStreamingOutput(rdfStream, mediaType);
addCallback(streamOutput, new LogoutCallback(rdfStream.session()));
streamOutput.write(entityStream);

}

}
Expand Up @@ -245,12 +245,11 @@ public Dataset getPropertiesDataset(final GraphSubjects graphSubjects,
JcrRdfTools.withContext(graphSubjects, getNode().getSession());

final RdfStream propertiesStream =
jcrRdfTools.getJcrTriples(getNode()).limit(limit).skip(offset);
jcrRdfTools.getJcrTriples(getNode());

propertiesStream.concat(jcrRdfTools.getTreeTriples(getNode(), offset,
limit));
propertiesStream.concat(jcrRdfTools.getTreeTriples(getNode()));

final Dataset dataset = DatasetFactory.create(propertiesStream.asModel());
final Dataset dataset = DatasetFactory.create(propertiesStream.limit(limit).skip(offset).asModel());

final Model problemsModel = createDefaultModel();

Expand Down
Expand Up @@ -49,19 +49,20 @@ public Dataset getProperties(final Node node, final GraphSubjects subjects,
final int offset, final int limit)
throws RepositoryException {
final JcrRdfTools jcrRdfTools = JcrRdfTools.withContext(subjects, node.getSession());
final Model model = jcrRdfTools.getJcrTriples(node).asModel();
final Model treeModel = jcrRdfTools.getTreeTriples(node, offset, limit).asModel();
final Model model =
jcrRdfTools.getJcrTriples(node).concat(
jcrRdfTools.getTreeTriples(node)).limit(limit).skip(offset)
.asModel();
final Model problemModel = JcrRdfTools.getProblemsModel();

final JcrPropertyStatementListener listener =
JcrPropertyStatementListener.getListener(
subjects, node.getSession(), problemModel);

model.register(listener);
treeModel.register(listener);

final Dataset dataset = DatasetFactory.create(model);
dataset.addNamedModel(MODEL_NAME, treeModel);
dataset.addNamedModel(MODEL_NAME, model);

final Resource subject = subjects.getGraphSubject(node);
final String uri = subject.getURI();
Expand Down
Expand Up @@ -93,6 +93,6 @@ public NamespaceRdfContext(final Session session) throws RepositoryException {
createLiteral(nsURI)));
}
}
concat(nsTriples.build()).addNamespaces(namespaces.build());
concat(nsTriples.build()).namespaces(namespaces.build());
}
}
Expand Up @@ -304,17 +304,14 @@ public RdfStream getNamespaceTriples() throws RepositoryException {
* the jcr:content of children) to the given {@link RdfStream}
*
* @param node
* @param offset
* @param limit @throws RepositoryException
* @throws RepositoryException
*/
public RdfStream getTreeTriples(final Node node, final int offset,
final int limit) throws RepositoryException {
return new HierarchyRdfContext(node, graphSubjects, llstore).limit(
limit).skip(offset);
public RdfStream getTreeTriples(final Node node) throws RepositoryException {
return new HierarchyRdfContext(node, graphSubjects, llstore);
}

/**
* Decides whether the RDF represetnation of this {@link Node} will receive LDP Container status.
* Decides whether the RDF representation of this {@link Node} will receive LDP Container status.
*
* @param node
* @return
Expand Down Expand Up @@ -516,7 +513,7 @@ public String getPropertyNameFromPredicate(final com.hp.hpl.jena.rdf.model.Prope
public String getPropertyNameFromPredicate(final com.hp.hpl.jena.rdf.model.Property predicate) throws RepositoryException {


Map<String, String> emptyNamespaceMapping = emptyMap();
final Map<String, String> emptyNamespaceMapping = emptyMap();
return getPropertyNameFromPredicate(predicate, emptyNamespaceMapping);
}

Expand Down Expand Up @@ -604,7 +601,7 @@ public int getPropertyType(final String nodeType, final String propertyName) thr
public int getPropertyType(final NodeType nodeType, final String propertyName) throws RepositoryException {
final PropertyDefinition[] propertyDefinitions = nodeType.getPropertyDefinitions();
int type = UNDEFINED;
for (PropertyDefinition propertyDefinition : propertyDefinitions) {
for (final PropertyDefinition propertyDefinition : propertyDefinitions) {
if (propertyDefinition.getName().equals(propertyName)) {
if (type != UNDEFINED) {
return UNDEFINED;
Expand Down

0 comments on commit 760b842

Please sign in to comment.