diff --git a/corese-server/src/test/java/fr/inria/corese/server/webservice/HTTPConnectionUtils.java b/corese-server/src/test/java/fr/inria/corese/server/webservice/HTTPConnectionUtils.java new file mode 100644 index 000000000..daef3b068 --- /dev/null +++ b/corese-server/src/test/java/fr/inria/corese/server/webservice/HTTPConnectionUtils.java @@ -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> 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 header : headers) { + con.setRequestProperty(header.get(0), header.get(1)); + } + return con; + } + + public static HttpURLConnection getConnection(String url, List> headers) + throws IOException { + return methodConnection("GET", url, headers); + } + + public static HttpURLConnection postConnection(String url, List> 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> headers, String body) + throws IOException { + List> newHeaders = new ArrayList<>(headers); + List 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> 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> 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> headers) + throws IOException { + return methodConnection("HEAD", url, headers); + } + + public static HttpURLConnection headConnection(String url) + throws IOException { + return headConnection(url, new ArrayList<>()); + } +} diff --git a/corese-server/src/test/java/fr/inria/corese/server/webservice/SPARQLTestUtils.java b/corese-server/src/test/java/fr/inria/corese/server/webservice/SPARQLTestUtils.java index 1ef1e5f4e..a4e07e9f2 100644 --- a/corese-server/src/test/java/fr/inria/corese/server/webservice/SPARQLTestUtils.java +++ b/corese-server/src/test/java/fr/inria/corese/server/webservice/SPARQLTestUtils.java @@ -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; @@ -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> 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 header : headers) { - con.setRequestProperty(header.get(0), header.get(1)); - } - return con; - } - - public static HttpURLConnection getConnection(String url, List> headers) - throws IOException { - return methodConnection("GET", url, headers); - } - - public static HttpURLConnection postConnection(String url, List> 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> headers, String body) - throws IOException { - List> newHeaders = new ArrayList<>(headers); - List 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> 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> 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> 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> optionalParameters) { return generateSPARQLParameters("query", query, optionalParameters); } @@ -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())); @@ -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())); @@ -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()));