blob: 5ea4699c958dae9f5a6802168b665217aef978a9 [file] [log] [blame]
Jonathan Hart382623d2014-04-03 09:48:11 -07001package net.onrc.onos.apps.bgproute;
Jonathan Hart61ba9372013-05-19 20:10:29 -07002
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.net.HttpURLConnection;
7import java.net.MalformedURLException;
8import java.net.URL;
9
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070010import org.apache.commons.httpclient.ConnectTimeoutException;
Jonathan Hart61ba9372013-05-19 20:10:29 -070011import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
14
15public class RestClient {
Ray Milkey269ffb92014-04-03 14:43:30 -070016 protected final static Logger log = LoggerFactory.getLogger(RestClient.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070017
Ray Milkey269ffb92014-04-03 14:43:30 -070018 public static String get(String str) {
19 StringBuilder response = new StringBuilder();
Jonathan Hart61ba9372013-05-19 20:10:29 -070020
Ray Milkey269ffb92014-04-03 14:43:30 -070021 try {
Jonathan Hart61ba9372013-05-19 20:10:29 -070022
Ray Milkey269ffb92014-04-03 14:43:30 -070023 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 Hart61ba9372013-05-19 20:10:29 -070028
Ray Milkey269ffb92014-04-03 14:43:30 -070029 if (conn.getResponseCode() != 200) {
30 throw new RuntimeException("Failed : HTTP error code : "
31 + conn.getResponseCode());
32 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070033
Ray Milkey269ffb92014-04-03 14:43:30 -070034 if (!conn.getContentType().equals("application/json")) {
35 log.warn("The content received from {} is not json", str);
36 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070037
Ray Milkey269ffb92014-04-03 14:43:30 -070038 BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
39 String line;
40 while ((line = br.readLine()) != null) {
41 response.append(line);
42 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 br.close();
45 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -070046
Ray Milkey269ffb92014-04-03 14:43:30 -070047 } 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 Hart61ba9372013-05-19 20:10:29 -070054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 return response.toString();
56 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070057
Ray Milkey269ffb92014-04-03 14:43:30 -070058 public static void post(String str) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 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 Hart61ba9372013-05-19 20:10:29 -070080
81
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public static void delete(String str) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 try {
85 URL url = new URL(str);
86 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
87 conn.setRequestMethod("DELETE");
88 conn.setRequestProperty("Accept", "application/json");
Jonathan Hart61ba9372013-05-19 20:10:29 -070089
90
Ray Milkey269ffb92014-04-03 14:43:30 -070091 if (conn.getResponseCode() != 200) {
92 throw new RuntimeException("Failed : HTTP error code : "
93 + conn.getResponseCode());
94 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 conn.disconnect();
Jonathan Hart61ba9372013-05-19 20:10:29 -070097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 } 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 Hart61ba9372013-05-19 20:10:29 -0700104}