d94d575bf4831430212e2008a7d70337ba8326ee
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / examples / HueJavaSampleBundle / hue / src / main / java / org / iotivity / bundle / hue / HueConnector.java
1 package org.iotivity.bundle.hue;
2
3 import java.io.IOException;
4
5 import org.apache.http.HttpEntity;
6 import org.apache.http.client.ClientProtocolException;
7 import org.apache.http.client.methods.CloseableHttpResponse;
8 import org.apache.http.client.methods.HttpPost;
9 import org.apache.http.entity.ContentType;
10 import org.apache.http.entity.StringEntity;
11 import org.apache.http.impl.client.CloseableHttpClient;
12 import org.apache.http.impl.client.HttpClients;
13 import org.apache.http.util.EntityUtils;
14 import org.iotivity.resourcecontainer.bundle.api.ProtocolBridgeConnector;
15
16 public class HueConnector implements ProtocolBridgeConnector {
17     public void connect() {
18
19     }
20
21     public void disconnect() {
22
23     }
24
25     public void transmit(String target, String payload) {
26         try {
27             CloseableHttpClient httpclient = HttpClients.createDefault();
28             HttpPost httpPost = new HttpPost(target);
29             // httpPost.setHeader("content-type","application/json");
30             StringEntity stringEntity = new StringEntity(payload,
31                     ContentType.create("application/json", "UTF-8"));
32             httpPost.setEntity(stringEntity);
33
34             CloseableHttpResponse response1;
35             response1 = httpclient.execute(httpPost);
36             // The underlying HTTP connection is still held by the response
37             // object
38             // to allow the response content to be streamed directly from the
39             // network socket.
40             // In order to ensure correct deallocation of system resources
41             // the user MUST call CloseableHttpResponse#close() from a finally
42             // clause.
43             // Please note that if response content is not fully consumed the
44             // underlying
45             // connection cannot be safely re-used and will be shut down and
46             // discarded
47             // by the connection manager.
48             try {
49                 HttpEntity entity1 = response1.getEntity();
50                 // do something useful with the response body
51                 // and ensure it is fully consumed
52                 EntityUtils.consume(entity1);
53             } finally {
54                 response1.close();
55             }
56
57         } catch (ClientProtocolException e) {
58             // TODO Auto-generated catch block
59             e.printStackTrace();
60         } catch (IOException e) {
61             // TODO Auto-generated catch block
62             e.printStackTrace();
63         }
64     }
65
66     public String read(String target) {
67         return "";
68     }
69
70 }