blob: fe349d54b17e77a72d5056c0aca9f8b19c6982a5 [file] [log] [blame]
Jonathan Hart8f6dc092014-04-18 15:56:43 -07001package net.onrc.onos.apps.sdnip;
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 Hart31e15f12014-04-10 10:33:00 -070015/**
16 * A simple HTTP client. It is used to make REST calls to the BGPd process.
17 */
Jonathan Hart738980f2014-04-04 10:11:15 -070018public final class RestClient {
Ray Milkeyec838942014-04-09 11:28:43 -070019 private static final Logger log = LoggerFactory.getLogger(RestClient.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070020
Jonathan Hart31e15f12014-04-10 10:33:00 -070021 /**
22 * Default constructor.
23 */
Jonathan Hart738980f2014-04-04 10:11:15 -070024 private RestClient() {
25 // Private constructor to prevent instantiation
26 }
Ray Milkey5d406012014-04-08 14:44:41 -070027
Jonathan Hart31e15f12014-04-10 10:33:00 -070028 /**
29 * Issues a HTTP GET request to the specified URL.
30 *
31 * @param strUrl the URL to make the request to
32 * @return a String containing any data returned by the server
33 */
34 public static String get(String strUrl) {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 StringBuilder response = new StringBuilder();
Jonathan Hart61ba9372013-05-19 20:10:29 -070036
Ray Milkey269ffb92014-04-03 14:43:30 -070037 try {
Jonathan Hart31e15f12014-04-10 10:33:00 -070038 URL url = new URL(strUrl);
Ray Milkey269ffb92014-04-03 14:43:30 -070039 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Jonathan Hart738980f2014-04-04 10:11:15 -070040 conn.setConnectTimeout(2 * 1000); // 2 seconds
Ray Milkey269ffb92014-04-03 14:43:30 -070041 conn.setRequestMethod("GET");
42 conn.setRequestProperty("Accept", "application/json");
Jonathan Hart61ba9372013-05-19 20:10:29 -070043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070045 // XXX bad. RestClient API needs to be redesigned
46 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070047 + conn.getResponseCode());
48 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 if (!conn.getContentType().equals("application/json")) {
Jonathan Hart31e15f12014-04-10 10:33:00 -070051 log.warn("The content received from {} is not json", strUrl);
Ray Milkey269ffb92014-04-03 14:43:30 -070052 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070053
Jonathan Hartec9ee2e2014-04-08 22:45:44 -070054 BufferedReader br = new BufferedReader(new InputStreamReader(
55 conn.getInputStream(), StandardCharsets.UTF_8));
Ray Milkey269ffb92014-04-03 14:43:30 -070056 String line;
57 while ((line = br.readLine()) != null) {
58 response.append(line);
59 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070060
Ray Milkey269ffb92014-04-03 14:43:30 -070061 br.close();
62 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -070063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 } catch (MalformedURLException e) {
65 log.error("Malformed URL for GET request", e);
66 } catch (ConnectTimeoutException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -070067 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070068 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -070069 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070070 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 return response.toString();
73 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070074
Jonathan Hart31e15f12014-04-10 10:33:00 -070075 /**
76 * Issues a HTTP POST request to the specified URL.
77 *
78 * @param strUrl the URL to make the request to
79 */
80 public static void post(String strUrl) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 try {
Jonathan Hart31e15f12014-04-10 10:33:00 -070083 URL url = new URL(strUrl);
Ray Milkey269ffb92014-04-03 14:43:30 -070084 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
85 conn.setDoOutput(true);
86 conn.setRequestMethod("POST");
87 conn.setRequestProperty("Content-Type", "application/json");
88
89 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -070090 // XXX bad. RestClient API needs to be redesigned
91 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -070092 + conn.getResponseCode());
93 }
94
95 conn.disconnect();
96
97 } catch (MalformedURLException e) {
98 log.error("Malformed URL for GET request", e);
99 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700100 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 }
102 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700103
Jonathan Hart31e15f12014-04-10 10:33:00 -0700104 /**
105 * Issues a HTTP DELETE request to the specified URL.
106 *
107 * @param strUrl the URL to make the request to
108 */
109 public static void delete(String strUrl) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700110
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 try {
Jonathan Hart31e15f12014-04-10 10:33:00 -0700112 URL url = new URL(strUrl);
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
114 conn.setRequestMethod("DELETE");
115 conn.setRequestProperty("Accept", "application/json");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 if (conn.getResponseCode() != 200) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700118 // XXX bad. RestClient API needs to be redesigned
119 throw new IOException("Failed : HTTP error code : "
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 + conn.getResponseCode());
121 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700122
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -0700124
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 } catch (MalformedURLException e) {
126 log.error("Malformed URL for GET request", e);
127 } catch (IOException e) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700128 log.warn("Couldn't connect to the remote REST server", e);
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 }
130 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700131}