Skip to content

Commit

Permalink
add a search form for text/html requests to field search without a qu…
Browse files Browse the repository at this point in the history
…ery parameter
  • Loading branch information
cbeer committed Jun 4, 2013
1 parent bbe3179 commit 7fcadb5
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
Expand Up @@ -29,6 +29,7 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.RdfLexicon;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.responses.VelocityViewer;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
Expand All @@ -54,7 +55,28 @@ public class FedoraFieldSearch extends AbstractResource implements

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


if (terms == null) {
LOGGER.trace("Received search request, but terms was empty. Aborting.");
throw new WebApplicationException(Response.status(Response.Status.OK).entity(new VelocityViewer().getSearchForm()).build());
}

return searchSubmitRdf(terms, offset, limit, request, uriInfo);
}

@GET
@Timed
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public Dataset searchSubmitRdf(@QueryParam("q")
final String terms, @QueryParam("offset")
@DefaultValue("0")
Expand All @@ -65,7 +87,7 @@ public Dataset searchSubmitRdf(@QueryParam("q")
final UriInfo uriInfo) throws RepositoryException {


if (terms.isEmpty()) {
if (terms == null) {
LOGGER.trace("Received search request, but terms was empty. Aborting.");
throw new WebApplicationException(Response.status(
Response.Status.BAD_REQUEST).entity(
Expand Down
Expand Up @@ -7,6 +7,7 @@

import java.net.URI;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -22,6 +23,21 @@

public class FedoraFieldSearchIT extends AbstractResourceIT {


@Test
public void testSearchHtml() throws Exception {
final HttpGet method = new HttpGet(serverAddress + "fcr:search");
method.setHeader("Accept", "text/html");

HttpResponse resp = execute(method);

String content = IOUtils.toString(resp.getEntity().getContent());
logger.debug("Got search form: {}", content);
assertEquals(200, resp.getStatusLine().getStatusCode());
assertTrue(content.contains("<form"));

}

@Test
public void testSearchRdf() throws Exception {
/* first post an object which can be used for the search */
Expand Down
@@ -0,0 +1,56 @@
package org.fcrepo.responses;

import static org.slf4j.LoggerFactory.getLogger;

import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;

/**
* Resolves the view to be used
* @author Vincent Nguyen
*
*/
public class VelocityViewer {

private VelocityEngine velocityEngine;

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

public VelocityViewer() {
try {
// Load the velocity properties from the class path
final Properties properties = new Properties();
properties.load(getClass().getClassLoader().getResourceAsStream("velocity.properties"));

// Create and initialize the template engine
velocityEngine = new VelocityEngine(properties);
} catch (final Exception e) {
logger.warn("Exception rendering Velocity template: {}", e);
}
}

public String getSearchForm() {

try {
// Build a context to hold the model
final VelocityContext velocityContext = new VelocityContext();

// Execute the template
final StringWriter writer = new StringWriter();
velocityEngine.mergeTemplate("views/search.vsl",
"utf-8", velocityContext, writer);

// Return the result
return writer.toString();
} catch (final Exception e) {
logger.warn("Exception rendering Velocity template: {}", e);
}
return null;
}


}
24 changes: 24 additions & 0 deletions fcrepo-http-commons/src/main/resources/views/search.vsl
@@ -0,0 +1,24 @@
#parse("views/common.vsl")
<html>
<head>
<title>Search</title>
<script type="text/javascript">
#include("views/common.js")
</script>
<style type="text/css">
#include("views/common.css")
</style>
</head>
<body>
<h1>nt:file: $topic</h1>

## output actions
<div class="actions">
<form method="GET">
<input type="text" name="q" />
<input type="submit" value="search"/>
</form>
</div>

</body>
</html>

0 comments on commit 7fcadb5

Please sign in to comment.