Skip to content

Commit

Permalink
Updated to accept request with parameter query only as suggested, and
Browse files Browse the repository at this point in the history
changed to use the Strings.NullOrEmpty(java.lang.String) function.
  • Loading branch information
lsitu committed Jul 15, 2014
1 parent 2d0449b commit d3b5243
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
Expand Up @@ -16,6 +16,7 @@
package org.fcrepo.transform.http;

import com.codahale.metrics.annotation.Timed;
import com.google.common.base.Strings;
import com.hp.hpl.jena.query.ResultSet;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
Expand All @@ -37,12 +38,12 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
Expand Down Expand Up @@ -183,20 +184,12 @@ public Response runSparqlQuery(final InputStream requestBodyStream,
contentTypeResultsXML, contentTypeResultsBIO, contentTypeTurtle,
contentTypeN3, contentTypeNTriples, contentTypeRDFXML})
public Response runSparqlQuery(
final MultivaluedMap<String, String> formParams,
@Context final Request request)
@FormParam("query") final String query,
@Context final Request request, @Context final UriInfo uriInfo)
throws IOException, RepositoryException {
String query = null;
if (formParams.size() == 1) {
// The only parameter is the SPARQL query. This will be flexible
// to accept SPARQL in any query names.
query = formParams.getFirst(formParams.keySet().iterator().next());
} else {
// the default parameter "query" for submitting the SPARQL
query = formParams.getFirst("query");
}

LOGGER.trace("POST SPARQL query with {}: {}", contentTypeHTMLForm, query);
if (query == null || query.length() == 0) {
if (Strings.isNullOrEmpty(query)) {
return status(BAD_REQUEST)
.entity("SPARQL must not be null. Please submit a query with parameter 'query'.")
.build();
Expand Down
Expand Up @@ -220,13 +220,10 @@ private String getResponseContent(final String sparql) throws IOException {
return content;
}

private String getFormRequestResponseContent(final String sparql) throws IOException {
return getFormRequestResponseContent(sparql, null);
}

private String getFormRequestResponseContent(final String sparql, final String paramName)
private String getFormRequestResponseContent(final String sparql)
throws IOException {
final HttpPost request = getFormRequest (sparql, paramName);
final HttpPost request = getFormRequest (sparql, "query");

final HttpResponse response = client.execute(request);
assertEquals(200, response.getStatusLine().getStatusCode());
Expand Down Expand Up @@ -343,20 +340,4 @@ public void testBadFormRequest() throws IOException {
response = client.execute(badRequest);
assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatusLine().getStatusCode());
}

@Test
public void testFormRequestWithDummyParamName() throws IOException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> " +
"SELECT ?subject WHERE { ?subject dc:title \"xyz\"}";

final String content = getFormRequestResponseContent(sparql, "dummyName");
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));


assertTrue(resultSet.hasNext());

assertEquals("subject", resultSet.getResultVars().get(0));

assertEquals(serverAddress + "/abc", resultSet.next().get("subject").toString());
}
}

0 comments on commit d3b5243

Please sign in to comment.