blob: 3164945ed3467dafbb62e2d52e0979ed7e6faaab [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;
9
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070010import org.apache.commons.httpclient.ConnectTimeoutException;
Jonathan Hart61ba9372013-05-19 20:10:29 -070011import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
Jonathan Hart738980f2014-04-04 10:11:15 -070014public final class RestClient {
15 private final static Logger log = LoggerFactory.getLogger(RestClient.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070016
Jonathan Hart738980f2014-04-04 10:11:15 -070017 private RestClient() {
18 // Private constructor to prevent instantiation
19 }
20
Ray Milkey269ffb92014-04-03 14:43:30 -070021 public static String get(String str) {
22 StringBuilder response = new StringBuilder();
Jonathan Hart61ba9372013-05-19 20:10:29 -070023
Ray Milkey269ffb92014-04-03 14:43:30 -070024 try {
Ray Milkey269ffb92014-04-03 14:43:30 -070025 URL url = new URL(str);
26 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Jonathan Hart738980f2014-04-04 10:11:15 -070027 conn.setConnectTimeout(2 * 1000); // 2 seconds
Ray Milkey269ffb92014-04-03 14:43:30 -070028 conn.setRequestMethod("GET");
29 conn.setRequestProperty("Accept", "application/json");
Jonathan Hart61ba9372013-05-19 20:10:29 -070030
Ray Milkey269ffb92014-04-03 14:43:30 -070031 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070032 // XXX bad. RestClient API needs to be redesigned
33 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070034 + conn.getResponseCode());
35 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070036
Ray Milkey269ffb92014-04-03 14:43:30 -070037 if (!conn.getContentType().equals("application/json")) {
38 log.warn("The content received from {} is not json", str);
39 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070040
Ray Milkey269ffb92014-04-03 14:43:30 -070041 BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
42 String line;
43 while ((line = br.readLine()) != null) {
44 response.append(line);
45 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070046
Ray Milkey269ffb92014-04-03 14:43:30 -070047 br.close();
48 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -070049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 } catch (MalformedURLException e) {
51 log.error("Malformed URL for GET request", e);
52 } catch (ConnectTimeoutException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -070053 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070054 } catch (IOException 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 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070057
Ray Milkey269ffb92014-04-03 14:43:30 -070058 return response.toString();
59 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070060
Ray Milkey269ffb92014-04-03 14:43:30 -070061 public static void post(String str) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 try {
64 URL url = new URL(str);
65 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
66 conn.setDoOutput(true);
67 conn.setRequestMethod("POST");
68 conn.setRequestProperty("Content-Type", "application/json");
69
70 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070071 // XXX bad. RestClient API needs to be redesigned
72 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070073 + conn.getResponseCode());
74 }
75
76 conn.disconnect();
77
78 } catch (MalformedURLException e) {
79 log.error("Malformed URL for GET request", e);
80 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -070081 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070082 }
83 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 public static void delete(String str) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070086
Ray Milkey269ffb92014-04-03 14:43:30 -070087 try {
88 URL url = new URL(str);
89 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
90 conn.setRequestMethod("DELETE");
91 conn.setRequestProperty("Accept", "application/json");
Jonathan Hart61ba9372013-05-19 20:10:29 -070092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070094 // XXX bad. RestClient API needs to be redesigned
95 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070096 + conn.getResponseCode());
97 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -0700100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 } catch (MalformedURLException e) {
102 log.error("Malformed URL for GET request", e);
103 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700104 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 }
106 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700107}