Skip to content

Commit

Permalink
Use MessageBodyWriter to output RDF responses and provide HTML responses
Browse files Browse the repository at this point in the history
for RDF
  • Loading branch information
ajs6f authored and cbeer committed May 19, 2013
1 parent f107ea5 commit 1620818
Show file tree
Hide file tree
Showing 58 changed files with 1,832 additions and 906 deletions.
12 changes: 12 additions & 0 deletions fcrepo-http-api/pom.xml
Expand Up @@ -155,6 +155,18 @@
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.2.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
46 changes: 26 additions & 20 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraFieldSearch.java
Expand Up @@ -23,18 +23,18 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;

import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.update.GraphStore;
import org.fcrepo.AbstractResource;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.http.RDFMediaType;
import org.fcrepo.provider.GraphStreamingOutput;
import org.fcrepo.responses.GraphStoreStreamingOutput;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.update.GraphStore;

/**
* @author Frank Asseg
Expand All @@ -47,35 +47,41 @@ public class FedoraFieldSearch extends AbstractResource implements

private static final Logger logger = getLogger(FedoraFieldSearch.class);


@GET
@Timed
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public GraphStreamingOutput searchSubmitRdf(@QueryParam("q") final String terms,
@QueryParam("offset") @DefaultValue("0") final long offset,
@QueryParam("limit") @DefaultValue("25") final int limit, @Context final Request request)
throws RepositoryException{
public GraphStoreStreamingOutput searchSubmitRdf(@QueryParam("q")
final String terms, @QueryParam("offset")
@DefaultValue("0")
final long offset, @QueryParam("limit")
@DefaultValue("25")
final int limit, @Context
final Request request) throws RepositoryException {

if (terms.isEmpty()) {
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity("q parameter is mandatory")
.build()
);
throw new WebApplicationException(Response.status(
Response.Status.BAD_REQUEST).entity(
"q parameter is mandatory").build());
}

final Session session = getAuthenticatedSession();
try{
try {
/* select the best response type */
final Variant bestPossibleResponse = request.selectVariant(RDFMediaType.POSSIBLE_RDF_VARIANTS);

final Variant bestPossibleResponse =
request.selectVariant(RDFMediaType.POSSIBLE_RDF_VARIANTS);

final Resource searchResult = ResourceFactory.createResource(uriInfo.getRequestUri().toASCIIString());
final GraphStore graphStore = nodeService.searchRepository(new HttpGraphSubjects(FedoraNodes.class, uriInfo), searchResult, session, terms, limit, offset);
final Resource searchResult =
ResourceFactory.createResource(uriInfo.getRequestUri()
.toASCIIString());
final GraphStore graphStore =
nodeService.searchRepository(new HttpGraphSubjects(
FedoraNodes.class, uriInfo), searchResult, session,
terms, limit, offset);

return new GraphStreamingOutput(graphStore, bestPossibleResponse.getMediaType());
return new GraphStoreStreamingOutput(graphStore,
bestPossibleResponse.getMediaType());

} finally{
} finally {
session.logout();
}
}
Expand Down
102 changes: 48 additions & 54 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
@@ -1,16 +1,15 @@

package org.fcrepo.api;

import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.Response.created;
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 org.apache.jena.riot.WebContent.contentTypeSPARQLUpdate;
import static org.fcrepo.http.RDFMediaType.N3;
import static org.fcrepo.http.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.RDFMediaType.N3_ALT2;
import static org.fcrepo.http.RDFMediaType.NTRIPLES;
import static org.fcrepo.http.RDFMediaType.POSSIBLE_RDF_VARIANTS;
import static org.fcrepo.http.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.RDFMediaType.RDF_XML;
import static org.fcrepo.http.RDFMediaType.TURTLE;
Expand All @@ -34,22 +33,22 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.provider.GraphStreamingOutput;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
Expand All @@ -60,57 +59,50 @@
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.update.UpdateAction;

