Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
using GraphStreamingOutput for Response generation
  • Loading branch information
fasseg committed May 17, 2013
1 parent 64ee8dd commit 4013a46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
57 changes: 28 additions & 29 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraFieldSearch.java
Expand Up @@ -14,9 +14,6 @@
import static org.fcrepo.http.RDFMediaType.TURTLE;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.OutputStream;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
Expand All @@ -31,15 +28,18 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.Variant;

import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.http.RDFMediaType;
import org.fcrepo.jaxb.search.FieldSearchResult;
import org.fcrepo.jaxb.search.ObjectFields;
import org.fcrepo.provider.GraphStreamingOutput;
import org.fcrepo.provider.VelocityViewer;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
Expand All @@ -50,6 +50,8 @@
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.GraphStoreFactory;

/**
* @author Vincent Nguyen
Expand All @@ -76,12 +78,13 @@ public String searchForm() throws RepositoryException {
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public Response searchSubmitRdf(@FormParam("terms") final String terms,
@FormParam("offSet") @DefaultValue("0") final String offSet,
@FormParam("maxResults") final String maxResults, @Context final Request request) throws RepositoryException{
@FormParam("maxResults") final String maxResults, @Context final Request request)
throws RepositoryException{

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

/* construct the query from the user's search terms */
final Query query = getQuery(session.getWorkspace().getQueryManager(), session.getValueFactory(), terms);
Expand All @@ -92,31 +95,27 @@ public Response searchSubmitRdf(@FormParam("terms") final String terms,

final Model model = ModelFactory.createDefaultModel();
final Resource searchResult = model.createResource(uriInfo.getAbsolutePath().toASCIIString());
searchResult.addProperty(model.createProperty("info:fedora/fedora-system:def/search#numSearchResults"), model.createTypedLiteral(result.getSize()));
searchResult.addProperty(model.createProperty("info:fedora/fedora-system:def/search#searchTerms"), result.getSearchTerms());
searchResult.addProperty(model.createProperty("info:fedora/fedora-system:def/search#maxNumResults"), model.createTypedLiteral(result.getMaxResults()));

/* add the result description to the RDF model */
searchResult.addProperty(model.createProperty("info:fedora/fedora-system:def/search#numSearchResults"),
model.createTypedLiteral(result.getSize()));
searchResult.addProperty(model.createProperty("info:fedora/fedora-system:def/search#searchTerms"),
result.getSearchTerms());
searchResult.addProperty(model.createProperty("info:fedora/fedora-system:def/search#maxNumResults"),
model.createTypedLiteral(result.getMaxResults()));


final GraphStore store = GraphStoreFactory.create(model);

for (ObjectFields field : result.getObjectFieldsList()){
Resource objRes = model.createResource(uriInfo.getBaseUri() + "/rest" + field.getPath());
// TODO: add a complete node desription to the model via JcrRdfTools
model.add(objRes, model.createProperty("info:fedora/fedora-system:def/search#matchesSearchTerm"), terms);
model.add(searchResult, model.createProperty("info:fedora/fedora-system:def/search#hasResult"), objRes);
final FedoraResource fo = this.nodeService.getObject(session, field.getPath());
Resource objResource = model.createResource(uriInfo.getBaseUri() + "/rest/" + field.getPath());
store.addGraph(objResource.asNode(),fo.getGraphStore(new HttpGraphSubjects(FedoraNodes.class, uriInfo)).getDefaultGraph());
}

/* create a StreamingOutput that writes the model to the response stream */
final StreamingOutput rdfStream = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException,
WebApplicationException {
try{
searchResult.getModel().write(output);
output.flush();
}finally{
output.close();
}
}
};
return Response.ok(rdfStream).build();
/* prepare the model for out put by using a GraphStore */
final GraphStreamingOutput graphOut = new GraphStreamingOutput(store, bestPossibleResponse.getMediaType());
return Response.ok(graphOut).build();
}finally{
session.logout();
}
Expand Down
Expand Up @@ -40,6 +40,12 @@ public void testSearchSubmit() throws Exception {

@Test
public void testSearchSubmitRdfJSON() throws Exception {
/* first post an object which can be used for the search */
HttpPost postObj = postObjMethod("testobj");
HttpResponse postResp = execute(postObj);
postObj.releaseConnection();
assertEquals(201, postResp.getStatusLine().getStatusCode());

final HttpPost method = new HttpPost(serverAddress + "fcr:search");
method.setHeader("Accept", "application/rdf+json");
final List<BasicNameValuePair> list = new ArrayList<>();
Expand Down

0 comments on commit 4013a46

Please sign in to comment.