|
1 | 1 | package client;
|
2 | 2 |
|
3 |
| -import java.io.IOException; |
4 |
| -import java.util.List; |
5 |
| - |
6 | 3 | import org.apache.log4j.Logger;
|
7 | 4 |
|
8 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
9 |
| -import com.mashape.unirest.http.HttpResponse; |
10 |
| -import com.mashape.unirest.http.Unirest; |
11 | 5 | import com.mashape.unirest.http.exceptions.UnirestException;
|
12 | 6 |
|
13 |
| -import model.JSONable; |
| 7 | +import exceptions.FBaseEncryptionException; |
14 | 8 | import model.data.DataIdentifier;
|
15 | 9 | import model.data.DataRecord;
|
16 | 10 | import model.data.KeygroupID;
|
17 |
| -import model.messages.Message; |
18 | 11 |
|
19 | 12 | /**
|
20 | 13 | * A client implemantation which allows the usage of the FBase rest interface
|
|
25 | 18 | public class Client {
|
26 | 19 |
|
27 | 20 | private static Logger logger = Logger.getLogger(Client.class.getName());
|
28 |
| - |
29 |
| - public static void main(String[] args) throws UnirestException { |
30 |
| - Client c = new Client(); |
31 |
| - DataRecord record = new DataRecord(); |
32 |
| - DataIdentifier dataIdentifier = new DataIdentifier("smartlight", "h1", "brightness", "M-1"); |
33 |
| - record.setDataIdentifier(new DataIdentifier(dataIdentifier.getKeygroupID(), "M-4")); |
34 |
| - Message m = c.runPutRecordRequest("http://localhost", 8080, record); |
35 |
| - logger.info(JSONable.toJSON(m)); |
36 |
| - |
37 |
| - DataRecord record2 = c.runGetRecordRequest("http://localhost", 8080, dataIdentifier); |
38 |
| - logger.info(JSONable.toJSON(record2)); |
39 |
| - |
40 |
| - List<String> list = c.runGetListRecordRequest("http://localhost", 8080, |
41 |
| - dataIdentifier.getKeygroupID()); |
42 |
| - list.stream().forEach(i -> logger.info(i)); |
| 21 | + |
| 22 | + public void getScenario(String address, int port) throws UnirestException { |
| 23 | + KeygroupID keygroupID = new KeygroupID("smartlight", "h1", "brightness"); |
| 24 | + DataIdentifier dataID = new DataIdentifier(keygroupID, "M-1"); |
| 25 | + |
| 26 | + DataRecord newRecord = new DataRecord(new DataIdentifier(keygroupID, "M-2"), null); |
| 27 | + newRecord.setValueWithoutKey("Example value"); |
| 28 | + |
| 29 | + RecordRequest records = new RecordRequest(address, port); |
| 30 | + |
| 31 | + logger.info("Got M-1: " + records.getDataRecord(dataID)); |
| 32 | + logger.info("Put M-2: " + records.putDataRecord(newRecord)); |
| 33 | + logger.info("Got M-2: " + records.getDataRecord(newRecord.getDataIdentifier())); |
| 34 | + logger.info("List: " + records.listDataRecords(keygroupID)); |
| 35 | + logger.info("Deleted M-2: " + records.deleteDataRecord(newRecord.getDataIdentifier())); |
| 36 | + logger.info("Got M-2: " + records.getDataRecord(newRecord.getDataIdentifier())); |
| 37 | + logger.info("List: " + records.listDataRecords(keygroupID)); |
43 | 38 | }
|
44 |
| - |
45 |
| - public Client() { |
| 39 | + |
| 40 | + public static void main(String[] args) throws UnirestException, FBaseEncryptionException { |
| 41 | + String address = "localhost"; |
| 42 | + int port = 8081; |
| 43 | + |
| 44 | + Client c = new Client(); |
| 45 | + c.getScenario(address, port); |
46 | 46 | }
|
47 | 47 |
|
48 |
| - public DataRecord runGetRecordRequest(String address, int port, DataIdentifier dataIdentifier) |
49 |
| - throws UnirestException { |
50 |
| - DataRecord record = null; |
51 |
| - try { |
52 |
| - String target = address + ":" + port + "/record"; |
53 |
| - logger.info("Running get request targeting " + target); |
54 |
| - HttpResponse<String> response = Unirest.get(target) |
55 |
| - .queryString("dataIdentifier", dataIdentifier).asString(); |
56 |
| - if (response.getStatus() == 200) { |
57 |
| - logger.info("Status = 200"); |
58 |
| - Message m = JSONable.fromJSON(response.getBody(), Message.class); |
59 |
| - // Insert decryption here if needed |
60 |
| - record = JSONable.fromJSON(m.getContent(), DataRecord.class); |
61 |
| - } else { |
62 |
| - logger.info("Status = " + response.getStatus()); |
63 |
| - } |
64 |
| - } catch (UnirestException e) { |
65 |
| - e.printStackTrace(); |
66 |
| - } |
67 | 48 |
|
68 |
| - return record; |
69 |
| - } |
70 | 49 |
|
71 |
| - @SuppressWarnings("unchecked") |
72 |
| - public List<String> runGetListRecordRequest(String address, int port, KeygroupID keygroupID) { |
73 |
| - List<String> list = null; |
74 |
| - try { |
75 |
| - String target = address + ":" + port + "/record/list"; |
76 |
| - logger.info("Running get request targeting " + target); |
77 |
| - HttpResponse<String> response = Unirest.get(target) |
78 |
| - .queryString("keygroupID", keygroupID).asString(); |
79 |
| - if (response.getStatus() == 200) { |
80 |
| - logger.info("Status = 200"); |
81 |
| - Message m = JSONable.fromJSON(response.getBody(), Message.class); |
82 |
| - // Insert decryption here if needed |
83 |
| - ObjectMapper mapper = new ObjectMapper(); |
84 |
| - list = mapper.readValue(m.getContent(), List.class); |
85 |
| - } else { |
86 |
| - logger.info("Status = " + response.getStatus()); |
87 |
| - } |
88 |
| - } catch (UnirestException | IOException e) { |
89 |
| - e.printStackTrace(); |
90 |
| - } |
91 |
| - return list; |
92 |
| - } |
| 50 | +// public boolean keygroupConfig_create(String address, int port, KeygroupConfig keygroupConfig) |
| 51 | +// throws UnirestException, FBaseEncryptionException { |
| 52 | +// |
| 53 | +// // prepare |
| 54 | +// String target = address + ":" + port + "/keygroupConfig"; |
| 55 | +// logger.info("Running post request targeting " + target); |
| 56 | +// |
| 57 | +// // create and sign request message |
| 58 | +// Message requestM = new Message(); |
| 59 | +// requestM.setContent(JSONable.toJSON(keygroupConfig)); |
| 60 | +// requestM.signMessage(privateKey, algorithm); |
| 61 | +// |
| 62 | +// // send message |
| 63 | +// HttpResponse<String> response = Unirest.post(target).header("accept", "application/json") |
| 64 | +// .queryString("clientID", clientID.getID()).body(JSONable.toJSON(requestM)) |
| 65 | +// .asString(); |
| 66 | +// |
| 67 | +// // process response |
| 68 | +// if (response.getStatus() == 200) { |
| 69 | +// return true; |
| 70 | +// } else { |
| 71 | +// logger.error("Status = " + response.getStatus()); |
| 72 | +// return false; |
| 73 | +// } |
| 74 | +// } |
93 | 75 |
|
94 |
| - public Message runPutRecordRequest(String address, int port, DataRecord record) { |
95 |
| - Message m = null; |
96 |
| - try { |
97 |
| - String target = address + ":" + port + "/record"; |
98 |
| - logger.info("Running put request targeting " + target); |
99 |
| - HttpResponse<String> response = Unirest.put(target).header("accept", "application/json") |
100 |
| - // insert decryption here if needed |
101 |
| - .queryString("keygroupID", record.getKeygroupID().getID()).body(JSONable.toJSON(record)) |
102 |
| - .asString(); |
103 |
| - if (response.getStatus() == 200) { |
104 |
| - m = JSONable.fromJSON(response.getBody(), Message.class); |
105 |
| - } else { |
106 |
| - logger.info("Status = " + response.getStatus()); |
107 |
| - } |
108 |
| - } catch (UnirestException e) { |
109 |
| - e.printStackTrace(); |
110 |
| - } |
111 |
| - return m; |
112 |
| - } |
113 |
| - |
114 |
| - public Message runDeleteRecordRequest(String address, int port, DataIdentifier identifier) { |
115 |
| - Message m = null; |
116 |
| - try { |
117 |
| - String target = address + ":" + port + "/record"; |
118 |
| - logger.info("Running delete request targeting " + target); |
119 |
| - HttpResponse<String> response = Unirest.delete(target) |
120 |
| - .header("accept", "application/json") |
121 |
| - .queryString("keygroupID", identifier.getKeygroupID().getID()) |
122 |
| - .body(JSONable.toJSON(identifier)) // insert encryption here if needed |
123 |
| - .asString(); |
124 |
| - if (response.getStatus() == 200) { |
125 |
| - m = JSONable.fromJSON(response.getBody(), Message.class); |
126 |
| - } else { |
127 |
| - logger.info("Status = " + response.getStatus()); |
128 |
| - } |
129 |
| - } catch (UnirestException e) { |
130 |
| - e.printStackTrace(); |
131 |
| - } |
132 |
| - return m; |
133 |
| - } |
134 |
| - |
135 | 76 | }
|
0 commit comments