Skip to content

Commit

Permalink
use LDP-style paging, and use the Sindice predicates instead of our m…
Browse files Browse the repository at this point in the history
…ade up opensearch ones
  • Loading branch information
cbeer committed Jul 22, 2013
1 parent 02f64cc commit 09cb7d8
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 49 deletions.
66 changes: 53 additions & 13 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraFieldSearch.java
Expand Up @@ -21,8 +21,14 @@
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static org.fcrepo.RdfLexicon.NEXT_PAGE;
import static org.fcrepo.RdfLexicon.PAGE;
import static org.fcrepo.RdfLexicon.PAGE_OF;
import static org.fcrepo.RdfLexicon.SEARCH_HAS_MORE;
import static org.fcrepo.RdfLexicon.SEARCH_NEXT_PAGE;
import static org.fcrepo.RdfLexicon.SEARCH_ITEMS_PER_PAGE;
import static org.fcrepo.RdfLexicon.SEARCH_OFFSET;
import static org.fcrepo.RdfLexicon.SEARCH_PAGE;
import static org.fcrepo.RdfLexicon.SEARCH_TERMS;
import static org.fcrepo.http.RDFMediaType.N3;
import static org.fcrepo.http.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.RDFMediaType.N3_ALT2;
Expand All @@ -46,6 +52,7 @@
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.hp.hpl.jena.vocabulary.RDF;
import org.fcrepo.AbstractResource;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.responses.HtmlTemplate;
Expand Down Expand Up @@ -159,26 +166,59 @@ private Dataset getSearchDataset(final String terms, final long offset,
"Received search request with search terms {}, offset {}, and limit {}",
terms, offset, limit);

final Resource searchResult =
createResource(uriInfo.getRequestUri().toASCIIString());
final Resource searchResult;

if (terms == null) {
searchResult = createResource(uriInfo.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.build().toString());
} else {
searchResult = createResource(uriInfo.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.queryParam("q", terms)
.build().toString());
}

final Dataset dataset =
nodeService.searchRepository(new HttpGraphSubjects(
FedoraNodes.class, uriInfo), searchResult,
session, terms, limit, offset);

final Model searchModel = createDefaultModel();
if (terms != null &&
dataset.getDefaultModel().contains(searchResult,
SEARCH_HAS_MORE,
searchModel.createTypedLiteral(true))) {
final Map<String, ?> pathMap =
if (terms != null) {
final Resource pageResource = createResource(uriInfo.getRequestUri().toASCIIString());
searchModel.add(pageResource, RDF.type, SEARCH_PAGE);
searchModel.add(pageResource, RDF.type, PAGE);
searchModel.add(pageResource, PAGE_OF, searchResult);


searchModel.add(pageResource,
SEARCH_ITEMS_PER_PAGE,
searchModel.createTypedLiteral(limit));
searchModel.add(pageResource,
SEARCH_OFFSET,
searchModel.createTypedLiteral(offset));
searchModel.add(pageResource, SEARCH_TERMS, terms);

if (dataset.getDefaultModel()
.contains(searchResult,
SEARCH_HAS_MORE,
searchModel.createTypedLiteral(true))) {

final Map<String, ?> pathMap =
ImmutableBiMap.of("q", terms, "offset", offset + limit,
"limit", limit);
searchModel.add(searchResult, SEARCH_NEXT_PAGE, searchModel
.createResource(uriInfo.getBaseUriBuilder().path(
FedoraFieldSearch.class).buildFromMap(pathMap)
.toString()));
"limit", limit);

final Resource nextPageResource =
searchModel.createResource(uriInfo
.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.buildFromMap(pathMap)
.toString());
searchModel.add(pageResource, NEXT_PAGE, nextPageResource);
} else {
searchModel.add(pageResource, NEXT_PAGE, RDF.nil);
}
dataset.addNamedModel("search-pagination", searchModel);
}

Expand Down
Expand Up @@ -78,23 +78,16 @@ public void setUp() throws Exception {

@Test
public void testFieldSearch() throws RepositoryException {
when(uriInfo.getRequestUri()).thenReturn(
URI.create("http://localhost/fcrepo/path/to/query/endpoint"));
when(mockRequest.selectVariant(anyListOf(Variant.class))).thenReturn(
new Variant(MediaType.valueOf("application/n-triples"), null,
null));
when(
mockNodeService
.searchRepository(
any(GraphSubjects.class),
eq(createResource("http://localhost/fcrepo/path/to/query/endpoint")),
eq(createResource("http://localhost/fcrepo/fcr:search?q=ZZZ")),
eq(mockSession), eq("ZZZ"), eq(0), eq(0L)))
.thenReturn(createMem());
when(mockUriBuilder.path(FedoraFieldSearch.class)).thenReturn(
mockUriBuilder);
when(mockUriBuilder.buildFromMap(Matchers.<Map<String, Object>> any()))
.thenReturn(URI.create("path/to/object"));

when(uriInfo.getRequestUriBuilder()).thenReturn(mockUriBuilder);

testObj.searchSubmitRdf("ZZZ", 0, 0, mockRequest, uriInfo);
Expand All @@ -103,7 +96,7 @@ public void testFieldSearch() throws RepositoryException {
.searchRepository(
any(GraphSubjects.class),
eq(ResourceFactory
.createResource("http://localhost/fcrepo/path/to/query/endpoint")),
.createResource("http://localhost/fcrepo/fcr:search?q=ZZZ")),
eq(mockSession), eq("ZZZ"), eq(0), eq(0L));
}

Expand Down
Expand Up @@ -16,12 +16,14 @@

package org.fcrepo.integration.api;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.net.URI;

import com.hp.hpl.jena.vocabulary.RDF;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -110,16 +112,31 @@ public void testSearchRdf() throws Exception {
assertTrue(graphStore
.contains(
Node.ANY,
ResourceFactory
.createResource(
serverAddress +
"fcr:search?q=testobj&offset=0&limit=1")
createResource(serverAddress + "fcr:search?q=testobj")
.asNode(),
ResourceFactory
.createResource(
"http://a9.com/-/spec/opensearch/1.1/totalResults")
.asNode(), ResourceFactory
.createTypedLiteral(1).asNode()));
RdfLexicon.SEARCH_HAS_TOTAL_RESULTS.asNode(),
ResourceFactory.createTypedLiteral(1).asNode()));

