Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added FieldSearchRDF impl and test, but yet unable to produce a RDF r…
…esponse
  • Loading branch information
fasseg committed May 17, 2013
1 parent 11f5cc6 commit 73a7415
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Expand Up @@ -5,8 +5,19 @@
import static java.lang.Integer.parseInt;
import static javax.jcr.query.Query.JCR_SQL2;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
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;
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 @@ -21,6 +32,11 @@
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.StreamingOutput;
import javax.ws.rs.core.Variant;

import org.fcrepo.AbstractResource;
import org.fcrepo.jaxb.search.FieldSearchResult;
Expand All @@ -32,6 +48,9 @@

import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableList;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;

/**
* @author Vincent Nguyen
Expand All @@ -53,6 +72,56 @@ public String searchForm() throws RepositoryException {
return new VelocityViewer().getFieldSearch(null);
}

@POST
@Timed
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public StreamingOutput searchSubmitRdf(@FormParam("terms") final String terms,
@FormParam("offSet") @DefaultValue("0") final String offSet,
@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);

/* construct the query from the user's search terms */
final Query query = getQuery(session.getWorkspace().getQueryManager(), session.getValueFactory(), terms);

/* perform the actual search in the repository */
final FieldSearchResult result = search(query, parseInt(offSet), parseInt(maxResults));
result.setSearchTerms(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()));

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);
}
model.write(System.out,"RDF/JSON");

return new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException,
WebApplicationException {
try{
model.write(output,"RDF/JSON");
output.flush();
}finally{
output.close();
}
}
};
}finally{
session.logout();
}
}

@POST
@Timed
@Produces(TEXT_HTML)
Expand Down
Expand Up @@ -2,10 +2,13 @@
package org.fcrepo.integration.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand Down Expand Up @@ -35,6 +38,26 @@ public void testSearchSubmit() throws Exception {

}

@Test
public void testSearchSubmitRdfXML() throws Exception {
final HttpPost method = new HttpPost(serverAddress + "fcr:search");
method.setHeader("Accept", "application/rdf+json");
final List<BasicNameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("terms", ""));
list.add(new BasicNameValuePair("offset", "0"));
list.add(new BasicNameValuePair("maxResults", "1"));
final UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
method.setEntity(formEntity);

HttpResponse resp = execute(method);
assertEquals(200, resp.getStatusLine().getStatusCode());
String json = IOUtils.toString(resp.getEntity().getContent());
logger.debug("RDF/XML Response for FieldSearch:\n",json);

method.releaseConnection();
fail("implement a test");
}

@Test
public void testSearchSubmitPaging() throws Exception {
final HttpPost method = new HttpPost(serverAddress + "fcr:search");
Expand Down

0 comments on commit 73a7415

Please sign in to comment.