Merge branch 'develop' into f/config
This commit is contained in:
commit
990785d1a8
@ -1,113 +1,123 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.ws.rs.client.Client;
|
import javax.ws.rs.client.Client;
|
||||||
import javax.ws.rs.client.ClientBuilder;
|
import javax.ws.rs.client.ClientBuilder;
|
||||||
import javax.ws.rs.client.Entity;
|
import javax.ws.rs.client.Entity;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
import javax.xml.datatype.DatatypeConfigurationException;
|
||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
|
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.Messages;
|
||||||
|
import envoy.schema.ObjectFactory;
|
||||||
/**
|
|
||||||
* Project: <strong>envoy-client</strong><br>
|
/**
|
||||||
* File: <strong>EnvoyClient.java</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* Created: <strong>28 Sep 2019</strong><br>
|
* File: <strong>EnvoyClient.java</strong><br>
|
||||||
* Author: <strong>Kai S. K. Engelbart & Maximilian Käfer</strong>
|
* Created: <strong>28 Sep 2019</strong><br>
|
||||||
*/
|
* Author: <strong>Kai S. K. Engelbart & Maximilian Käfer</strong>
|
||||||
|
*/
|
||||||
public class EnvoyClient {
|
|
||||||
|
public class EnvoyClient {
|
||||||
private DatatypeFactory datatypeFactory;
|
|
||||||
|
private ObjectFactory objectFactory = new ObjectFactory();
|
||||||
private static final Properties serverProps = new Properties();
|
private DatatypeFactory datatypeFactory;
|
||||||
|
|
||||||
static {
|
private static final Properties serverProps = new Properties();
|
||||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
||||||
try {
|
static {
|
||||||
serverProps.load(loader.getResourceAsStream("server.properties"));
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||||
|
try {
|
||||||
} catch (IOException e) {
|
serverProps.load(loader.getResourceAsStream("server.properties"));
|
||||||
e.printStackTrace();
|
|
||||||
}
|
} catch (IOException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
|
}
|
||||||
public EnvoyClient() {
|
}
|
||||||
try {
|
|
||||||
datatypeFactory = DatatypeFactory.newInstance();
|
public EnvoyClient() {
|
||||||
} catch (DatatypeConfigurationException e) {
|
try {
|
||||||
e.printStackTrace();
|
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.
|
* Sends a message with text content to the server.<br>
|
||||||
*
|
* Because sending a request is a blocking operation, it is executed
|
||||||
* @param sender Name of the sender
|
* asynchronously.
|
||||||
* @param recipient Name of the recipient
|
*
|
||||||
* @param textContent Content (text) of the message
|
* @param sender Name of the sender
|
||||||
*/
|
* @param recipient Name of the recipient
|
||||||
public void sendMessage(Message message) {
|
* @param textContent Content (text) of the message
|
||||||
new Thread(() -> {
|
*/
|
||||||
// Print message XML to console
|
public void sendMessage(Message message) {
|
||||||
JAXBContext jc;
|
new Thread(() -> {
|
||||||
try {
|
// Wrap single message into messages list
|
||||||
jc = JAXBContext.newInstance("envoy.schema");
|
Messages messages = wrapMessage(message);
|
||||||
Marshaller m = jc.createMarshaller();
|
|
||||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
// Print message XML to console
|
||||||
m.marshal(message, System.out);
|
JAXBContext jc;
|
||||||
} catch (JAXBException e) {
|
try {
|
||||||
e.printStackTrace();
|
jc = JAXBContext.newInstance("envoy.schema");
|
||||||
}
|
Marshaller m = jc.createMarshaller();
|
||||||
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||||
// Send message
|
m.marshal(messages, System.out);
|
||||||
String url = String.format("%s:%s/envoy-server/rest/message/send",
|
} catch (JAXBException e) {
|
||||||
serverProps.getProperty("server"),
|
e.printStackTrace();
|
||||||
serverProps.getProperty("port"));
|
}
|
||||||
Client client = ClientBuilder.newClient();
|
|
||||||
WebTarget target = client.target(url);
|
// Send message
|
||||||
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
String url = String.format("%s:%s/envoy-server/rest/message/send",
|
||||||
System.out.println("Response code: " + response.getStatus());
|
serverProps.getProperty("server"),
|
||||||
response.close();
|
serverProps.getProperty("port"));
|
||||||
client.close();
|
Client client = ClientBuilder.newClient();
|
||||||
}).start();
|
WebTarget target = client.target(url);
|
||||||
}
|
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
||||||
|
System.out.println("Response code: " + response.getStatus());
|
||||||
/**
|
response.close();
|
||||||
* Creates a {@link Message} object serializable to XML.
|
client.close();
|
||||||
*
|
}).start();
|
||||||
* @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
|
* Creates a {@link Message} object serializable to XML.
|
||||||
*/
|
*
|
||||||
public Message createMessage(String senderID, String recipientID, String textContent) {
|
* @param senderID The ID of the sender
|
||||||
ObjectFactory factory = new ObjectFactory();
|
* @param recipientID The ID of the recipient
|
||||||
Message.MetaData metaData = factory.createMessageMetaData();
|
* @param textContent The content (text) of the message
|
||||||
metaData.setSender(senderID);
|
* @return Prepared {@link Message} object
|
||||||
metaData.setRecipient(recipientID);
|
*/
|
||||||
metaData.setState(false);
|
public Message createMessage(String senderID, String recipientID, String textContent) {
|
||||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
Message.MetaData metaData = objectFactory.createMessageMetaData();
|
||||||
|
metaData.setSender(senderID);
|
||||||
Message.Content content = factory.createMessageContent();
|
metaData.setRecipient(recipientID);
|
||||||
content.setType("text");
|
metaData.setState(false);
|
||||||
content.setText(textContent);
|
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||||
|
|
||||||
Message message = factory.createMessage();
|
Message.Content content = objectFactory.createMessageContent();
|
||||||
message.setMetaData(metaData);
|
content.setType("text");
|
||||||
message.getContent().add(content);
|
content.setText(textContent);
|
||||||
|
|
||||||
return message;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user