Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.sdnip; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.IOException; |
| 5 | import java.io.InputStreamReader; |
| 6 | import java.net.HttpURLConnection; |
| 7 | import java.net.MalformedURLException; |
| 8 | import java.net.URL; |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 9 | import java.nio.charset.StandardCharsets; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 10 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 11 | import org.apache.commons.httpclient.ConnectTimeoutException; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 15 | /** |
| 16 | * A simple HTTP client. It is used to make REST calls to the BGPd process. |
| 17 | */ |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 18 | public final class RestClient { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 19 | private static final Logger log = LoggerFactory.getLogger(RestClient.class); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 20 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Default constructor. |
| 23 | */ |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 24 | private RestClient() { |
| 25 | // Private constructor to prevent instantiation |
| 26 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 27 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 28 | /** |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | StringBuilder response = new StringBuilder(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 36 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | try { |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 38 | URL url = new URL(strUrl); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 40 | conn.setConnectTimeout(2 * 1000); // 2 seconds |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | conn.setRequestMethod("GET"); |
| 42 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 43 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | if (conn.getResponseCode() != 200) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 45 | // XXX bad. RestClient API needs to be redesigned |
| 46 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | + conn.getResponseCode()); |
| 48 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 49 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | if (!conn.getContentType().equals("application/json")) { |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 51 | log.warn("The content received from {} is not json", strUrl); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 53 | |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 54 | BufferedReader br = new BufferedReader(new InputStreamReader( |
| 55 | conn.getInputStream(), StandardCharsets.UTF_8)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | String line; |
| 57 | while ((line = br.readLine()) != null) { |
| 58 | response.append(line); |
| 59 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | br.close(); |
| 62 | conn.disconnect(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 63 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | } catch (MalformedURLException e) { |
| 65 | log.error("Malformed URL for GET request", e); |
| 66 | } catch (ConnectTimeoutException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 67 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | } catch (IOException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 69 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 71 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | return response.toString(); |
| 73 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 74 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 75 | /** |
| 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 Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 81 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | try { |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 83 | URL url = new URL(strUrl); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | 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 Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 90 | // XXX bad. RestClient API needs to be redesigned |
| 91 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | + 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 Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 100 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 103 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 104 | /** |
| 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 Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 110 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | try { |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 112 | URL url = new URL(strUrl); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 114 | conn.setRequestMethod("DELETE"); |
| 115 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 116 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | if (conn.getResponseCode() != 200) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 118 | // XXX bad. RestClient API needs to be redesigned |
| 119 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | + conn.getResponseCode()); |
| 121 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 122 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | conn.disconnect(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 124 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 125 | } catch (MalformedURLException e) { |
| 126 | log.error("Malformed URL for GET request", e); |
| 127 | } catch (IOException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 128 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 131 | } |