Skip to content

Commit

Permalink
URL-Encode output
Browse files Browse the repository at this point in the history
  • Loading branch information
acoburn authored and Andrew Woods committed Oct 2, 2015
1 parent 6b161c2 commit 9fde12a
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 36 deletions.
Expand Up @@ -15,6 +15,8 @@
*/
package org.fcrepo.camel.processor;

import static java.net.URLEncoder.encode;

import java.io.IOException;

import org.apache.camel.Exchange;
Expand Down Expand Up @@ -57,13 +59,13 @@ public void process(final Exchange exchange) throws IOException {
* too many triples from the triplestore. This command does
* not remove blank nodes.
*/
final StringBuilder query = new StringBuilder("update=");
final StringBuilder query = new StringBuilder();

query.append(ProcessorUtils.deleteWhere(subject, namedGraph));
query.append(";\n");
query.append(ProcessorUtils.deleteWhere(subject + "/fcr:export?format=jcr/xml", namedGraph));

in.setBody(query.toString());
in.setBody("update=" + encode(query.toString(), "UTF-8"));
in.setHeader(Exchange.HTTP_METHOD, "POST");
in.setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
}
Expand Down
Expand Up @@ -15,6 +15,8 @@
*/
package org.fcrepo.camel.processor;

import static java.net.URLEncoder.encode;

import java.io.IOException;

