2019-10-06 10:45:19 +02:00
|
|
|
package envoy.client;
|
|
|
|
|
2019-12-14 10:53:20 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-11-27 17:07:25 +01:00
|
|
|
|
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-12-11 18:52:30 +01:00
|
|
|
import envoy.exception.EnvoyException;
|
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-12-11 18:52:30 +01:00
|
|
|
private boolean online = false;
|
2019-10-06 10:45:19 +02:00
|
|
|
|
2019-12-14 11:30:00 +01:00
|
|
|
/**
|
|
|
|
* Initializes the client. At this state, the client user has yet to be
|
2019-12-20 12:53:40 +01:00
|
|
|
* initialized, which can be done by calling {@link Client#onlineInit(String)}.
|
2019-12-14 11:30:00 +01:00
|
|
|
*
|
|
|
|
* @param config The {@link Config} instance to use in this client
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-14 10:53:20 +01:00
|
|
|
public Client(Config config) { this.config = config; }
|
2019-11-27 17:07:25 +01:00
|
|
|
|
2019-12-14 11:30:00 +01:00
|
|
|
/**
|
|
|
|
* Enters the online mode by acquiring a user ID from the server.
|
|
|
|
*
|
|
|
|
* @param userName the name of the client user
|
|
|
|
* @throws EnvoyException if the online mode could not be entered or the request
|
|
|
|
* failed for some other reason
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-14 10:53:20 +01:00
|
|
|
public void onlineInit(String userName) throws EnvoyException {
|
|
|
|
sender = getUser(userName);
|
|
|
|
online = true;
|
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-04 23:10:53 +01:00
|
|
|
}
|
2019-11-09 13:25:18 +01:00
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
/**
|
2019-12-14 11:30:00 +01:00
|
|
|
* @return a {@code Map<String, User>} of all users on the server with their
|
|
|
|
* user names as keys
|
2019-12-14 10:53:20 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-11-04 23:10:53 +01:00
|
|
|
*/
|
2019-12-14 10:53:20 +01:00
|
|
|
public Map<String, User> getUsers() {
|
2019-11-04 23:10:53 +01:00
|
|
|
Sync sendSync = objectFactory.createSync();
|
|
|
|
User user = objectFactory.createUser();
|
|
|
|
user.setID(-1);
|
|
|
|
sendSync.getUsers().add(user);
|
|
|
|
|
2019-12-14 10:53:20 +01:00
|
|
|
Sync returnSync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
2019-11-09 13:25:18 +01:00
|
|
|
sendSync,
|
|
|
|
Sync.class);
|
2019-11-04 23:10:53 +01:00
|
|
|
|
2019-12-14 10:53:20 +01:00
|
|
|
Map<String, User> users = new HashMap<>();
|
|
|
|
returnSync.getUsers().forEach(u -> users.put(u.getName(), u));
|
|
|
|
return users;
|
2019-11-04 23:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2019-12-11 18:52:30 +01:00
|
|
|
* @throws EnvoyException if the server does not return the requested ID
|
2019-11-04 23:10:53 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-12-11 18:52:30 +01:00
|
|
|
private User getUser(String name) throws EnvoyException {
|
|
|
|
// Create a sync with only a user with the requested name
|
2019-11-04 23:10:53 +01:00
|
|
|
Sync senderSync = objectFactory.createSync();
|
|
|
|
User user = objectFactory.createUser();
|
|
|
|
user.setName(name);
|
|
|
|
senderSync.getUsers().add(user);
|
|
|
|
|
2019-12-11 18:52:30 +01:00
|
|
|
try {
|
|
|
|
Sync sync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
|
|
|
senderSync,
|
|
|
|
Sync.class);
|
|
|
|
|
|
|
|
// Expecting a single user with an ID
|
|
|
|
if (sync.getUsers().size() == 1) {
|
|
|
|
online = true;
|
|
|
|
return sync.getUsers().get(0);
|
|
|
|
} else throw new EnvoyException("Unexpected response from Envoy Server");
|
|
|
|
} catch (Exception e) {
|
2019-12-14 09:58:25 +01:00
|
|
|
throw new EnvoyException("Could not connect to server", e);
|
2019-11-04 23:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-12-11 18:52:30 +01:00
|
|
|
* @param sync the sync object (yet to be converted from java class to
|
|
|
|
* sync.xml)
|
2019-11-22 22:40:00 +01:00
|
|
|
* @return a returnSync.xml file
|
2019-12-14 11:30:00 +01:00
|
|
|
* @throws EnvoyException if the client is not in online mode
|
2019-11-04 23:10:53 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-12-14 11:30:00 +01:00
|
|
|
public Sync sendSync(long userId, Sync sync) throws EnvoyException {
|
|
|
|
if(!isOnline())
|
|
|
|
throw new EnvoyException("Client is not in online mode");
|
2019-11-09 13:25:18 +01:00
|
|
|
// 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
|
2019-12-11 18:52:30 +01:00
|
|
|
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-12-14 11:30:00 +01:00
|
|
|
/**
|
|
|
|
* Sets the client user which is used to send messages.
|
|
|
|
*
|
|
|
|
* @param sender the client user to set
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-14 10:53:20 +01:00
|
|
|
public void setSender(User sender) { this.sender = 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-12-14 11:30:00 +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-12-11 18:52:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {@code true} if a connection to the server could be established
|
2019-12-14 11:30:00 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-12-11 18:52:30 +01:00
|
|
|
*/
|
|
|
|
public boolean isOnline() { return online; }
|
2019-11-09 10:04:58 +01:00
|
|
|
}
|