Added support for new message schema

This commit is contained in:
Kai S. K. Engelbart 2019-10-05 15:07:56 +02:00
parent 5610174340
commit affbcc1774

View File

@ -1,6 +1,7 @@
package envoy.client;
import java.time.Instant;
import java.util.Arrays;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
@ -14,6 +15,7 @@ import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import envoy.schema.Message;
import envoy.schema.Messages;
import envoy.schema.ObjectFactory;
/**
@ -25,7 +27,8 @@ import envoy.schema.ObjectFactory;
public class EnvoyClient {
private DatatypeFactory datatypeFactory;
private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory;
public EnvoyClient() {
try {
@ -46,13 +49,16 @@ public class EnvoyClient {
*/
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(message, System.out);
m.marshal(messages, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
@ -60,7 +66,7 @@ public class EnvoyClient {
// Send message
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://kske.feste-ip.net:43315/envoy-server/rest/message/send");
Response response = target.request().post(Entity.entity(message, "application/xml"));
Response response = target.request().post(Entity.entity(messages, "application/xml"));
System.out.println("Response code: " + response.getStatus());
response.close();
client.close();
@ -76,21 +82,26 @@ public class EnvoyClient {
* @return Prepared {@link Message} object
*/
public Message createMessage(String senderID, String recipientID, String textContent) {
ObjectFactory factory = new ObjectFactory();
Message.MetaData metaData = factory.createMessageMetaData();
Message.MetaData metaData = objectFactory.createMessageMetaData();
metaData.setSender(senderID);
metaData.setRecipient(recipientID);
metaData.setState(false);
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
Message.Content content = factory.createMessageContent();
Message.Content content = objectFactory.createMessageContent();
content.setType("text");
content.setText(textContent);
Message message = factory.createMessage();
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;
}
}