From 68aca9d0ab557638c0e5d7282a96914cef5caaca Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 12 Oct 2019 11:19:29 +0200 Subject: [PATCH] Renamed EnvoyClient to Client, added Config class --- .../client/{EnvoyClient.java => Client.java} | 47 +++++++------------ src/main/java/envoy/client/Config.java | 46 ++++++++++++++++++ src/main/java/envoy/client/ui/ChatWindow.java | 45 +++++++++++++----- 3 files changed, 94 insertions(+), 44 deletions(-) rename src/main/java/envoy/client/{EnvoyClient.java => Client.java} (74%) create mode 100644 src/main/java/envoy/client/Config.java diff --git a/src/main/java/envoy/client/EnvoyClient.java b/src/main/java/envoy/client/Client.java similarity index 74% rename from src/main/java/envoy/client/EnvoyClient.java rename to src/main/java/envoy/client/Client.java index 7bef3e5..f302a39 100644 --- a/src/main/java/envoy/client/EnvoyClient.java +++ b/src/main/java/envoy/client/Client.java @@ -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: envoy-client
- * File: EnvoyClient.java
+ * File: Client.java
* Created: 28 Sep 2019
* * @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,12 +70,11 @@ 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"))); - - Response response = target.request().post(Entity.entity(messages, "application/xml")); + 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()); response.close(); client.close(); @@ -126,14 +112,13 @@ public class EnvoyClient { wrapper.getMessage().addAll(Arrays.asList(messages)); return wrapper; } - + 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"))); - Response response = target.request("application/xml").get(); - Users users = response.readEntity(Users.class); + 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()); response.close(); client.close(); diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java new file mode 100644 index 0000000..8ee95bd --- /dev/null +++ b/src/main/java/envoy/client/Config.java @@ -0,0 +1,46 @@ +package envoy.client; + +import java.util.Properties; + +/** + * Project: envoy-client
+ * File: Config.java
+ * Created: 12 Oct 2019
+ * + * @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; } +} diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 178c738..6e4891d 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -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; @@ -43,9 +41,10 @@ public class ChatWindow extends JFrame { private static final long serialVersionUID = 6865098428255463649L; - private long recipientID = 0; - private JPanel contentPane = new JPanel(); - private EnvoyClient envoyClient = new EnvoyClient(); + private long recipientID = 0; + private JPanel contentPane = new JPanel(); + + private static Client client; private DefaultListModel 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 userList) { new Thread(() -> { - Users users = envoyClient.getUsersListXml(); + Users users = client.getUsersListXml(); DefaultListModel 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) + ""); } - 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();