blob: a9f2abeec387eeb322b74de623036d8f26ff3c09 [file] [log] [blame]
HIGUCHI Yutaea60e5f2013-06-12 11:10:21 -07001package net.onrc.onos.ofcontroller.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 {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070016 protected final static Logger log = LoggerFactory.getLogger(RestClient.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070017
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 Harte7e1c6e2013-06-04 20:50:23 -070025 conn.setConnectTimeout(2 * 1000); //2 seconds
Jonathan Hart61ba9372013-05-19 20:10:29 -070026 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
38 BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
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 Harte7e1c6e2013-06-04 20:50:23 -070049 } catch (ConnectTimeoutException e) {
50 log.warn("Couldn't connect remote REST server");
Jonathan Hart61ba9372013-05-19 20:10:29 -070051 } 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}