2019-10-06 10:45:19 +02:00
|
|
|
package envoy.client;
|
|
|
|
|
|
|
|
import java.time.Instant;
|
2019-10-06 13:30:31 +02:00
|
|
|
import java.util.Arrays;
|
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;
|
|
|
|
import javax.xml.datatype.DatatypeConfigurationException;
|
|
|
|
import javax.xml.datatype.DatatypeFactory;
|
2019-10-07 16:14:14 +02:00
|
|
|
|
2019-10-06 10:45:19 +02:00
|
|
|
import envoy.schema.Message;
|
2019-10-12 08:19:00 +02:00
|
|
|
import envoy.schema.Message.MetaData.MessageState;
|
2019-10-06 10:45:19 +02:00
|
|
|
import envoy.schema.Messages;
|
|
|
|
import envoy.schema.ObjectFactory;
|
2019-10-12 14:45:58 +02:00
|
|
|
import envoy.schema.User;
|
2019-10-07 17:35:57 +02:00
|
|
|
import envoy.schema.Users;
|
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-12 08:19:00 +02:00
|
|
|
* @since Envoy 0.1
|
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();
|
|
|
|
private DatatypeFactory datatypeFactory;
|
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-10-12 14:45:58 +02:00
|
|
|
public Client(Config config, String username) {
|
2019-10-12 11:19:29 +02:00
|
|
|
this.config = config;
|
2019-10-06 10:45:19 +02:00
|
|
|
try {
|
|
|
|
datatypeFactory = DatatypeFactory.newInstance();
|
|
|
|
} catch (DatatypeConfigurationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-10-19 12:10:52 +02:00
|
|
|
sender = getUser(username);
|
2019-10-06 10:45:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a message with text content to the server.<br>
|
|
|
|
* Because sending a request is a blocking operation, it is executed
|
|
|
|
* asynchronously.
|
|
|
|
*
|
2019-10-13 17:25:20 +02:00
|
|
|
* @param sender name of the sender
|
|
|
|
* @param recipient name of the recipient
|
|
|
|
* @param textContent content (text) of the message
|
2019-10-06 10:45:19 +02:00
|
|
|
*/
|
|
|
|
public void sendMessage(Message message) {
|
|
|
|
new Thread(() -> {
|
|
|
|
// Wrap single message into messages list
|
2019-10-26 18:31:49 +02:00
|
|
|
Messages messages = wrapMessages(message);
|
2019-10-06 10:45:19 +02:00
|
|
|
|
|
|
|
// Print message XML to console
|
|
|
|
JAXBContext jc;
|
|
|
|
try {
|
|
|
|
jc = JAXBContext.newInstance("envoy.schema");
|
|
|
|
Marshaller m = jc.createMarshaller();
|
|
|
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
|
|
|
m.marshal(messages, System.out);
|
|
|
|
} catch (JAXBException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send message
|
2019-10-12 11:19:29 +02:00
|
|
|
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
|
|
|
WebTarget target = client
|
|
|
|
.target(String.format("%s:%d/envoy-server/rest/message/send", config.getServer(), config.getPort()));
|
|
|
|
|
|
|
|
Response response = target.request().post(Entity.entity(messages, "application/xml"));
|
2019-10-06 10:45:19 +02:00
|
|
|
System.out.println("Response code: " + response.getStatus());
|
|
|
|
response.close();
|
|
|
|
client.close();
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a {@link Message} object serializable to XML.
|
|
|
|
*
|
|
|
|
* @param textContent The content (text) of the message
|
2019-10-13 17:25:20 +02:00
|
|
|
* @return prepared {@link Message} object
|
2019-10-06 10:45:19 +02:00
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public Message createMessage(String textContent) {
|
2019-10-06 10:45:19 +02:00
|
|
|
Message.MetaData metaData = objectFactory.createMessageMetaData();
|
2019-10-19 12:10:52 +02:00
|
|
|
metaData.setSender(sender.getID());
|
|
|
|
metaData.setRecipient(recipient.getID());
|
2019-10-12 08:19:00 +02:00
|
|
|
metaData.setState(MessageState.WAITING);
|
2019-10-06 10:45:19 +02:00
|
|
|
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
|
|
|
|
|
|
|
Message.Content content = objectFactory.createMessageContent();
|
|
|
|
content.setType("text");
|
|
|
|
content.setText(textContent);
|
|
|
|
|
|
|
|
Message message = objectFactory.createMessage();
|
|
|
|
message.setMetaData(metaData);
|
|
|
|
message.getContent().add(content);
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2019-10-26 18:31:49 +02:00
|
|
|
public Users getUsersListXml() { return get(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()), Users.class); }
|
2019-10-12 11:19:29 +02:00
|
|
|
|
2019-10-26 18:31:49 +02:00
|
|
|
public Messages getUnreadMessages(long userId) {
|
|
|
|
return get(String.format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId), Messages.class);
|
2019-10-07 16:14:14 +02:00
|
|
|
}
|
2019-10-13 17:25:20 +02:00
|
|
|
|
2019-10-12 14:45:58 +02:00
|
|
|
/**
|
2019-10-13 17:25:20 +02:00
|
|
|
* Returns a {@link User} with a specific name by name.
|
2019-10-12 14:45:58 +02:00
|
|
|
*
|
2019-10-13 17:25:20 +02:00
|
|
|
* @param name - the name of the {@link User}
|
|
|
|
* @return a {@link User} with the specified name
|
2019-10-26 18:31:49 +02:00
|
|
|
* @since Envoy v0.1-alpha
|
2019-10-12 14:45:58 +02:00
|
|
|
*/
|
|
|
|
private User getUser(String name) {
|
2019-10-26 18:31:49 +02:00
|
|
|
return get(String.format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name), Users.class).getUser()
|
|
|
|
.get(0);
|
2019-10-12 14:45:58 +02:00
|
|
|
}
|
2019-10-13 17:25:20 +02:00
|
|
|
|
2019-10-26 18:31:49 +02:00
|
|
|
/**
|
|
|
|
* Invokes the GET method of a web service.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the object returned by the web service
|
|
|
|
* @param uri the URI of the web service
|
|
|
|
* @param responseClass the class of the object returned by the web service
|
|
|
|
* @return the object returned by the web service
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
private <T> T get(String uri, Class<T> responseClass) {
|
2019-10-19 15:09:32 +02:00
|
|
|
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
2019-10-26 18:31:49 +02:00
|
|
|
WebTarget target = client.target(uri);
|
2019-10-19 15:09:32 +02:00
|
|
|
Response response = target.request("application/xml").get();
|
2019-10-26 18:31:49 +02:00
|
|
|
T responseObject = response.readEntity(responseClass);
|
2019-10-19 15:09:32 +02:00
|
|
|
System.out.println("Response code: " + response.getStatus());
|
|
|
|
response.close();
|
|
|
|
client.close();
|
2019-10-26 18:31:49 +02:00
|
|
|
return responseObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps one or more {@link Message} objects into a {@link Messages} object.
|
|
|
|
*
|
|
|
|
* @param messages the {@link Message} objects to wrap
|
|
|
|
* @return {@link Messages} object with all messages as its children
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
private Messages wrapMessages(Message... messages) {
|
|
|
|
Messages wrapper = objectFactory.createMessages();
|
|
|
|
wrapper.getMessage().addAll(Arrays.asList(messages));
|
|
|
|
return wrapper;
|
2019-10-19 15:09:32 +02:00
|
|
|
}
|
|
|
|
|
2019-10-12 17:35:58 +02:00
|
|
|
/**
|
2019-10-19 12:10:52 +02: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; }
|
|
|
|
|
|
|
|
public User getRecipient() { return recipient; }
|
|
|
|
|
|
|
|
public void setRecipient(User recipient) { this.recipient = recipient; }
|
|
|
|
|
|
|
|
public boolean hasRecipient() { return recipient != null; }
|
2019-09-30 09:53:16 +02:00
|
|
|
}
|