Merge branch 'develop' into f/config
This commit is contained in:
commit
043990c64d
@ -1,113 +1,123 @@
|
||||
package envoy.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
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;
|
||||
|
||||
import envoy.schema.Message;
|
||||
import envoy.schema.ObjectFactory;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
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(() -> {
|
||||
// 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(message, System.out);
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Send message
|
||||
String url = String.format("%s:%s/envoy-server/rest/message/send",
|
||||
serverProps.getProperty("server"),
|
||||
serverProps.getProperty("port"));
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(url);
|
||||
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
||||
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
|
||||
*/
|
||||
public Message createMessage(String senderID, String recipientID, String textContent) {
|
||||
ObjectFactory factory = new ObjectFactory();
|
||||
Message.MetaData metaData = factory.createMessageMetaData();
|
||||
metaData.setSender(senderID);
|
||||
metaData.setRecipient(recipientID);
|
||||
metaData.setState(false);
|
||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||
|
||||
Message.Content content = factory.createMessageContent();
|
||||
content.setType("text");
|
||||
content.setText(textContent);
|
||||
|
||||
Message message = factory.createMessage();
|
||||
message.setMetaData(metaData);
|
||||
message.getContent().add(content);
|
||||
|
||||
return message;
|
||||
}
|
||||
package envoy.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
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;
|
||||
|
||||
import envoy.schema.Message;
|
||||
import envoy.schema.Messages;
|
||||
import envoy.schema.ObjectFactory;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
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
|
||||
String url = String.format("%s:%s/envoy-server/rest/message/send",
|
||||
serverProps.getProperty("server"),
|
||||
serverProps.getProperty("port"));
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(url);
|
||||
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
||||
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
|
||||
*/
|
||||
public Message createMessage(String senderID, String recipientID, String textContent) {
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user