HIGUCHI Yuta | ea60e5f | 2013-06-12 11:10:21 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.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 { |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -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 | |
| 18 | public static String get(String str) { |
| 19 | StringBuilder response = new StringBuilder(); |
| 20 | |
| 21 | try { |
| 22 | |
| 23 | URL url = new URL(str); |
| 24 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 25 | conn.setConnectTimeout(2 * 1000); //2 seconds |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 26 | conn.setRequestMethod("GET"); |
| 27 | conn.setRequestProperty("Accept", "application/json"); |
| 28 | |
| 29 | if (conn.getResponseCode() != 200) { |
| 30 | throw new RuntimeException("Failed : HTTP error code : " |
| 31 | + conn.getResponseCode()); |
| 32 | } |
| 33 | |
| 34 | if (!conn.getContentType().equals("application/json")){ |
| 35 | log.warn("The content received from {} is not json", str); |
| 36 | } |
| 37 | |
Naoki Shiota | 8c3a3b6 | 2014-01-23 10:24:41 -0800 | [diff] [blame^] | 38 | BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 39 | String line; |
| 40 | while ((line = br.readLine()) != null) { |
| 41 | response.append(line); |
| 42 | } |
| 43 | |
| 44 | br.close(); |
| 45 | conn.disconnect(); |
| 46 | |
| 47 | } catch (MalformedURLException e) { |
| 48 | log.error("Malformed URL for GET request", e); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 49 | } catch (ConnectTimeoutException e) { |
| 50 | log.warn("Couldn't connect remote REST server"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 51 | } catch (IOException e) { |
| 52 | log.warn("Couldn't connect remote REST server"); |
| 53 | } |
| 54 | |
| 55 | return response.toString(); |
| 56 | } |
| 57 | |
| 58 | public static void post (String str) { |
| 59 | |
| 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 | } |
| 80 | |
| 81 | |
| 82 | public static void delete (String str) { |
| 83 | |
| 84 | try { |
| 85 | URL url = new URL(str); |
| 86 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 87 | conn.setRequestMethod("DELETE"); |
| 88 | conn.setRequestProperty("Accept", "application/json"); |
| 89 | |
| 90 | |
| 91 | if (conn.getResponseCode() != 200) { |
| 92 | throw new RuntimeException("Failed : HTTP error code : " |
| 93 | + conn.getResponseCode()); |
| 94 | } |
| 95 | |
| 96 | conn.disconnect(); |
| 97 | |
| 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 | } |
| 104 | } |