@Component
@Path("/rest/{path: .*}")
public class FedoraNodes extends AbstractResource {

private static final Logger logger = getLogger(FedoraNodes.class);

@Autowired
private LowLevelStorageService llStoreService;

@GET
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public Response describeRdf(
@PathParam("path") final List<PathSegment> pathList,
@Context final Request request,
@Context UriInfo uriInfo) throws RepositoryException, IOException {

@Autowired
private LowLevelStorageService llStoreService;

@GET
@Produces({TEXT_HTML, N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON,
NTRIPLES})
public Dataset describe(@PathParam("path")
final List<PathSegment> pathList, @Context
final Request request) throws RepositoryException, IOException {
final String path = toPath(pathList);
logger.trace("Getting profile for {}", path);

final Variant bestPossibleResponse =
request.selectVariant(POSSIBLE_RDF_VARIANTS);

final Session session = getAuthenticatedSession();
try {
final FedoraResource resource =
nodeService.getObject(session, path);

final Date date = resource.getLastModifiedDate();
final Date roundedDate = new Date();

if (date != null) {
roundedDate.setTime(date.getTime() - date.getTime() % 1000);
}

Response.ResponseBuilder builder =
final ResponseBuilder builder =
request.evaluatePreconditions(roundedDate);

if (builder == null) {
builder =
ok(new GraphStreamingOutput(resource.getGraphStore(
new HttpGraphSubjects(FedoraNodes.class, uriInfo)),
bestPossibleResponse.getMediaType()));
if (builder != null) {
final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);
// here we are implicitly emitting a 304
// the exception is not an error, it's genuinely
// an exceptional condition
throw new WebApplicationException(builder.cacheControl(cc)
.lastModified(date).build());
}

final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);

return builder.cacheControl(cc).lastModified(date).build();
return resource.getGraphStore().toDataset();

} finally {
session.logout();
Expand All @@ -128,10 +120,9 @@ public Response describeRdf(
@PUT
@Consumes({contentTypeSPARQLUpdate})
@Timed
public Response modifyObject(
@PathParam("path") final List<PathSegment> pathList,
@Context UriInfo uriInfo,
final InputStream requestBodyStream)
public Response modifyObject(@PathParam("path")
final List<PathSegment> pathList, @Context
final UriInfo uriInfo, final InputStream requestBodyStream)
throws RepositoryException, IOException {
final Session session = getAuthenticatedSession();
final String path = toPath(pathList);
Expand All @@ -143,7 +134,8 @@ public Response modifyObject(

if (requestBodyStream != null) {
UpdateAction.parseExecute(IOUtils.toString(requestBodyStream),
result.getGraphStore(new HttpGraphSubjects(FedoraNodes.class, uriInfo)));
result.getGraphStore(new HttpGraphSubjects(
FedoraNodes.class, uriInfo)));
}
session.save();

Expand All @@ -165,9 +157,9 @@ public Response modifyObject(
@POST
@Consumes({contentTypeSPARQLUpdate})
@Timed
public Response updateSparql(
@PathParam("path") final List<PathSegment> pathList,
@Context UriInfo uriInfo, final InputStream requestBodyStream)
public Response updateSparql(@PathParam("path")
final List<PathSegment> pathList, @Context
final UriInfo uriInfo, final InputStream requestBodyStream)
throws RepositoryException, IOException {

final String path = toPath(pathList);
Expand All @@ -182,8 +174,8 @@ public Response updateSparql(
final FedoraResource result =
nodeService.getObject(session, path);

result.updateGraph(new HttpGraphSubjects(FedoraNodes.class, uriInfo),
IOUtils.toString(requestBodyStream));
result.updateGraph(new HttpGraphSubjects(FedoraNodes.class,
uriInfo), IOUtils.toString(requestBodyStream));
final Problems problems = result.getGraphProblems();
if (problems != null && problems.hasProblems()) {
logger.info(
Expand Down Expand Up @@ -218,14 +210,14 @@ public Response updateSparql(
*/
@POST
@Timed
public Response createObject(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("mixin") @DefaultValue(FedoraJcrTypes.FEDORA_OBJECT) final String mixin,
@QueryParam("checksumType") final String checksumType,
@QueryParam("checksum") final String checksum,
@HeaderParam("Content-Type") final MediaType requestContentType,
@Context UriInfo uriInfo,
final InputStream requestBodyStream)
public Response createObject(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("mixin")
@DefaultValue(FedoraJcrTypes.FEDORA_OBJECT)
final String mixin, @QueryParam("checksumType")
final String checksumType, @QueryParam("checksum")
final String checksum, @HeaderParam("Content-Type")
final MediaType requestContentType, @Context
final UriInfo uriInfo, final InputStream requestBodyStream)
throws RepositoryException, IOException, InvalidChecksumException {

final String path = toPath(pathList);
Expand All @@ -239,7 +231,9 @@ public Response createObject(
path + " is an existing resource").build();
}

createObjectOrDatastreamFromRequestContent(FedoraNodes.class, session, path, mixin, uriInfo, requestBodyStream, requestContentType, checksumType, checksum);
createObjectOrDatastreamFromRequestContent(FedoraNodes.class,
session, path, mixin, uriInfo, requestBodyStream,
requestContentType, checksumType, checksum);

session.save();
logger.debug("Finished creating {} with path: {}", mixin, path);
Expand All @@ -259,9 +253,8 @@ public Response createObject(
*/
@DELETE
@Timed
public Response deleteObject(
@PathParam("path") final List<PathSegment> path)
throws RepositoryException {
public Response deleteObject(@PathParam("path")
final List<PathSegment> path) throws RepositoryException {
final Session session = getAuthenticatedSession();

try {
Expand All @@ -277,6 +270,7 @@ public ObjectService getObjectService() {
return objectService;
}

@Override
public void setObjectService(final ObjectService objectService) {
this.objectService = objectService;
}
Expand Down

0 comments on commit 1620818

Please sign in to comment.