2019-10-06 10:45:19 +02:00
|
|
|
package envoy.client;
|
|
|
|
|
2019-11-27 17:07:25 +01:00
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2019-10-06 10:45:19 +02:00
|
|
|
import javax.ws.rs.client.ClientBuilder;
|
|
|
|
import javax.ws.rs.client.Entity;
|
|
|
|
import javax.ws.rs.client.WebTarget;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
import javax.xml.bind.JAXBException;
|
|
|
|
import javax.xml.bind.Marshaller;
|
2019-10-07 16:14:14 +02:00
|
|
|
|
2019-10-06 10:45:19 +02:00
|
|
|
import envoy.schema.ObjectFactory;
|
2019-11-04 23:10:53 +01:00
|
|
|
import envoy.schema.Sync;
|
2019-10-12 14:45:58 +02:00
|
|
|
import envoy.schema.User;
|
2019-10-06 10:45:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
2019-10-12 11:19:29 +02:00
|
|
|
* File: <strong>Client.java</strong><br>
|
2019-10-06 10:45:19 +02:00
|
|
|
* Created: <strong>28 Sep 2019</strong><br>
|
2019-10-12 08:19:00 +02:00
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @author Maximilian Käfer
|
2019-10-12 14:45:58 +02:00
|
|
|
* @author Leon Hofmeister
|
2019-10-31 22:11:50 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
2019-10-06 10:45:19 +02:00
|
|
|
*/
|
2019-10-12 11:19:29 +02:00
|
|
|
public class Client {
|
2019-10-06 10:45:19 +02:00
|
|
|
|
|
|
|
private ObjectFactory objectFactory = new ObjectFactory();
|
2019-10-12 11:19:29 +02:00
|
|
|
private Config config;
|
2019-10-19 12:10:52 +02:00
|
|
|
private User sender, recipient;
|
2019-10-06 10:45:19 +02:00
|
|
|
|
2019-11-27 17:07:25 +01:00
|
|
|
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
|
|
|
|
|
2019-10-12 14:45:58 +02:00
|
|
|
public Client(Config config, String username) {
|
2019-11-09 13:25:18 +01:00
|
|
|
this.config = config;
|
|
|
|
sender = getUser(username);
|
2019-11-27 17:07:25 +01:00
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
logger.info("ID: " + sender.getID());
|
2019-10-06 10:45:19 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 13:25:18 +01:00
|
|
|
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
|
2019-12-07 10:44:25 +01:00
|
|
|
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
|
|
|
WebTarget target = client.target(uri);
|
|
|
|
Response response = target.request().post(Entity.entity(body, "application/xml"));
|
|
|
|
R responseBody = response.readEntity(responseBodyClass);
|
2019-11-09 13:25:18 +01:00
|
|
|
response.close();
|
|
|
|
client.close();
|
2019-11-04 23:10:53 +01:00
|
|
|
|
2019-11-09 13:25:18 +01:00
|
|
|
return responseBody;
|
2019-11-09 17:23:15 +01:00
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
}
|
2019-11-09 13:25:18 +01:00
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
/**
|
|
|
|
* Returns a {@link Sync} with all users on the server.
|
2019-11-09 13:25:18 +01:00
|
|
|
*
|
2019-11-04 23:10:53 +01:00
|
|
|
* @return Sync - List of all users on the server.
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
public Sync getUsersListXml() {
|
|
|
|
Sync sendSync = objectFactory.createSync();
|
|
|
|
User user = objectFactory.createUser();
|
|
|
|
user.setID(-1);
|
|
|
|
sendSync.getUsers().add(user);
|
|
|
|
|
2019-11-09 13:25:18 +01:00
|
|
|
Sync returnSendSync = post(
|
|
|
|
String
|
|
|
|
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
|
|
|
sendSync,
|
|
|
|
Sync.class);
|
2019-11-04 23:10:53 +01:00
|
|
|
return returnSendSync;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-31 22:11:50 +01:00
|
|
|
* Returns a {@link User} with a specific id by name.
|
2019-11-04 23:10:53 +01:00
|
|
|
*
|
|
|
|
* @param name - the name of the {@link User}
|
|
|
|
* @return a {@link User} with the specified name
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
private User getUser(String name) {
|
|
|
|
Sync senderSync = objectFactory.createSync();
|
|
|
|
User user = objectFactory.createUser();
|
|
|
|
user.setName(name);
|
|
|
|
senderSync.getUsers().add(user);
|
|
|
|
|
2019-11-09 13:25:18 +01:00
|
|
|
Sync returnSenderSync = post(
|
|
|
|
String
|
|
|
|
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
|
|
|
senderSync,
|
|
|
|
Sync.class);
|
|
|
|
|
|
|
|
User returnSender = objectFactory.createUser();
|
2019-11-04 23:10:53 +01:00
|
|
|
|
|
|
|
if (returnSenderSync.getUsers().size() == 1) {
|
|
|
|
returnSender = returnSenderSync.getUsers().get(0);
|
|
|
|
} else {
|
2019-11-27 17:07:25 +01:00
|
|
|
logger.warning("ERROR exiting...");
|
2019-11-04 23:10:53 +01:00
|
|
|
}
|
2019-11-09 13:25:18 +01:00
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
return returnSender;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the "sync" Sync Object to the server and gets a "returnSync" Sync
|
|
|
|
* Object as response. <br>
|
2019-11-09 13:25:18 +01:00
|
|
|
* It is also used to get the own sender at the start of the client
|
|
|
|
* (Client sends "sync" Sync Object with single user in it(name: the name
|
|
|
|
* entered at login, id: 0, UserStatus:null))<br>
|
|
|
|
* and to get a complete list of all users saved on the server.
|
|
|
|
* (Client sends "sync" Sync Object with single user in it(name: "" (empty), id:
|
|
|
|
* -1, UserStatus:null)) <br>
|
2019-11-04 23:10:53 +01:00
|
|
|
* This method also processes the response Sync Object. <br>
|
|
|
|
* It sorts its users and messages by specific variables and does certain things
|
|
|
|
* with them. <br>
|
|
|
|
* <br>
|
|
|
|
* Messages: <br>
|
2019-11-09 13:25:18 +01:00
|
|
|
* -State SENT: Update Local message(s) with State WAITING (add Message ID and
|
|
|
|
* change State to SENT). (server sends these informations to the client if
|
|
|
|
* message(s) with State WAITING were successfully sent to the server)<br>
|
|
|
|
* -State RECEIVED, SenderID != 0: Adds the unread Messages returned from the
|
|
|
|
* server in the latest sync to the "unreadMessagesSync" Sync Object. <br>
|
|
|
|
* -State RECEIVED, SenderID == 0: Update message(s) in localDB to state
|
|
|
|
* RECEIVED.
|
|
|
|
* (server sends these informations to the client if the other client received
|
|
|
|
* the message(s).) <br>
|
|
|
|
* -State READ: Update message(s) in the LocalDB to state READ. (server sends
|
|
|
|
* these informations to the client if the other client read
|
|
|
|
* the message(s).) <br>
|
2019-11-04 23:10:53 +01:00
|
|
|
* <br>
|
|
|
|
* Users: <br>
|
2019-11-09 13:25:18 +01:00
|
|
|
* Updating UserStatus of all users in LocalDB. (Server sends all users with
|
|
|
|
* their updated UserStatus to the client.) <br>
|
2019-11-04 23:10:53 +01:00
|
|
|
*
|
2019-12-07 10:44:25 +01:00
|
|
|
* @param userId the id of the {@link Client} who sends the {@link Sync}
|
2019-11-22 22:40:00 +01:00
|
|
|
* @param sync the sync object (yet to be converted from java class to sync.xml)
|
|
|
|
* @return a returnSync.xml file
|
2019-11-04 23:10:53 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-11-09 13:25:18 +01:00
|
|
|
public Sync sendSync(long userId, Sync sync) {
|
|
|
|
// Print sync XML to console
|
|
|
|
JAXBContext jc;
|
|
|
|
try {
|
|
|
|
jc = JAXBContext.newInstance("envoy.schema");
|
|
|
|
Marshaller m = jc.createMarshaller();
|
|
|
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
|
|
|
m.marshal(sync, System.out);
|
|
|
|
} catch (JAXBException e) {
|
|
|
|
e.printStackTrace();
|
2019-11-04 23:10:53 +01:00
|
|
|
}
|
2019-10-26 18:31:49 +02:00
|
|
|
|
2019-11-09 13:25:18 +01:00
|
|
|
// Send sync
|
|
|
|
return post(String
|
|
|
|
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId),
|
|
|
|
sync,
|
|
|
|
Sync.class);
|
2019-10-19 15:09:32 +02:00
|
|
|
}
|
|
|
|
|
2019-10-12 17:35:58 +02:00
|
|
|
/**
|
2019-11-04 23:10:53 +01:00
|
|
|
* @return the sender object that represents this client.
|
2019-10-26 18:31:49 +02:00
|
|
|
* @since Envoy v0.1-alpha
|
2019-10-12 17:35:58 +02:00
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public User getSender() { return sender; }
|
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
/**
|
|
|
|
* @return the current recipient of the current chat.
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public User getRecipient() { return recipient; }
|
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
/**
|
2019-12-07 10:44:25 +01:00
|
|
|
* Sets the recipient.
|
|
|
|
*
|
2019-11-09 17:23:15 +01:00
|
|
|
* @param recipient - the recipient to set
|
2019-11-04 23:10:53 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public void setRecipient(User recipient) { this.recipient = recipient; }
|
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
/**
|
2019-10-31 22:11:50 +01:00
|
|
|
* @return true, if a recipient is selected
|
2019-11-04 23:10:53 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public boolean hasRecipient() { return recipient != null; }
|
2019-11-09 10:04:58 +01:00
|
|
|
}
|