Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/iot2
[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 final 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          * openConnection() method\r
55          */\r
56         public void openConnection() throws IOException {\r
57 \r
58                 httpConnection = (HttpURLConnection) url.openConnection();\r
59 \r
60         }\r
61 \r
62         /**\r
63          * setDoInput() method inherited from HttpURLConnection class\r
64          *\r
65          * @param  bSetDoInput\r
66          * @return void\r
67          */\r
68         public void setDoInput(boolean bSetDoInput) {\r
69 \r
70                 httpConnection.setDoInput(bSetDoInput);\r
71 \r
72         }\r
73 \r
74         /**\r
75          * setRequestProperty() method inherited from HttpURLConnection class\r
76          *\r
77          * @param  strProperty             String property\r
78          * @param  strHttpAuthCredentials  String HTTP authentication credentials\r
79          * @return void\r
80          */\r
81         public void setRequestProperty(String strProperty, String strHttpAuthCredentials) {\r
82 \r
83                 httpConnection.setRequestProperty(strProperty, strHttpAuthCredentials);\r
84 \r
85         }\r
86 \r
87         /**\r
88          * setRequestMethod() method inherited from HttpURLConnection class\r
89          *\r
90          * @param  strMethod             String method\r
91          * @return void\r
92          */\r
93         public void setRequestMethod(String strMethod) throws ProtocolException {\r
94 \r
95                 httpConnection.setRequestMethod(strMethod);\r
96 \r
97         }\r
98 \r
99         /**\r
100          * setDoOutput() method inherited from HttpURLConnection class\r
101          *\r
102          * @param  doOut\r
103          * @return void\r
104          */\r
105         public void setDoOutput(boolean doOut) {\r
106 \r
107                 httpConnection.setDoOutput(doOut);\r
108 \r
109         }\r
110 \r
111         /**\r
112          * getOutputStream() method inherited from HttpURLConnection class\r
113          *\r
114          * @return OutputStream\r
115          */\r
116         public OutputStream getOutputStream() throws IOException {\r
117 \r
118                 return httpConnection.getOutputStream();\r
119 \r
120         }\r
121 \r
122         /**\r
123          * getInputStream() method inherited from HttpURLConnection class\r
124          *\r
125          * @return InputStream\r
126          */\r
127         public InputStream getInputStream() throws IOException {\r
128 \r
129                 return httpConnection.getInputStream();\r
130 \r
131         }\r
132 \r
133         /**\r
134          * connect() method inherited from HttpURLConnection class\r
135          */\r
136         public void connect() throws IOException {\r
137 \r
138                 httpConnection.connect();\r
139 \r
140         }\r
141 \r
142         /**\r
143          * disconnect() method inherited from HttpURLConnection class\r
144          */\r
145         public void disconnect() throws IOException {\r
146 \r
147                 httpConnection.disconnect();\r
148 \r
149         }\r
150 }\r