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; package envoy.client;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays;
import javax.ws.rs.client.Client; import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
@ -14,6 +15,7 @@ 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.Messages;
import envoy.schema.ObjectFactory; import envoy.schema.ObjectFactory;
/** /**
@ -25,6 +27,7 @@ import envoy.schema.ObjectFactory;
public class EnvoyClient { public class EnvoyClient {
private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory; private DatatypeFactory datatypeFactory;
public EnvoyClient() { public EnvoyClient() {
@ -46,13 +49,16 @@ public class EnvoyClient {
*/ */
public void sendMessage(Message message) { public void sendMessage(Message message) {
new Thread(() -> { new Thread(() -> {
// Wrap single message into messages list
Messages messages = wrapMessage(message);
// Print message XML to console // Print message XML to console
JAXBContext jc; JAXBContext jc;
try { try {
jc = JAXBContext.newInstance("envoy.schema"); jc = JAXBContext.newInstance("envoy.schema");
Marshaller m = jc.createMarshaller(); Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(message, System.out); m.marshal(messages, System.out);
} catch (JAXBException e) { } catch (JAXBException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -60,7 +66,7 @@ public class EnvoyClient {
// Send message // Send message
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://kske.feste-ip.net:43315/envoy-server/rest/message/send"); 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()); System.out.println("Response code: " + response.getStatus());
response.close(); response.close();
client.close(); client.close();
@ -76,21 +82,26 @@ public class EnvoyClient {
* @return Prepared {@link Message} object * @return Prepared {@link Message} object
*/ */
public Message createMessage(String senderID, String recipientID, String textContent) { public Message createMessage(String senderID, String recipientID, String textContent) {
ObjectFactory factory = new ObjectFactory(); Message.MetaData metaData = objectFactory.createMessageMetaData();
Message.MetaData metaData = factory.createMessageMetaData();
metaData.setSender(senderID); metaData.setSender(senderID);
metaData.setRecipient(recipientID); metaData.setRecipient(recipientID);
metaData.setState(false); metaData.setState(false);
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
Message.Content content = factory.createMessageContent(); Message.Content content = objectFactory.createMessageContent();
content.setType("text"); content.setType("text");
content.setText(textContent); content.setText(textContent);
Message message = factory.createMessage(); Message message = objectFactory.createMessage();
message.setMetaData(metaData); message.setMetaData(metaData);
message.getContent().add(content); message.getContent().add(content);
return message; return message;
} }
public Messages wrapMessage(Message... messages) {
Messages wrapper = objectFactory.createMessages();
wrapper.getMessage().addAll(Arrays.asList(messages));
return wrapper;
}
} }