Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.bgproute; |
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 | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 15 | public final class RestClient { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 16 | private static final Logger log = LoggerFactory.getLogger(RestClient.class); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 18 | private RestClient() { |
| 19 | // Private constructor to prevent instantiation |
| 20 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 21 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | public static String get(String str) { |
| 23 | StringBuilder response = new StringBuilder(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 24 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 25 | try { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | URL url = new URL(str); |
| 27 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 28 | conn.setConnectTimeout(2 * 1000); // 2 seconds |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 29 | conn.setRequestMethod("GET"); |
| 30 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 31 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | if (conn.getResponseCode() != 200) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 33 | // XXX bad. RestClient API needs to be redesigned |
| 34 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | + conn.getResponseCode()); |
| 36 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 37 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 38 | if (!conn.getContentType().equals("application/json")) { |
| 39 | log.warn("The content received from {} is not json", str); |
| 40 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 41 | |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame^] | 42 | BufferedReader br = new BufferedReader(new InputStreamReader( |
| 43 | conn.getInputStream(), StandardCharsets.UTF_8)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | String line; |
| 45 | while ((line = br.readLine()) != null) { |
| 46 | response.append(line); |
| 47 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 48 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | br.close(); |
| 50 | conn.disconnect(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | } catch (MalformedURLException e) { |
| 53 | log.error("Malformed URL for GET request", e); |
| 54 | } catch (ConnectTimeoutException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 55 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | } catch (IOException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 57 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 59 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | return response.toString(); |
| 61 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 62 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | public static void post(String str) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 64 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | 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 Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 73 | // XXX bad. RestClient API needs to be redesigned |
| 74 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | + 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 Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 83 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 86 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 87 | public static void delete(String str) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 88 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | try { |
| 90 | URL url = new URL(str); |
| 91 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 92 | conn.setRequestMethod("DELETE"); |
| 93 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 94 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | if (conn.getResponseCode() != 200) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 96 | // XXX bad. RestClient API needs to be redesigned |
| 97 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | + conn.getResponseCode()); |
| 99 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 100 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | conn.disconnect(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 102 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | } catch (MalformedURLException e) { |
| 104 | log.error("Malformed URL for GET request", e); |
| 105 | } catch (IOException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 106 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 109 | } |