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

Renamed EnvoyClient to Client, added Config class
This commit is contained in:
Kai S. K. Engelbart 2019-10-12 11:26:11 +02:00 committed by GitHub
commit 5ce2d87793
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 44 deletions

View File

@ -1,11 +1,8 @@
package envoy.client;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
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;
@ -24,7 +21,7 @@ import envoy.schema.Users;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>EnvoyClient.java</strong><br>
* File: <strong>Client.java</strong><br>
* Created: <strong>28 Sep 2019</strong><br>
*
* @author Kai S. K. Engelbart
@ -32,24 +29,14 @@ import envoy.schema.Users;
* @since Envoy 0.1
*/
public class EnvoyClient {
public class Client {
private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory;
private Config config;
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() {
public Client(Config config) {
this.config = config;
try {
datatypeFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
@ -83,10 +70,9 @@ public class EnvoyClient {
}
// Send message
Client client = ClientBuilder.newClient();
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send",
serverProps.getProperty("server"),
serverProps.getProperty("port")));
javax.ws.rs.client.Client client = ClientBuilder.newClient();
WebTarget target = client
.target(String.format("%s:%d/envoy-server/rest/message/send", config.getServer(), config.getPort()));
Response response = target.request().post(Entity.entity(messages, "application/xml"));
System.out.println("Response code: " + response.getStatus());
@ -128,10 +114,9 @@ public class EnvoyClient {
}
public Users getUsersListXml() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/user",
serverProps.getProperty("server"),
serverProps.getProperty("port")));
javax.ws.rs.client.Client client = ClientBuilder.newClient();
WebTarget target = client
.target(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()));
Response response = target.request("application/xml").get();
Users users = response.readEntity(Users.class);
System.out.println("Response code: " + response.getStatus());

View File

@ -0,0 +1,46 @@
package envoy.client;
import java.util.Properties;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Config.java</strong><br>
* Created: <strong>12 Oct 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy 0.1
*/
public class Config {
private String server;
private int port;
public void load(Properties properties) {
if (properties.containsKey("server")) server = properties.getProperty("server");
if (properties.containsKey("port")) port = Integer.parseInt(properties.getProperty("port"));
}
public void load(String[] args) {
for (int i = 0; i < args.length; i++)
switch (args[i]) {
case "--server":
case "-s":
server = args[++i];
break;
case "--port":
case "-p":
port = Integer.parseInt(args[++i]);
break;
}
}
public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0; }
public String getServer() { return server; }
public void setServer(String server) { this.server = server; }
public int getPort() { return port; }
public void setPort(int port) { this.port = port; }
}

View File

@ -8,6 +8,7 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.util.Properties;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
@ -20,12 +21,9 @@ import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;
import envoy.client.EnvoyClient;
import envoy.client.Client;
import envoy.client.Config;
import envoy.schema.Message;
import envoy.schema.User;
import envoy.schema.Users;
@ -45,7 +43,8 @@ public class ChatWindow extends JFrame {
private long recipientID = 0;
private JPanel contentPane = new JPanel();
private EnvoyClient envoyClient = new EnvoyClient();
private static Client client;
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
@ -136,8 +135,8 @@ public class ChatWindow extends JFrame {
// TODO: Acquire proper sender id
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
final Message message = envoyClient.createMessage(1, recipientID, messageEnterTextfield.getText());
envoyClient.sendMessage(message);
final Message message = client.createMessage(1, recipientID, messageEnterTextfield.getText());
client.sendMessage(message);
appendMessageToChat(message);
messageEnterTextfield.setText("");
} catch (Exception e) {
@ -187,7 +186,7 @@ public class ChatWindow extends JFrame {
*/
private void loadUserList(JList<User> userList) {
new Thread(() -> {
Users users = envoyClient.getUsersListXml();
Users users = client.getUsersListXml();
DefaultListModel<User> userListModel = new DefaultListModel<>();
users.getUser().forEach(user -> userListModel.addElement(user));
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
@ -213,8 +212,28 @@ public class ChatWindow extends JFrame {
+ getFirstTextContent(message) + "</span></html>");
}
public static void main(String[] args)
throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
public static void main(String[] args) {
Config config = new Config();
if (args.length > 0) {
config.load(args);
} else {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
Properties configProperties = new Properties();
configProperties.load(loader.getResourceAsStream("server.properties"));
config.load(configProperties);
} catch (IOException e) {
e.printStackTrace();
}
}
if(!config.isInitialized()) {
System.err.println("Server or port are not defined. Exiting...");
System.exit(1);
}
client = new Client(config);
EventQueue.invokeLater(() -> {
try {
ChatWindow frame = new ChatWindow();