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; |
| 9 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 10 | import org.apache.commons.httpclient.ConnectTimeoutException; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
| 13 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 14 | public final class RestClient { |
| 15 | private final static Logger log = LoggerFactory.getLogger(RestClient.class); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 16 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 17 | private RestClient() { |
| 18 | // Private constructor to prevent instantiation |
| 19 | } |
| 20 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | public static String get(String str) { |
| 22 | StringBuilder response = new StringBuilder(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 23 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | try { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 25 | URL url = new URL(str); |
| 26 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 27 | conn.setConnectTimeout(2 * 1000); // 2 seconds |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | conn.setRequestMethod("GET"); |
| 29 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 30 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | if (conn.getResponseCode() != 200) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 32 | // XXX bad. RestClient API needs to be redesigned |
| 33 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | + conn.getResponseCode()); |
| 35 | } |
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 | if (!conn.getContentType().equals("application/json")) { |
| 38 | log.warn("The content received from {} is not json", str); |
| 39 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 40 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); |
| 42 | String line; |
| 43 | while ((line = br.readLine()) != null) { |
| 44 | response.append(line); |
| 45 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 46 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | br.close(); |
| 48 | conn.disconnect(); |
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 | } catch (MalformedURLException e) { |
| 51 | log.error("Malformed URL for GET request", e); |
| 52 | } catch (ConnectTimeoutException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 53 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | } catch (IOException 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 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 57 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | return response.toString(); |
| 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 | public static void post(String str) { |
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 | 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 Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 71 | // XXX bad. RestClient API needs to be redesigned |
| 72 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | + 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 Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 81 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | public static void delete(String str) { |
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 | try { |
| 88 | URL url = new URL(str); |
| 89 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 90 | conn.setRequestMethod("DELETE"); |
| 91 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 92 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | if (conn.getResponseCode() != 200) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 94 | // XXX bad. RestClient API needs to be redesigned |
| 95 | throw new IOException("Failed : HTTP error code : " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | + conn.getResponseCode()); |
| 97 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 98 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | conn.disconnect(); |
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 | } catch (MalformedURLException e) { |
| 102 | log.error("Malformed URL for GET request", e); |
| 103 | } catch (IOException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 104 | log.warn("Couldn't connect to the remote REST server", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 107 | } |