Skip to content

Commit

Permalink
Merge branch 'Unconsistentbehaviorbetweendirectpostandurl-encodedpost…
Browse files Browse the repository at this point in the history
…forUpdateinCorese-Server#191' into development
  • Loading branch information
MaillPierre committed Aug 28, 2024
2 parents 0a26499 + f04dca1 commit 91267b4
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 372 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void getGraphStoreProtocolWithGraph() throws Exception {
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/A");
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -112,7 +112,7 @@ public void getGraphStoreProtocolWithDefault() throws Exception{
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("default");
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -147,7 +147,7 @@ public void getGraphStoreProtocolWithUnknownGraph() throws Exception{
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/Z");
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -186,7 +186,7 @@ public void putGraphStoreProtocolNewGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.putConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.putConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand Down Expand Up @@ -214,7 +214,7 @@ public void putGraphStoreProtocolExistingGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.putConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.putConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand All @@ -233,7 +233,7 @@ public void deleteGraphStoreProtocol() throws Exception {
boolean presenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/B> { ?s ?p ?o } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/B");
HttpURLConnection deleteCon = SPARQLTestUtils.deleteConnection(urlQuery);
HttpURLConnection deleteCon = HTTPConnectionUtils.deleteConnection(urlQuery);

int status = deleteCon.getResponseCode();

Expand All @@ -252,7 +252,7 @@ public void deleteGraphStoreProtocolWithUnknownGraph() throws Exception {
boolean presenceTest = ! SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/Z> { ?s ?p ?o } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/Z");
HttpURLConnection deleteCon = SPARQLTestUtils.deleteConnection(urlQuery);
HttpURLConnection deleteCon = HTTPConnectionUtils.deleteConnection(urlQuery);

int status = deleteCon.getResponseCode();

Expand Down Expand Up @@ -280,7 +280,7 @@ public void postGraphStoreProtocolNewGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.postConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.postConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand Down Expand Up @@ -308,7 +308,7 @@ public void postGraphStoreProtocolExistingGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.postConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.postConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand All @@ -331,7 +331,7 @@ public void headGraphStoreProtocolWithDefault() throws Exception{
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("default");
HttpURLConnection con = SPARQLTestUtils.headConnection(urlQuery);
HttpURLConnection con = HTTPConnectionUtils.headConnection(urlQuery);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand All @@ -356,7 +356,7 @@ public void headGraphStoreProtocolWithGraph() throws Exception{
boolean presenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/A> { ?x ?y ?z } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/A");
HttpURLConnection con = SPARQLTestUtils.headConnection(urlQuery);
HttpURLConnection con = HTTPConnectionUtils.headConnection(urlQuery);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
Expand All @@ -381,7 +381,7 @@ public void headGraphStoreProtocolWithUnknownGraph() throws Exception{
boolean absenceTest = ! SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/Z> { ?x ?y ?z } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/Z");
HttpURLConnection con = SPARQLTestUtils.headConnection(urlQuery);
HttpURLConnection con = HTTPConnectionUtils.headConnection(urlQuery);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.inria.corese.server.webservice;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HTTPConnectionUtils {

/**
* Get a connection to a server.
*
* @param url server URL
* @param headers HTTP headers
* @return
* @throws MalformedURLException
* @throws IOException
* @throws ProtocolException
*/

public static HttpURLConnection methodConnection(String method, String url, List<List<String>> headers)
throws IOException {
URL u = new URL(url);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
con.setRequestMethod(method);
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.setInstanceFollowRedirects(true);
for (List<String> header : headers) {
con.setRequestProperty(header.get(0), header.get(1));
}
return con;
}

public static HttpURLConnection getConnection(String url, List<List<String>> headers)
throws IOException {
return methodConnection("GET", url, headers);
}

public static HttpURLConnection postConnection(String url, List<List<String>> headers, String body)
throws IOException {
HttpURLConnection con = methodConnection("POST", url, headers);
con.setDoOutput(true);
con.getOutputStream().write(body.getBytes());
return con;
}

public static HttpURLConnection postUrlencodedConnection(String url, List<List<String>> headers, String body)
throws IOException {
List<List<String>> newHeaders = new ArrayList<>(headers);
List<String> contentTypeHeader = new ArrayList<>();
contentTypeHeader.add("Content-Type");
contentTypeHeader.add("application/x-www-form-urlencoded");
newHeaders.add(contentTypeHeader);
return postConnection(url, newHeaders, body);
}

public static HttpURLConnection putConnection(String url, List<List<String>> headers, String body)
throws IOException {
HttpURLConnection con = methodConnection("PUT", url, headers);
con.setDoOutput(true);
con.getOutputStream().write(body.getBytes());
return con;
}

public static HttpURLConnection deleteConnection(String url, List<List<String>> headers)
throws IOException {
return methodConnection("DELETE", url, headers);
}

public static HttpURLConnection deleteConnection(String url)
throws IOException {
return deleteConnection( url, new ArrayList<>());
}

public static HttpURLConnection headConnection(String url, List<List<String>> headers)
throws IOException {
return methodConnection("HEAD", url, headers);
}

public static HttpURLConnection headConnection(String url)
throws IOException {
return headConnection(url, new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void sparqlWellKnownVoidXMLRDF() throws Exception {
contentTypeHeader.add(RDF_XML);
headers.add(contentTypeHeader);

HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -139,7 +139,7 @@ public void sparqlWellKnownVoidTurtleRDF() throws Exception {
contentTypeHeader.add(TURTLE_TEXT);
headers.add(contentTypeHeader);

HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -180,7 +180,7 @@ public void wellKnownVoidXMLRDF() throws Exception {
contentTypeHeader.add(RDF_XML);
headers.add(contentTypeHeader);

HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -221,7 +221,7 @@ public void wellKnownVoidTurtleRDF() throws Exception {
contentTypeHeader.add(TURTLE_TEXT);
headers.add(contentTypeHeader);

HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down
Loading

0 comments on commit 91267b4

Please sign in to comment.