Commit 1f0f3542 authored by Riccardo Presotto's avatar Riccardo Presotto

aggiunto codice client

parent 91165ef3
File added
......@@ -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'
......
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
File added
......@@ -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();
......
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;
}
}
}
......@@ -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<User> 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<User> usersCopy = getUserslist();
for(User u: usersCopy)
if(u.getName().toLowerCase().equals(name.toLowerCase()))
return u;
......
......@@ -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")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment