2019-10-06 10:45:19 +02:00
|
|
|
package envoy.client;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
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 java.util.Properties;
|
|
|
|
|
|
|
|
import javax.ws.rs.client.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;
|
|
|
|
import javax.xml.datatype.DatatypeConfigurationException;
|
|
|
|
import javax.xml.datatype.DatatypeFactory;
|
2019-10-07 16:14:14 +02:00
|
|
|
|
|
|
|
import envoy.client.ui.ChatWindow;
|
2019-10-06 10:45:19 +02:00
|
|
|
import envoy.schema.Message;
|
|
|
|
import envoy.schema.Messages;
|
|
|
|
import envoy.schema.ObjectFactory;
|
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>
|
|
|
|
* File: <strong>EnvoyClient.java</strong><br>
|
|
|
|
* Created: <strong>28 Sep 2019</strong><br>
|
|
|
|
* Author: <strong>Kai S. K. Engelbart & Maximilian Käfer</strong>
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class EnvoyClient {
|
|
|
|
|
2019-10-07 17:35:57 +02:00
|
|
|
ChatWindow chatWindow = new ChatWindow();
|
2019-10-06 10:45:19 +02:00
|
|
|
private ObjectFactory objectFactory = new ObjectFactory();
|
|
|
|
private DatatypeFactory datatypeFactory;
|
|
|
|
|
|
|
|
private static final Properties serverProps = new Properties();
|
|
|
|
|
|
|
|
static {
|
|
|
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
|
|
try {
|
|
|
|
serverProps.load(loader.getResourceAsStream("server.properties"));
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public EnvoyClient() {
|
|
|
|
try {
|
|
|
|
datatypeFactory = DatatypeFactory.newInstance();
|
|
|
|
} catch (DatatypeConfigurationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a message with text content to the server.<br>
|
|
|
|
* Because sending a request is a blocking operation, it is executed
|
|
|
|
* asynchronously.
|
|
|
|
*
|
|
|
|
* @param sender Name of the sender
|
|
|
|
* @param recipient Name of the recipient
|
|
|
|
* @param textContent Content (text) of the message
|
|
|
|
*/
|
|
|
|
public void sendMessage(Message message) {
|
|
|
|
new Thread(() -> {
|
|
|
|
// Wrap single message into messages list
|
|
|
|
Messages messages = wrapMessage(message);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
Client client = ClientBuilder.newClient();
|
2019-10-06 13:33:23 +02:00
|
|
|
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send",
|
|
|
|
serverProps.getProperty("server"),
|
|
|
|
serverProps.getProperty("port")));
|
2019-10-09 21:54:31 +02:00
|
|
|
|
2019-10-06 13:33:23 +02:00
|
|
|
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 senderID The ID of the sender
|
|
|
|
* @param recipientID The ID of the recipient
|
|
|
|
* @param textContent The content (text) of the message
|
|
|
|
* @return Prepared {@link Message} object
|
|
|
|
*/
|
2019-10-09 21:54:31 +02:00
|
|
|
public Message createMessage(String senderID, long recipientID, String textContent) {
|
2019-10-06 10:45:19 +02:00
|
|
|
Message.MetaData metaData = objectFactory.createMessageMetaData();
|
|
|
|
metaData.setSender(senderID);
|
|
|
|
metaData.setRecipient(recipientID);
|
|
|
|
metaData.setState(false);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Messages wrapMessage(Message... messages) {
|
|
|
|
Messages wrapper = objectFactory.createMessages();
|
|
|
|
wrapper.getMessage().addAll(Arrays.asList(messages));
|
|
|
|
return wrapper;
|
|
|
|
}
|
2019-10-09 21:54:31 +02:00
|
|
|
|
2019-10-07 17:35:57 +02:00
|
|
|
public Users getUsersListXml() {
|
|
|
|
Client client = ClientBuilder.newClient();
|
|
|
|
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/user",
|
2019-10-07 16:14:14 +02:00
|
|
|
serverProps.getProperty("server"),
|
|
|
|
serverProps.getProperty("port")));
|
2019-10-07 17:35:57 +02:00
|
|
|
Response response = target.request("application/xml").get();
|
|
|
|
Users users = response.readEntity(Users.class);
|
|
|
|
System.out.println("Response code: " + response.getStatus());
|
|
|
|
response.close();
|
|
|
|
client.close();
|
|
|
|
return users;
|
2019-10-07 16:14:14 +02:00
|
|
|
}
|
2019-09-30 09:53:16 +02:00
|
|
|
}
|