This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/Client.java

192 lines
6.2 KiB
Java
Raw Normal View History

2019-10-06 10:45:19 +02:00
package envoy.client;
import java.util.HashMap;
import java.util.Map;
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
import envoy.exception.EnvoyException;
2019-10-06 10:45:19 +02:00
import envoy.schema.ObjectFactory;
import envoy.schema.Sync;
import envoy.schema.User;
2019-10-06 10:45:19 +02:00
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Client.java</strong><br>
2019-10-06 10:45:19 +02:00
* Created: <strong>28 Sep 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @author Leon Hofmeister
* @since Envoy v0.1-alpha
2019-10-06 10:45:19 +02:00
*/
public class Client {
2019-10-06 10:45:19 +02:00
private ObjectFactory objectFactory = new ObjectFactory();
private Config config;
private User sender, recipient;
private boolean online = false;
2019-10-06 10:45:19 +02:00
public Client(Config config) { this.config = config; }
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-09 13:25:18 +01:00
return responseBody;
}
2019-11-09 13:25:18 +01:00
/**
* Returns a {@code Map<String, User>} of all users on the server with their
* user names as keys.
2019-11-09 13:25:18 +01:00
*
* @return Sync - List of all users on the server.
* @since Envoy v0.2-alpha
*/
public Map<String, User> getUsers() {
Sync sendSync = objectFactory.createSync();
User user = objectFactory.createUser();
user.setID(-1);
sendSync.getUsers().add(user);
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);
Map<String, User> users = new HashMap<>();
returnSync.getUsers().forEach(u -> users.put(u.getName(), u));
return users;
}
/**
* Returns a {@link User} with a specific id by name.
*
* @param name - the name of the {@link User}
* @return a {@link User} with the specified name
* @throws EnvoyException if the server does not return the requested ID
* @since Envoy v0.1-alpha
*/
private User getUser(String name) throws EnvoyException {
// Create a sync with only a user with the requested name
Sync senderSync = objectFactory.createSync();
User user = objectFactory.createUser();
user.setName(name);
senderSync.getUsers().add(user);
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) {
throw new EnvoyException("Could not connect to server", e);
}
}
/**
* 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>
* 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>
* <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-12-07 10:44:25 +01:00
* @param userId the id of the {@link Client} who sends the {@link Sync}
* @param sync the sync object (yet to be converted from java class to
* sync.xml)
* @return a returnSync.xml file
* @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-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);
}
/**
* @return the sender object that represents this client.
* @since Envoy v0.1-alpha
*/
public User getSender() { return sender; }
public void setSender(User sender) { this.sender = sender; }
/**
* @return the current recipient of the current chat.
* @since Envoy v0.1-alpha
*/
public User getRecipient() { return recipient; }
/**
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
* @since Envoy v0.1-alpha
*/
public void setRecipient(User recipient) { this.recipient = recipient; }
2019-12-07 10:44:25 +01:00
/**
* @return true, if a recipient is selected
* @since Envoy v0.1-alpha
*/
public boolean hasRecipient() { return recipient != null; }
/**
* @return {@code true} if a connection to the server could be established
*/
public boolean isOnline() { return online; }
2019-11-09 10:04:58 +01:00
}