Merge pull request #2 from informatik-ag-ngl/f/config

Added server.properties file
This commit is contained in:
delvh 2019-10-06 12:44:10 +02:00 committed by GitHub
commit ffdca7172b
2 changed files with 124 additions and 106 deletions

View File

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

View File

@ -0,0 +1,2 @@
server=http://kske.feste-ip.net
port=43315