blob: f1fc37075c6a62d7631bbd910566502fc36624d1 [file] [log] [blame]
Jonathan Hart382623d2014-04-03 09:48:11 -07001package net.onrc.onos.apps.bgproute;
Jonathan Hart61ba9372013-05-19 20:10:29 -07002
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.net.HttpURLConnection;
7import java.net.MalformedURLException;
8import java.net.URL;
Jonathan Hartec9ee2e2014-04-08 22:45:44 -07009import java.nio.charset.StandardCharsets;
Jonathan Hart61ba9372013-05-19 20:10:29 -070010
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070011import org.apache.commons.httpclient.ConnectTimeoutException;
Jonathan Hart61ba9372013-05-19 20:10:29 -070012import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14
Jonathan Hart738980f2014-04-04 10:11:15 -070015public final class RestClient {
Ray Milkeyec838942014-04-09 11:28:43 -070016 private static final Logger log = LoggerFactory.getLogger(RestClient.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070017
Jonathan Hart738980f2014-04-04 10:11:15 -070018 private RestClient() {
19 // Private constructor to prevent instantiation
20 }
Ray Milkey5d406012014-04-08 14:44:41 -070021
Ray Milkey269ffb92014-04-03 14:43:30 -070022 public static String get(String str) {
23 StringBuilder response = new StringBuilder();
Jonathan Hart61ba9372013-05-19 20:10:29 -070024
Ray Milkey269ffb92014-04-03 14:43:30 -070025 try {
Ray Milkey269ffb92014-04-03 14:43:30 -070026 URL url = new URL(str);
27 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Jonathan Hart738980f2014-04-04 10:11:15 -070028 conn.setConnectTimeout(2 * 1000); // 2 seconds
Ray Milkey269ffb92014-04-03 14:43:30 -070029 conn.setRequestMethod("GET");
30 conn.setRequestProperty("Accept", "application/json");
Jonathan Hart61ba9372013-05-19 20:10:29 -070031
Ray Milkey269ffb92014-04-03 14:43:30 -070032 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070033 // XXX bad. RestClient API needs to be redesigned
34 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070035 + conn.getResponseCode());
36 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070037
Ray Milkey269ffb92014-04-03 14:43:30 -070038 if (!conn.getContentType().equals("application/json")) {
39 log.warn("The content received from {} is not json", str);
40 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070041
Jonathan Hartec9ee2e2014-04-08 22:45:44 -070042 BufferedReader br = new BufferedReader(new InputStreamReader(
43 conn.getInputStream(), StandardCharsets.UTF_8));
Ray Milkey269ffb92014-04-03 14:43:30 -070044 String line;
45 while ((line = br.readLine()) != null) {
46 response.append(line);
47 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070048
Ray Milkey269ffb92014-04-03 14:43:30 -070049 br.close();
50 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -070051
Ray Milkey269ffb92014-04-03 14:43:30 -070052 } catch (MalformedURLException e) {
53 log.error("Malformed URL for GET request", e);
54 } catch (ConnectTimeoutException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -070055 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070056 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -070057 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070058 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 return response.toString();
61 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 public static void post(String str) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070064
Ray Milkey269ffb92014-04-03 14:43:30 -070065 try {
66 URL url = new URL(str);
67 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
68 conn.setDoOutput(true);
69 conn.setRequestMethod("POST");
70 conn.setRequestProperty("Content-Type", "application/json");
71
72 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070073 // XXX bad. RestClient API needs to be redesigned
74 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070075 + conn.getResponseCode());
76 }
77
78 conn.disconnect();
79
80 } catch (MalformedURLException e) {
81 log.error("Malformed URL for GET request", e);
82 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -070083 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070084 }
85 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070086
Ray Milkey269ffb92014-04-03 14:43:30 -070087 public static void delete(String str) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 try {
90 URL url = new URL(str);
91 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
92 conn.setRequestMethod("DELETE");
93 conn.setRequestProperty("Accept", "application/json");
Jonathan Hart61ba9372013-05-19 20:10:29 -070094
Ray Milkey269ffb92014-04-03 14:43:30 -070095 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070096 // XXX bad. RestClient API needs to be redesigned
97 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070098 + conn.getResponseCode());
99 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -0700102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 } catch (MalformedURLException e) {
104 log.error("Malformed URL for GET request", e);
105 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700106 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 }
108 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700109}