diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c14cf8b3d1f4303c7a326ea373ab57e9efe7398d Binary files /dev/null and b/.DS_Store differ diff --git a/.gradle/6.7/executionHistory/executionHistory.bin b/.gradle/6.7/executionHistory/executionHistory.bin index cf68230edc9d9ba5be3c1c19c012d35607759a02..ac2d3bc50b0dad75e647014873dc6fa0b674c4e2 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.bin and b/.gradle/6.7/executionHistory/executionHistory.bin differ diff --git a/.gradle/6.7/executionHistory/executionHistory.lock b/.gradle/6.7/executionHistory/executionHistory.lock index 764860d31f678bd696584538cc55e5d9d0cc14bd..d387f07e90872a35034ceea4a994f7c79da9d03a 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.lock and b/.gradle/6.7/executionHistory/executionHistory.lock differ diff --git a/.gradle/6.7/fileHashes/fileHashes.bin b/.gradle/6.7/fileHashes/fileHashes.bin index 59d8ae85875a05cff86a7f1941d9eb44d3201092..c31dd20d79ba3b7349e5f7bff0935b95b63ed041 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.bin and b/.gradle/6.7/fileHashes/fileHashes.bin differ diff --git a/.gradle/6.7/fileHashes/fileHashes.lock b/.gradle/6.7/fileHashes/fileHashes.lock index 5364a2b05eb42472f8ccd1c502f208f85ef02f15..e302a23ebd2a5f0f8ccf7579a32cbbb1fc73eb7f 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.lock and b/.gradle/6.7/fileHashes/fileHashes.lock differ diff --git a/.gradle/6.7/javaCompile/classAnalysis.bin b/.gradle/6.7/javaCompile/classAnalysis.bin index 171f0c01e68dccad4143e6a46836bd4862ac1fb1..93a33dfedace3a96436180822ec7eebcf00a1ff0 100644 Binary files a/.gradle/6.7/javaCompile/classAnalysis.bin and b/.gradle/6.7/javaCompile/classAnalysis.bin differ diff --git a/.gradle/6.7/javaCompile/javaCompile.lock b/.gradle/6.7/javaCompile/javaCompile.lock index 0042925bd1410ce2bde3e31d8f2ae0a941cbd577..549dd2246fe6813a0f23935d9fd93d981b6cd73a 100644 Binary files a/.gradle/6.7/javaCompile/javaCompile.lock and b/.gradle/6.7/javaCompile/javaCompile.lock differ diff --git a/.gradle/6.7/javaCompile/taskHistory.bin b/.gradle/6.7/javaCompile/taskHistory.bin index 0986f1f244f0639fbe4473a070a336d8043bbafd..61a815658122a919a671427ba2e3ac51cd8a8c76 100644 Binary files a/.gradle/6.7/javaCompile/taskHistory.bin and b/.gradle/6.7/javaCompile/taskHistory.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 5bed8506330eb7a9ea3cd5707c9ce1e82bf489e5..dac388f67820b873c1d450aa9ccbdc863f371b2e 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/build.gradle b/build.gradle index d1ce39d914bb5139e5dfc10a6684036e11b37ace..3bd8f5a8bc2d7f90c1f7931a5b2c668b8ce893e5 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,8 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' + compile 'com.google.code.gson:gson:2.7' + // REST Dependencies // https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.2' diff --git a/build/classes/java/main/MQTT/PubExample.class b/build/classes/java/main/MQTT/PubExample.class index 096af4d968d638287e8d55194f6dea477fa9c366..88733c3960417d475fa4a5f77a4c092a838ee392 100644 Binary files a/build/classes/java/main/MQTT/PubExample.class and b/build/classes/java/main/MQTT/PubExample.class differ diff --git a/build/classes/java/main/MQTT/PubSubExample$1.class b/build/classes/java/main/MQTT/PubSubExample$1.class index 4e867400b577c341026242f07ad2e876c516727a..cd083e996b3b4931ae491c9b661bb03e690e25a8 100644 Binary files a/build/classes/java/main/MQTT/PubSubExample$1.class and b/build/classes/java/main/MQTT/PubSubExample$1.class differ diff --git a/build/classes/java/main/MQTT/PubSubExample.class b/build/classes/java/main/MQTT/PubSubExample.class index 8658736e28e1f6ef4c6c3aaaff7b4894adc62222..ed877e0412025feb276a0b649845d90d8464571b 100644 Binary files a/build/classes/java/main/MQTT/PubSubExample.class and b/build/classes/java/main/MQTT/PubSubExample.class differ diff --git a/build/classes/java/main/REST/Client/ClientExample.class b/build/classes/java/main/REST/Client/ClientExample.class new file mode 100644 index 0000000000000000000000000000000000000000..63d0e0cb78d2a0765cb6305d9f93f0e4b04bb34a Binary files /dev/null and b/build/classes/java/main/REST/Client/ClientExample.class differ diff --git a/build/classes/java/main/REST/beans/Users.class b/build/classes/java/main/REST/beans/Users.class index 716a10ac95f02a1fe468dd0009cc1df8e354c6d8..71577ee673a4dbfea5f516fd0be6449ab23415dd 100644 Binary files a/build/classes/java/main/REST/beans/Users.class and b/build/classes/java/main/REST/beans/Users.class differ diff --git a/build/classes/java/main/REST/services/UsersService.class b/build/classes/java/main/REST/services/UsersService.class index 57372452ad74121755537da309603cb23f4d3f5f..ea2b6a19144f2f579cc31e8740ec91a3a1efd00c 100644 Binary files a/build/classes/java/main/REST/services/UsersService.class and b/build/classes/java/main/REST/services/UsersService.class differ diff --git a/build/tmp/compileJava/source-classes-mapping.txt b/build/tmp/compileJava/source-classes-mapping.txt index 45d582092147ca00217b93fa0d93b5ec7eb87316..7a5f4250208e69503858349e8deaadb8b2acdaab 100644 --- a/build/tmp/compileJava/source-classes-mapping.txt +++ b/build/tmp/compileJava/source-classes-mapping.txt @@ -1,21 +1,23 @@ +MQTT/SubExample.java + MQTT.SubExample + MQTT.SubExample$1 REST/beans/User.java REST.beans.User +REST/services/HelloWorld.java + REST.services.HelloWorld MQTT/PubSubExample.java MQTT.PubSubExample MQTT.PubSubExample$1 -REST/beans/Users.java - REST.beans.Users REST/StartServer.java REST.StartServer MQTT/PubExampleCallback.java MQTT.PubExampleCallback MQTT.PubExampleCallback$1 -REST/services/HelloWorld.java - REST.services.HelloWorld -REST/services/UsersService.java - REST.services.UsersService MQTT/PubExample.java MQTT.PubExample -MQTT/SubExample.java - MQTT.SubExample - MQTT.SubExample$1 +REST/services/UsersService.java + REST.services.UsersService +REST/beans/Users.java + REST.beans.Users +REST/Client/ClientExample.java + REST.Client.ClientExample diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f277d49cee3910a51f5757e553cdcdd40c66768a Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4fcdafec7ce672fc04976de4feaaffdb9e9f09b7 Binary files /dev/null and b/src/main/.DS_Store differ diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..33179ed1403b823ec452480b3ad82fe01a52c389 Binary files /dev/null and b/src/main/java/.DS_Store differ diff --git a/src/main/java/MQTT/PubExample.java b/src/main/java/MQTT/PubExample.java index 666b229878ebdc29bcbdccc7b4907731c4260980..502e76b927b4f57527993625c627296efa8b41d1 100644 --- a/src/main/java/MQTT/PubExample.java +++ b/src/main/java/MQTT/PubExample.java @@ -10,6 +10,8 @@ public class PubExample { String topic = "home/sensors/light"; int qos = 2; + //brew services start mosquitto + try { client = new MqttClient(broker, clientId); MqttConnectOptions connOpts = new MqttConnectOptions(); diff --git a/src/main/java/REST/.DS_Store b/src/main/java/REST/.DS_Store index 9fdb7fb8f988971f518e46ca0193ff093f0bf48e..5380d49adec926c0b330a28dbb91a6918692a494 100644 Binary files a/src/main/java/REST/.DS_Store and b/src/main/java/REST/.DS_Store differ diff --git a/src/main/java/REST/Client/ClientExample.java b/src/main/java/REST/Client/ClientExample.java new file mode 100644 index 0000000000000000000000000000000000000000..5679144420a8015086dae7726515a2c362535772 --- /dev/null +++ b/src/main/java/REST/Client/ClientExample.java @@ -0,0 +1,63 @@ +package REST.Client; + +import REST.beans.User; +import REST.beans.Users; +import com.google.gson.Gson; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientHandlerException; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; + +public class ClientExample { + + public static void main(String args[]){ + Client client = Client.create(); + String serverAddress = "http://localhost:1337"; + ClientResponse clientResponse = null; + + // POST EXAMPLE + String postPath = "/users/add"; + User user = new User("riccardo","presotto"); + clientResponse = postRequest(client,serverAddress+postPath,user); + System.out.println(clientResponse.toString()); + + //GET REQUEST #1 + String getPath = "/users"; + clientResponse = getRequest(client,serverAddress+getPath); + System.out.println(clientResponse.toString()); + Users users = clientResponse.getEntity(Users.class); + System.out.println("Users List"); + for (User u : users.getUserslist()){ + System.out.println("Name: " + u.getName() + " Surname: " + u.getSurname()); + } + + //GET REQUEST #2 + String getUserPath = "/users/get/riccardo"; + clientResponse = getRequest(client,serverAddress+getUserPath); + System.out.println(clientResponse.toString()); + User userResponse = clientResponse.getEntity(User.class); + System.out.println("Name: " + userResponse.getName() + " Surname: " + userResponse.getSurname()); + + } + + public static ClientResponse postRequest(Client client, String url, User u){ + WebResource webResource = client.resource(url); + String input = new Gson().toJson(u); + try { + return webResource.type("application/json").post(ClientResponse.class, input); + } catch (ClientHandlerException e) { + System.out.println("Server non disponibile"); + return null; + } + } + + public static ClientResponse getRequest(Client client, String url){ + WebResource webResource = client.resource(url); + try { + return webResource.type("application/json").get(ClientResponse.class); + } catch (ClientHandlerException e) { + System.out.println("Server non disponibile"); + return null; + } + } +} diff --git a/src/main/java/REST/beans/Users.java b/src/main/java/REST/beans/Users.java index 6ec3ad8c83cd5bd91af76dc46797da51bdf220bc..59f43b4cd8b974c0149927f4e13342c9e2d5bb64 100644 --- a/src/main/java/REST/beans/Users.java +++ b/src/main/java/REST/beans/Users.java @@ -14,7 +14,7 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType (XmlAccessType.FIELD) public class Users { - @XmlElement(name="user") + @XmlElement(name="my_users") private List userslist; private static Users instance; @@ -41,15 +41,12 @@ public class Users { } public synchronized void add(User u){ - userslist.add(u); - } - public User getByName(String name){ + public User getByName(String name){ List usersCopy = getUserslist(); - for(User u: usersCopy) if(u.getName().toLowerCase().equals(name.toLowerCase())) return u; diff --git a/src/main/java/REST/services/UsersService.java b/src/main/java/REST/services/UsersService.java index 89c24e29c82dc2e40805002272d26c02391c69fb..bf19bb8f30ea543b05edb08ae91f10a7bb968cba 100644 --- a/src/main/java/REST/services/UsersService.java +++ b/src/main/java/REST/services/UsersService.java @@ -4,12 +4,7 @@ package REST.services; import REST.beans.User; import REST.beans.Users; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; +import javax.ws.rs.*; import javax.ws.rs.core.Response; @Path("users")