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 | |
| 14 | |
| 15 | public class RestClient { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 16 | protected final static Logger log = LoggerFactory.getLogger(RestClient.class); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | public static String get(String str) { |
| 19 | StringBuilder response = new StringBuilder(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 20 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | try { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 22 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 23 | URL url = new URL(str); |
| 24 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 25 | conn.setConnectTimeout(2 * 1000); //2 seconds |
| 26 | conn.setRequestMethod("GET"); |
| 27 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 28 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 29 | if (conn.getResponseCode() != 200) { |
| 30 | throw new RuntimeException("Failed : HTTP error code : " |
| 31 | + conn.getResponseCode()); |
| 32 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 33 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | if (!conn.getContentType().equals("application/json")) { |
| 35 | log.warn("The content received from {} is not json", str); |
| 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 | BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); |
| 39 | String line; |
| 40 | while ((line = br.readLine()) != null) { |
| 41 | response.append(line); |
| 42 | } |
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 | br.close(); |
| 45 | conn.disconnect(); |
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 | } catch (MalformedURLException e) { |
| 48 | log.error("Malformed URL for GET request", e); |
| 49 | } catch (ConnectTimeoutException e) { |
| 50 | log.warn("Couldn't connect remote REST server"); |
| 51 | } catch (IOException e) { |
| 52 | log.warn("Couldn't connect remote REST server"); |
| 53 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 54 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | return response.toString(); |
| 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 | public static void post(String str) { |
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 | try { |
| 61 | URL url = new URL(str); |
| 62 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 63 | conn.setDoOutput(true); |
| 64 | conn.setRequestMethod("POST"); |
| 65 | conn.setRequestProperty("Content-Type", "application/json"); |
| 66 | |
| 67 | if (conn.getResponseCode() != 200) { |
| 68 | throw new RuntimeException("Failed : HTTP error code : " |
| 69 | + conn.getResponseCode()); |
| 70 | } |
| 71 | |
| 72 | conn.disconnect(); |
| 73 | |
| 74 | } catch (MalformedURLException e) { |
| 75 | log.error("Malformed URL for GET request", e); |
| 76 | } catch (IOException e) { |
| 77 | log.warn("Couldn't connect remote REST server"); |
| 78 | } |
| 79 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 80 | |
| 81 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | public static void delete(String str) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 83 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | try { |
| 85 | URL url = new URL(str); |
| 86 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 87 | conn.setRequestMethod("DELETE"); |
| 88 | conn.setRequestProperty("Accept", "application/json"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 89 | |
| 90 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | if (conn.getResponseCode() != 200) { |
| 92 | throw new RuntimeException("Failed : HTTP error code : " |
| 93 | + conn.getResponseCode()); |
| 94 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 95 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | conn.disconnect(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 97 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | } catch (MalformedURLException e) { |
| 99 | log.error("Malformed URL for GET request", e); |
| 100 | } catch (IOException e) { |
| 101 | log.warn("Couldn't connect remote REST server"); |
| 102 | } |
| 103 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 104 | } |