Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 91267b4

Browse files
committed
Merge branch 'Unconsistentbehaviorbetweendirectpostandurl-encodedpostforUpdateinCorese-Server#191' into development
2 parents 0a26499 + f04dca1 commit 91267b4

File tree

7 files changed

+549
-372
lines changed

7 files changed

+549
-372
lines changed

corese-server/src/main/java/fr/inria/corese/server/webservice/SPARQLRestAPI.java

+185-203
Large diffs are not rendered by default.

corese-server/src/test/java/fr/inria/corese/server/webservice/GraphStoreProtocolTest.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void getGraphStoreProtocolWithGraph() throws Exception {
7979
headers.add(acceptHeader);
8080

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

8484
BufferedReader in = new BufferedReader(
8585
new InputStreamReader(con.getInputStream()));
@@ -112,7 +112,7 @@ public void getGraphStoreProtocolWithDefault() throws Exception{
112112
headers.add(acceptHeader);
113113

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

117117
BufferedReader in = new BufferedReader(
118118
new InputStreamReader(con.getInputStream()));
@@ -147,7 +147,7 @@ public void getGraphStoreProtocolWithUnknownGraph() throws Exception{
147147
headers.add(acceptHeader);
148148

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

152152
BufferedReader in = new BufferedReader(
153153
new InputStreamReader(con.getInputStream()));
@@ -186,7 +186,7 @@ public void putGraphStoreProtocolNewGraph() throws Exception {
186186

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

189-
HttpURLConnection con = SPARQLTestUtils.putConnection(urlQuery, headers, rdfPayload);
189+
HttpURLConnection con = HTTPConnectionUtils.putConnection(urlQuery, headers, rdfPayload);
190190

191191
int status = con.getResponseCode();
192192

@@ -214,7 +214,7 @@ public void putGraphStoreProtocolExistingGraph() throws Exception {
214214

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

217-
HttpURLConnection con = SPARQLTestUtils.putConnection(urlQuery, headers, rdfPayload);
217+
HttpURLConnection con = HTTPConnectionUtils.putConnection(urlQuery, headers, rdfPayload);
218218

219219
int status = con.getResponseCode();
220220

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

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

238238
int status = deleteCon.getResponseCode();
239239

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

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

257257
int status = deleteCon.getResponseCode();
258258

@@ -280,7 +280,7 @@ public void postGraphStoreProtocolNewGraph() throws Exception {
280280

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

283-
HttpURLConnection con = SPARQLTestUtils.postConnection(urlQuery, headers, rdfPayload);
283+
HttpURLConnection con = HTTPConnectionUtils.postConnection(urlQuery, headers, rdfPayload);
284284

285285
int status = con.getResponseCode();
286286

@@ -308,7 +308,7 @@ public void postGraphStoreProtocolExistingGraph() throws Exception {
308308

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

311-
HttpURLConnection con = SPARQLTestUtils.postConnection(urlQuery, headers, rdfPayload);
311+
HttpURLConnection con = HTTPConnectionUtils.postConnection(urlQuery, headers, rdfPayload);
312312

313313
int status = con.getResponseCode();
314314

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

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

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

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

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

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

386386
BufferedReader in = new BufferedReader(
387387
new InputStreamReader(con.getInputStream()));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package fr.inria.corese.server.webservice;
2+
3+
import java.io.IOException;
4+
import java.net.HttpURLConnection;
5+
import java.net.MalformedURLException;
6+
import java.net.ProtocolException;
7+
import java.net.URL;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class HTTPConnectionUtils {
12+
13+
/**
14+
* Get a connection to a server.
15+
*
16+
* @param url server URL
17+
* @param headers HTTP headers
18+
* @return
19+
* @throws MalformedURLException
20+
* @throws IOException
21+
* @throws ProtocolException
22+
*/
23+
24+
public static HttpURLConnection methodConnection(String method, String url, List<List<String>> headers)
25+
throws IOException {
26+
URL u = new URL(url);
27+
HttpURLConnection con = (HttpURLConnection) u.openConnection();
28+
con.setRequestMethod(method);
29+
con.setConnectTimeout(5000);
30+
con.setReadTimeout(5000);
31+
con.setInstanceFollowRedirects(true);
32+
for (List<String> header : headers) {
33+
con.setRequestProperty(header.get(0), header.get(1));
34+
}
35+
return con;
36+
}
37+
38+
public static HttpURLConnection getConnection(String url, List<List<String>> headers)
39+
throws IOException {
40+
return methodConnection("GET", url, headers);
41+
}
42+
43+
public static HttpURLConnection postConnection(String url, List<List<String>> headers, String body)
44+
throws IOException {
45+
HttpURLConnection con = methodConnection("POST", url, headers);
46+
con.setDoOutput(true);
47+
con.getOutputStream().write(body.getBytes());
48+
return con;
49+
}
50+
51+
public static HttpURLConnection postUrlencodedConnection(String url, List<List<String>> headers, String body)
52+
throws IOException {
53+
List<List<String>> newHeaders = new ArrayList<>(headers);
54+
List<String> contentTypeHeader = new ArrayList<>();
55+
contentTypeHeader.add("Content-Type");
56+
contentTypeHeader.add("application/x-www-form-urlencoded");
57+
newHeaders.add(contentTypeHeader);
58+
return postConnection(url, newHeaders, body);
59+
}
60+
61+
public static HttpURLConnection putConnection(String url, List<List<String>> headers, String body)
62+
throws IOException {
63+
HttpURLConnection con = methodConnection("PUT", url, headers);
64+
con.setDoOutput(true);
65+
con.getOutputStream().write(body.getBytes());
66+
return con;
67+
}
68+
69+
public static HttpURLConnection deleteConnection(String url, List<List<String>> headers)
70+
throws IOException {
71+
return methodConnection("DELETE", url, headers);
72+
}
73+
74+
public static HttpURLConnection deleteConnection(String url)
75+
throws IOException {
76+
return deleteConnection( url, new ArrayList<>());
77+
}
78+
79+
public static HttpURLConnection headConnection(String url, List<List<String>> headers)
80+
throws IOException {
81+
return methodConnection("HEAD", url, headers);
82+
}
83+
84+
public static HttpURLConnection headConnection(String url)
85+
throws IOException {
86+
return headConnection(url, new ArrayList<>());
87+
}
88+
}

corese-server/src/test/java/fr/inria/corese/server/webservice/SPARQLEndpointDescriptionTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void sparqlWellKnownVoidXMLRDF() throws Exception {
9898
contentTypeHeader.add(RDF_XML);
9999
headers.add(contentTypeHeader);
100100

101-
HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
101+
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);
102102

103103
BufferedReader in = new BufferedReader(
104104
new InputStreamReader(con.getInputStream()));
@@ -139,7 +139,7 @@ public void sparqlWellKnownVoidTurtleRDF() throws Exception {
139139
contentTypeHeader.add(TURTLE_TEXT);
140140
headers.add(contentTypeHeader);
141141

142-
HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
142+
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);
143143

144144
BufferedReader in = new BufferedReader(
145145
new InputStreamReader(con.getInputStream()));
@@ -180,7 +180,7 @@ public void wellKnownVoidXMLRDF() throws Exception {
180180
contentTypeHeader.add(RDF_XML);
181181
headers.add(contentTypeHeader);
182182

183-
HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
183+
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);
184184

185185
BufferedReader in = new BufferedReader(
186186
new InputStreamReader(con.getInputStream()));
@@ -221,7 +221,7 @@ public void wellKnownVoidTurtleRDF() throws Exception {
221221
contentTypeHeader.add(TURTLE_TEXT);
222222
headers.add(contentTypeHeader);
223223

224-
HttpURLConnection con = SPARQLTestUtils.getConnection(sparqlEndpoint, headers);
224+
HttpURLConnection con = HTTPConnectionUtils.getConnection(sparqlEndpoint, headers);
225225

226226
BufferedReader in = new BufferedReader(
227227
new InputStreamReader(con.getInputStream()));

0 commit comments

Comments
 (0)