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

180 lines
5.3 KiB
Java
Raw Normal View History

2019-10-06 10:45:19 +02:00
package envoy.client;
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;
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;
2019-10-06 10:45:19 +02:00
public Client(Config config, String username) {
2019-11-09 13:25:18 +01:00
this.config = config;
sender = getUser(username);
System.out.println("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) {
javax.ws.rs.client.Client client = ClientBuilder.newClient();
WebTarget target = client.target(uri);
2019-10-06 10:45:19 +02:00
2019-11-09 13:25:18 +01:00
Response response = target.request().post(Entity.entity(body, "application/xml"));
R responseBody = response.readEntity(responseBodyClass);
response.close();
client.close();
2019-11-09 13:25:18 +01:00
return responseBody;
2019-11-09 17:23:15 +01:00
}
2019-11-09 13:25:18 +01:00
/**
* Returns a {@link Sync} with all users on the server.
2019-11-09 13:25:18 +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);
return returnSendSync;
}
/**
* 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
* @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();
if (returnSenderSync.getUsers().size() == 1) {
returnSender = returnSenderSync.getUsers().get(0);
} else {
System.out.println("ERROR exiting...");
}
2019-11-09 13:25:18 +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>
* 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>
*
* @param userId
* @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; }
/**
* @return the current recipient of the current chat.
* @since Envoy v0.1-alpha
*/
public User getRecipient() { return recipient; }
/**
2019-11-09 17:23:15 +01:00
* Sets the recipient.
* @param recipient - the recipient to set
* @since Envoy v0.1-alpha
*/
public void setRecipient(User recipient) { this.recipient = recipient; }
2019-11-09 17:23:15 +01:00
/**
* @return true, if a recipient is selected
* @since Envoy v0.1-alpha
*/
public boolean hasRecipient() { return recipient != null; }
2019-11-09 10:04:58 +01:00
}