Skip to content

Commit

Permalink
update GraphStoreStreamingOutput to accept several flavors of Jena data.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed May 20, 2013
1 parent 08342f9 commit d40943b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
Expand Up @@ -46,7 +46,7 @@
public class FedoraFieldSearch extends AbstractResource implements
FedoraJcrTypes {

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

@GET
@Timed
Expand All @@ -60,21 +60,26 @@ public GraphStoreStreamingOutput searchSubmitRdf(@QueryParam("q")
final Request request, @Context
final UriInfo uriInfo) throws RepositoryException {


if (terms.isEmpty()) {
LOGGER.trace("Received search request, but terms was empty. Aborting.");
throw new WebApplicationException(Response.status(
Response.Status.BAD_REQUEST).entity(
"q parameter is mandatory").build());
}

final Session session = getAuthenticatedSession();
try {
LOGGER.debug("Received search request with search terms {}, offset {}, and limit {}", terms, offset, limit);

/* select the best response type */
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,
Expand Down
Expand Up @@ -19,6 +19,8 @@
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;

import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.GraphStoreFactory;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.services.NodeService;
import org.fcrepo.test.util.TestHelpers;
Expand Down Expand Up @@ -61,6 +63,8 @@ public void testFieldSearch() throws RepositoryException, URISyntaxException {
new Variant(MediaType.valueOf("application/n-triples"), null,
null));

when(mockNodeService.searchRepository(any(GraphSubjects.class), eq(ResourceFactory.createResource("http://localhost/fcrepo/path/to/query/endpoint")), eq(mockSession), eq("ZZZ"), eq(0), eq(0L))).thenReturn(GraphStoreFactory.create());

testObj.searchSubmitRdf("ZZZ", 0, 0, mockRequest, uriInfo);

verify(mockNodeService).searchRepository(any(GraphSubjects.class), eq(ResourceFactory.createResource("http://localhost/fcrepo/path/to/query/endpoint")), eq(mockSession), eq("ZZZ"), eq(0), eq(0L));
Expand Down
Expand Up @@ -11,31 +11,45 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.StreamingOutput;

import org.slf4j.Logger;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.update.GraphStore;
import org.slf4j.Logger;

public class GraphStoreStreamingOutput implements StreamingOutput {

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

private final GraphStore m_graphStore;
private final Model model;

private final String format;

private final String m_format;

public GraphStoreStreamingOutput(final GraphStore graphStore,
final MediaType mediaType) {
this(graphStore.toDataset(), mediaType);
}

public GraphStoreStreamingOutput(final Dataset dataset,
final MediaType mediaType) {
m_graphStore = graphStore;
m_format =
this.model = dataset.getDefaultModel();
format =
contentTypeToLang(mediaType.toString()).getName().toUpperCase();
}

public GraphStoreStreamingOutput(final Model model,
final MediaType mediaType) {
this.model = model;
format =
contentTypeToLang(mediaType.toString()).getName().toUpperCase();
}

@Override
public void write(final OutputStream out) throws IOException,
WebApplicationException {
LOGGER.debug("Writing to: {}", out.hashCode());
m_graphStore.toDataset().getDefaultModel().write(out, m_format);
LOGGER.debug("Serializing graph model as {}", format);
model.write(out, format);

}

Expand Down
Expand Up @@ -44,7 +44,7 @@ public void writeTo(final Dataset rdf, final Class<?> type,
httpHeaders.put("Content-type", of((Object) mediaType.toString()));
setCachingHeaders(httpHeaders, rdf);

new GraphStoreStreamingOutput(new GraphStoreBasic(rdf), mediaType)
new GraphStoreStreamingOutput(rdf, mediaType)
.write(entityStream);
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import javax.jcr.RepositoryException;
import javax.ws.rs.WebApplicationException;

import com.hp.hpl.jena.query.DatasetFactory;
import org.junit.Test;
import org.slf4j.Logger;

Expand All @@ -32,7 +33,7 @@ public class GraphStreamingOutputTest {
public void testStuff() throws WebApplicationException, IOException,
RepositoryException {
final GraphStore graphStore =
new GraphStoreBasic(new DatasetImpl(createDefaultModel()));
new GraphStoreBasic(DatasetFactory.create(createDefaultModel()));
final Graph g = new GraphMem();
g.add(new Triple(createURI("test:subject"),
createURI("test:predicate"), createURI("test:object")));
Expand Down

0 comments on commit d40943b

Please sign in to comment.