Skip to content

Commit

Permalink
refactoring of utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
MaillPierre committed Aug 27, 2024
1 parent 2d84a53 commit 0bb37b6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package fr.inria.corese.server.webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 @@ -7,14 +7,10 @@

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -37,82 +33,6 @@ public class SPARQLTestUtils {
private static final String SERVER_URL = "http://localhost:8080/";
private static final String SPARQL_ENDPOINT_URL = SERVER_URL + "sparql";

/**
* 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<>());
}

public static String generateSPARQLQueryParameters(String query, List<List<String>> optionalParameters) {
return generateSPARQLParameters("query", query, optionalParameters);
}
Expand Down Expand Up @@ -166,7 +86,7 @@ public static Mappings sendSPARQLSelect(String query) throws Exception {
headers.add(acceptHeader);

String urlQuery = SPARQL_ENDPOINT_URL + "?" + SPARQLTestUtils.generateSPARQLQueryParameters(query);
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand All @@ -190,7 +110,7 @@ public static boolean sendSPARQLAsk(String query) throws Exception {
headers.add(acceptHeader);

String urlQuery = SPARQL_ENDPOINT_URL + "?" + SPARQLTestUtils.generateSPARQLQueryParameters(query);
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand All @@ -214,7 +134,7 @@ public static Graph sendSPARQLConstructDescribe(String query) throws Exception {
headers.add(acceptHeader);

String urlQuery = SPARQL_ENDPOINT_URL + "?" + SPARQLTestUtils.generateSPARQLQueryParameters(query);
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

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

0 comments on commit 0bb37b6

Please sign in to comment.