Merge pull request #2 from informatik-ag-ngl/f/config
Added server.properties file
This commit is contained in:
commit
ffdca7172b
@ -1,107 +1,123 @@
|
||||
package envoy.client;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
|
||||
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;
|
||||
|
||||
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
|
||||
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(messages, "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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
2
src/main/resources/server.properties
Normal file
2
src/main/resources/server.properties
Normal file
@ -0,0 +1,2 @@
|
||||
server=http://kske.feste-ip.net
|
||||
port=43315
|
Reference in New Issue
Block a user