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