assertTrue(graphStore.contains(Node.ANY,
createResource(serverAddress + "fcr:search?q=testobj&offset=0&limit=1").asNode(),
RdfLexicon.PAGE_OF.asNode(),
createResource(serverAddress + "fcr:search?q=testobj").asNode()));


assertTrue(graphStore.contains(Node.ANY,
createResource(serverAddress + "fcr:search?q=testobj&offset=0&limit=1").asNode(),
RdfLexicon.SEARCH_OFFSET.asNode(),
ResourceFactory.createTypedLiteral(0).asNode()));

assertTrue(graphStore.contains(Node.ANY,
createResource(serverAddress + "fcr:search?q=testobj&offset=0&limit=1").asNode(),
RdfLexicon.SEARCH_ITEMS_PER_PAGE.asNode(),
ResourceFactory.createTypedLiteral(1).asNode()));

assertTrue(graphStore.contains(Node.ANY,
createResource(serverAddress + "fcr:search?q=testobj&offset=0&limit=1").asNode(),
RdfLexicon.NEXT_PAGE.asNode(),
RDF.nil.asNode()));

}

Expand All @@ -145,10 +162,9 @@ public void testSearchSubmitPaging() throws Exception {
assertFalse(graphStore
.contains(
Node.ANY,
ResourceFactory
.createResource(
serverAddress +
"fcr:search?q=testobj&offset=1&limit=1")
createResource(
serverAddress +
"fcr:search?q=testobj")
.asNode(), RdfLexicon.HAS_MEMBER_OF_RESULT
.asNode(), Node.ANY));

Expand Down
18 changes: 12 additions & 6 deletions fcrepo-kernel/src/main/java/org/fcrepo/RdfLexicon.java
Expand Up @@ -58,18 +58,24 @@ public final class RdfLexicon {


// SEARCH
public static final Property SEARCH_PAGE = createProperty("http://sindice.com/vocab/search#Page");
public static final Property SEARCH_HAS_TOTAL_RESULTS =
createProperty("http://a9.com/-/spec/opensearch/1.1/totalResults");
createProperty("http://sindice.com/vocab/search#totalResults");
public static final Property SEARCH_ITEMS_PER_PAGE =
createProperty("http://a9.com/-/spec/opensearch/1.1/itemsPerPage");
createProperty("http://sindice.com/vocab/search#itemsPerPage");
public static final Property SEARCH_OFFSET =
createProperty("http://a9.com/-/spec/opensearch/1.1/startIndex");
createProperty("http://sindice.com/vocab/search#startIndex");
public static final Property SEARCH_TERMS =
createProperty("http://a9.com/-/spec/opensearch/1.1/Query#searchTerms");
createProperty("http://sindice.com/vocab/search#searchTerms");
public static final Property SEARCH_HAS_MORE =
createProperty("info:fedora/search/hasMoreResults");
public static final Property SEARCH_NEXT_PAGE =
createProperty("info:fedora/search/next");

public static final Property PAGE =
createProperty("http://www.w3.org/ns/ldp#Page");
public static final Property PAGE_OF =
createProperty("http://www.w3.org/ns/ldp#pageOf");
public static final Property NEXT_PAGE =
createProperty("http://www.w3.org/ns/ldp#nextPage");

// REPOSITORY INFORMATION
public static final Property HAS_OBJECT_COUNT =
Expand Down
Expand Up @@ -24,9 +24,6 @@
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.RdfLexicon.SEARCH_HAS_MORE;
import static org.fcrepo.RdfLexicon.SEARCH_HAS_TOTAL_RESULTS;
import static org.fcrepo.RdfLexicon.SEARCH_ITEMS_PER_PAGE;
import static org.fcrepo.RdfLexicon.SEARCH_OFFSET;
import static org.fcrepo.RdfLexicon.SEARCH_TERMS;
import static org.fcrepo.metrics.RegistryService.getMetrics;
import static org.fcrepo.utils.FedoraTypesUtils.getRepositoryCount;
import static org.fcrepo.utils.JcrRdfTools.getJcrNamespaceModel;
Expand Down Expand Up @@ -255,11 +252,6 @@ public Dataset searchRepository(final GraphSubjects subjectFactory,

model.add(searchSubject, SEARCH_HAS_TOTAL_RESULTS, model
.createTypedLiteral(size));
model.add(searchSubject, SEARCH_ITEMS_PER_PAGE, model
.createTypedLiteral(limit));
model.add(searchSubject, SEARCH_OFFSET, model
.createTypedLiteral(offset));
model.add(searchSubject, SEARCH_TERMS, terms);
model.add(searchSubject, SEARCH_HAS_MORE, model
.createTypedLiteral(nodeIterator.hasNext()));
} else {
Expand Down

0 comments on commit 09cb7d8

Please sign in to comment.