import org.apache.camel.Exchange;
Expand Down Expand Up @@ -46,7 +48,7 @@ public void process(final Exchange exchange) throws IOException {
final Message in = exchange.getIn();
final String subject = ProcessorUtils.getSubjectUri(in);

exchange.getIn().setBody("query=DESCRIBE <" + subject + ">");
exchange.getIn().setBody("query=" + encode("DESCRIBE <" + subject + ">", "UTF-8"));
exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
exchange.getIn().setHeader(Exchange.ACCEPT_CONTENT_TYPE, "application/rdf+xml");
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
Expand Down
Expand Up @@ -18,6 +18,7 @@
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.camel.processor.ProcessorUtils.insertData;
import static org.fcrepo.camel.processor.ProcessorUtils.langFromMimeType;
import static java.net.URLEncoder.encode;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -54,10 +55,8 @@ public void process(final Exchange exchange) throws IOException {

model.write(serializedGraph, "N-TRIPLE");

final StringBuilder query = new StringBuilder("update=");
query.append(insertData(serializedGraph.toString("UTF-8"), namedGraph));

exchange.getIn().setBody(query.toString());
exchange.getIn().setBody("update=" +
encode(insertData(serializedGraph.toString("UTF-8"), namedGraph), "UTF-8"));
exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
}
Expand Down
Expand Up @@ -17,6 +17,7 @@

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.camel.processor.ProcessorUtils.langFromMimeType;
import static java.net.URLEncoder.encode;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -69,14 +70,14 @@ public void process(final Exchange exchange) throws IOException {
* delete too many triples from the triplestore. This command does
* not delete blank nodes.
*/
final StringBuilder query = new StringBuilder("update=");
final StringBuilder query = new StringBuilder();
query.append(ProcessorUtils.deleteWhere(subject, namedGraph));
query.append(";\n");
query.append(ProcessorUtils.deleteWhere(subject + "/fcr:export?format=jcr/xml", namedGraph));
query.append(";\n");
query.append(ProcessorUtils.insertData(serializedGraph.toString("UTF-8"), namedGraph));

in.setBody(query.toString());
in.setBody("update=" + encode(query.toString(), "UTF-8"));
in.setHeader(Exchange.HTTP_METHOD, "POST");
in.setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
}
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/fcrepo/camel/SparqlDeleteProcessorTest.java
Expand Up @@ -15,6 +15,8 @@
*/
package org.fcrepo.camel;

import static java.net.URLEncoder.encode;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -82,9 +84,9 @@ public void testDelete() throws IOException, InterruptedException {
"</rdf:RDF>";

// Assertions
resultEndpoint.expectedBodiesReceived(
"update=DELETE WHERE { <" + base + path + "> ?p ?o };\n" +
"DELETE WHERE { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o }");
resultEndpoint.expectedBodiesReceived("update=" +
encode("DELETE WHERE { <" + base + path + "> ?p ?o };\n" +
"DELETE WHERE { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o }", "UTF-8"));
resultEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");

Expand Down
Expand Up @@ -18,6 +18,7 @@
import static org.apache.camel.Exchange.HTTP_METHOD;
import static org.apache.camel.Exchange.CONTENT_TYPE;
import static org.apache.camel.Exchange.ACCEPT_CONTENT_TYPE;
import static java.net.URLEncoder.encode;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void testDescribe() throws IOException, InterruptedException {
final String path = "/path/a/b/c/d";

// Assertions
resultEndpoint.expectedBodiesReceived("query=DESCRIBE <" + base + path + ">");
resultEndpoint.expectedBodiesReceived("query=" + encode("DESCRIBE <" + base + path + ">", "UTF-8"));
resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/x-www-form-urlencoded");
resultEndpoint.expectedHeaderReceived(HTTP_METHOD, "POST");
resultEndpoint.expectedHeaderReceived(ACCEPT_CONTENT_TYPE, "application/rdf+xml");
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/org/fcrepo/camel/SparqlInsertProcessorTest.java
Expand Up @@ -19,6 +19,7 @@
import static org.fcrepo.camel.integration.FcrepoTestUtils.getFcrepoEndpointUri;
import static org.fcrepo.camel.integration.FcrepoTestUtils.getN3Document;
import static org.fcrepo.camel.integration.FcrepoTestUtils.getTurtleDocument;
import static java.net.URLEncoder.encode;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -55,12 +56,13 @@ public void testInsert() throws IOException, InterruptedException {
final String document = getN3Document();

// Assertions
resultEndpoint.allMessages().body().contains("update=INSERT DATA { ");
resultEndpoint.allMessages().body().contains(" }");
resultEndpoint.allMessages().body().startsWith("update=" + encode("INSERT DATA { ", "UTF-8"));
resultEndpoint.allMessages().body().endsWith(encode("}", "UTF-8"));
for (final String s : document.split("\n")) {
resultEndpoint.expectedBodyReceived().body().contains(s);
resultEndpoint.expectedBodyReceived().body().contains(encode(s, "UTF-8"));
}
resultEndpoint.expectedBodyReceived().body().contains("<" + base + path + "> dc:title \"some title\" .");
resultEndpoint.expectedBodyReceived().body().contains(
encode("<" + base + path + "> dc:title \"some title & other\" .", "UTF-8"));
resultEndpoint.expectedHeaderReceived("Content-Type", "application/x-www-form-urlencoded");
resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");

Expand Down
33 changes: 18 additions & 15 deletions src/test/java/org/fcrepo/camel/SparqlUpdateProcessorTest.java
Expand Up @@ -19,6 +19,7 @@
import static org.fcrepo.camel.integration.FcrepoTestUtils.getFcrepoEndpointUri;
import static org.fcrepo.camel.integration.FcrepoTestUtils.getN3Document;
import static org.fcrepo.camel.integration.FcrepoTestUtils.getTurtleDocument;
import static java.net.URLEncoder.encode;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -57,18 +58,19 @@ public void testNamedGraph() throws IOException, InterruptedException {
// Reverse the lines as the RDF may be serialized in opposite order

final String responsePrefix =
"update=DELETE WHERE { GRAPH <" + graph + "> { <" + base + path + "> ?p ?o } }; " +
"DELETE WHERE { GRAPH <" + graph + "> { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o } }; " +
"INSERT DATA { GRAPH <" + graph + "> { ";
final String responseSuffix = " } }";
"DELETE WHERE { GRAPH <" + graph + "> { <" + base + path + "> ?p ?o } };\n" +
"DELETE WHERE { GRAPH <" + graph + "> { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o } };\n" +
"INSERT DATA { GRAPH <" + graph + "> { ";
final String responseSuffix = "\n} }";

// Assertions
resultEndpoint.allMessages().body().contains(responsePrefix);
resultEndpoint.allMessages().body().contains(responseSuffix);
resultEndpoint.allMessages().body().startsWith("update=" + encode(responsePrefix, "UTF-8"));
resultEndpoint.allMessages().body().endsWith(encode(responseSuffix, "UTF-8"));
for (final String s : document.split("\n")) {
resultEndpoint.expectedBodyReceived().body().contains(s);
resultEndpoint.expectedBodyReceived().body().contains(encode(s, "UTF-8"));
}
resultEndpoint.expectedBodyReceived().body().contains("<" + base + path + "> dc:title \"some title\" .");
resultEndpoint.expectedBodyReceived().body().contains(
encode("<" + base + path + "> dc:title \"some title\" .", "UTF-8"));
resultEndpoint.expectedHeaderReceived("Content-Type", "application/x-www-form-urlencoded");
resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");

Expand Down Expand Up @@ -114,18 +116,19 @@ public void testUpdate() throws IOException, InterruptedException {
// Reverse the lines as the RDF may be serialized in opposite order

final String responsePrefix =
"update=DELETE WHERE { <" + base + path + "> ?p ?o }; " +
"DELETE WHERE { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o }; " +
"DELETE WHERE { <" + base + path + "> ?p ?o };\n" +
"DELETE WHERE { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o };\n" +
"INSERT DATA { ";
final String responseSuffix = " }";
final String responseSuffix = "\n}";

// Assertions
resultEndpoint.allMessages().body().contains(responsePrefix);
resultEndpoint.allMessages().body().contains(responseSuffix);
resultEndpoint.allMessages().body().startsWith("update=" + encode(responsePrefix, "UTF-8"));
resultEndpoint.allMessages().body().endsWith(encode(responseSuffix, "UTF-8"));
for (final String s : document.split("\n")) {
resultEndpoint.expectedBodyReceived().body().contains(s);
resultEndpoint.expectedBodyReceived().body().contains(encode(s, "UTF-8"));
}
resultEndpoint.expectedBodyReceived().body().contains("<" + base + path + "> dc:title \"some title\" .");
resultEndpoint.expectedBodyReceived().body().contains(
encode("<" + base + path + "> dc:title \"some title\" .", "UTF-8"));
resultEndpoint.expectedHeaderReceived("Content-Type", "application/x-www-form-urlencoded");
resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");

Expand Down
Expand Up @@ -75,7 +75,8 @@ public void testGetContainer() throws InterruptedException {
operationEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);

titleEndpoint.expectedMessageCount(3);
titleEndpoint.expectedBodiesReceivedInAnyOrder("some title", "some title", "some other title");
titleEndpoint.expectedBodiesReceivedInAnyOrder(
"some title &amp; other", "some title &amp; other", "some other title");
titleEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
titleEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);

Expand Down
Expand Up @@ -59,7 +59,7 @@ public class FcrepoPostIT extends CamelTestSupport {
public void testPost() throws InterruptedException {
// Assertions
resultEndpoint.expectedMessageCount(1);
resultEndpoint.expectedBodiesReceived("some title");
resultEndpoint.expectedBodiesReceived("some title &amp; other");

createdEndpoint.expectedMessageCount(1);
createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
Expand Down
Expand Up @@ -77,7 +77,7 @@ public static String getFcrepoEndpointUriWithScheme() {
*/
public static String getTurtleDocument() {
return "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n\n" +
"<> dc:title \"some title\" .";
"<> dc:title \"some title & other\" .";
}

/**
Expand All @@ -87,7 +87,7 @@ public static String getTurtleDocument() {
*/
public static String getN3Document() {
return "<http://localhost/rest/path/a/b/c> <http://purl.org/dc/elements/1.1/author> \"Author\" .\n" +
"<http://localhost/rest/path/a/b/c> <http://purl.org/dc/elements/1.1/title> \"Title\" .";
"<http://localhost/rest/path/a/b/c> <http://purl.org/dc/elements/1.1/title> \"This & That\" .";
}

/**
Expand Down

0 comments on commit 9fde12a

Please sign in to comment.