-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d84a53
commit 0bb37b6
Showing
2 changed files
with
93 additions
and
83 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
corese-server/src/test/java/fr/inria/corese/server/webservice/HTTPConnectionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters