5d9d2b7d743ad87cc1e494d676d28cf22f9a0363
[iotcloud.git] / version2 / src / java / light_fan_benchmark / IoTHTTP.java
1 \r
2 // Java packages\r
3 import java.io.InputStream;\r
4 import java.io.OutputStream;\r
5 import java.io.IOException;\r
6 import java.net.HttpURLConnection;\r
7 import java.net.MalformedURLException;\r
8 import java.net.UnknownHostException;\r
9 import java.net.URL;\r
10 import java.net.ProtocolException;\r
11 \r
12 \r
13 /** Class IoTHTTP is a wrapper class that provides\r
14  *  minimum interfaces for user to interact with IoT\r
15  *  devices in our system\r
16  *\r
17  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>\r
18  * @version     1.0\r
19  * @since       2016-02-18\r
20  */\r
21 public class IoTHTTP {\r
22 \r
23         /**\r
24          * IoTHTTP class properties\r
25          */\r
26         private IoTDeviceAddress iotDevAdd;\r
27         private URL url;\r
28         private HttpURLConnection httpConnection;\r
29 \r
30         /**\r
31          * Class constructor\r
32          */\r
33         public IoTHTTP(IoTDeviceAddress _iotDevAdd) {\r
34 \r
35                 iotDevAdd = _iotDevAdd;\r
36                 url = null;\r
37                 httpConnection = null;\r
38         }\r
39 \r
40         /**\r
41          * setURL() method\r
42          *\r
43          * @param  strUrlComplete  String to complete the URL\r
44          * @return void\r
45          */\r
46         public void setURL(String strUrlComplete) throws MalformedURLException {\r
47 \r
48                 url = new URL(iotDevAdd.getURL(strUrlComplete));\r
49 \r
50         }\r
51 \r
52         /**\r
53          * openConnection() method\r
54          */\r
55         public void openConnection() throws IOException {\r
56 \r
57                 httpConnection = (HttpURLConnection) url.openConnection();\r
58 \r
59         }\r
60 \r
61         /**\r
62          * setDoInput() method inherited from HttpURLConnection class\r
63          *\r
64          * @param  bSetDoInput\r
65          * @return void\r
66          */\r
67         public void setDoInput(boolean bSetDoInput) {\r
68 \r
69                 httpConnection.setDoInput(bSetDoInput);\r
70 \r
71         }\r
72 \r
73         /**\r
74          * setRequestProperty() method inherited from HttpURLConnection class\r
75          *\r
76          * @param  strProperty             String property\r
77          * @param  strHttpAuthCredentials  String HTTP authentication credentials\r
78          * @return void\r
79          */\r
80         public void setRequestProperty(String strProperty, String strHttpAuthCredentials) {\r
81 \r
82                 httpConnection.setRequestProperty(strProperty, strHttpAuthCredentials);\r
83 \r
84         }\r
85 \r
86         /**\r
87          * setRequestMethod() method inherited from HttpURLConnection class\r
88          *\r
89          * @param  strMethod             String method\r
90          * @return void\r
91          */\r
92         public void setRequestMethod(String strMethod) throws ProtocolException {\r
93 \r
94                 httpConnection.setRequestMethod(strMethod);\r
95 \r
96         }\r
97 \r
98         /**\r
99          * setDoOutput() method inherited from HttpURLConnection class\r
100          *\r
101          * @param  doOut\r
102          * @return void\r
103          */\r
104         public void setDoOutput(boolean doOut) {\r
105 \r
106                 httpConnection.setDoOutput(doOut);\r
107 \r
108         }\r
109 \r
110         /**\r
111          * getOutputStream() method inherited from HttpURLConnection class\r
112          *\r
113          * @return OutputStream\r
114          */\r
115         public OutputStream getOutputStream() throws IOException {\r
116 \r
117                 return httpConnection.getOutputStream();\r
118 \r
119         }\r
120 \r
121         /**\r
122          * getInputStream() method inherited from HttpURLConnection class\r
123          *\r
124          * @return InputStream\r
125          */\r
126         public InputStream getInputStream() throws IOException {\r
127 \r
128                 return httpConnection.getInputStream();\r
129 \r
130         }\r
131 \r
132         /**\r
133          * connect() method inherited from HttpURLConnection class\r
134          */\r
135         public void connect() throws IOException {\r
136 \r
137                 httpConnection.connect();\r
138 \r
139         }\r
140 \r
141         /**\r
142          * disconnect() method inherited from HttpURLConnection class\r
143          */\r
144         public void disconnect() throws IOException {\r
145 \r
146                 httpConnection.disconnect();\r
147 \r
148         }\r
149 }\r