From affbcc177467161f6078bb6e3f11e64784c8abb8 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 5 Oct 2019 15:07:56 +0200 Subject: [PATCH] Added support for new message schema --- src/main/java/envoy/client/EnvoyClient.java | 25 +++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/envoy/client/EnvoyClient.java b/src/main/java/envoy/client/EnvoyClient.java index a6f5fb9..41c32d0 100644 --- a/src/main/java/envoy/client/EnvoyClient.java +++ b/src/main/java/envoy/client/EnvoyClient.java @@ -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; + } } \ No newline at end of file