From adefab34ccbfa1eb8968a641a17fef66a1b74528 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 12 Oct 2019 14:45:58 +0200 Subject: [PATCH 1/3] Added Startup class and login by username. --- src/main/java/envoy/client/Client.java | 26 +++++++- src/main/java/envoy/client/ui/ChatWindow.java | 38 ++--------- src/main/java/envoy/client/ui/Startup.java | 63 +++++++++++++++++++ 3 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 src/main/java/envoy/client/ui/Startup.java diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index f302a39..51ee5c0 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -17,6 +17,7 @@ import envoy.schema.Message; import envoy.schema.Message.MetaData.MessageState; import envoy.schema.Messages; import envoy.schema.ObjectFactory; +import envoy.schema.User; import envoy.schema.Users; /** @@ -26,6 +27,7 @@ import envoy.schema.Users; * * @author Kai S. K. Engelbart * @author Maximilian Käfer + * @author Leon Hofmeister * @since Envoy 0.1 */ @@ -34,14 +36,17 @@ public class Client { private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; private Config config; + private User user; - public Client(Config config) { + public Client(Config config, String username) { this.config = config; try { datatypeFactory = DatatypeFactory.newInstance(); } catch (DatatypeConfigurationException e) { e.printStackTrace(); } + user = getUser(username); + System.out.printf("Mein Name ist %s und ich habe die ID %d%n", user.getName(), user.getID()); } /** @@ -124,4 +129,23 @@ public class Client { client.close(); return users; } + + /** + * Returns the user by name. + * + * @param name - the name of the user + * @return an user object + * @since Envoy 0.1 + */ + private User getUser(String name) { + javax.ws.rs.client.Client client = ClientBuilder.newClient(); + WebTarget target = client + .target(String.format("%s:%d/envoy-server/rest/user/sender?name=" + name, 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(); + return users.getUser().get(0); + } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 6e4891d..8f71776 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -35,6 +35,7 @@ import envoy.schema.Users; * * @author Kai S. K. Engelbart * @author Maximilian Käfer + * @author Leon Hofmeister * @since Envoy 0.1 */ public class ChatWindow extends JFrame { @@ -44,11 +45,13 @@ public class ChatWindow extends JFrame { private long recipientID = 0; private JPanel contentPane = new JPanel(); - private static Client client; + private Client client; private DefaultListModel messageListModel = new DefaultListModel<>(); - public ChatWindow() { + public ChatWindow(Client client) { + this.client = client; + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); setTitle("Envoy"); @@ -212,35 +215,4 @@ public class ChatWindow extends JFrame { + getFirstTextContent(message) + ""); } - 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(); - frame.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } - }); - } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java new file mode 100644 index 0000000..d2848a9 --- /dev/null +++ b/src/main/java/envoy/client/ui/Startup.java @@ -0,0 +1,63 @@ +package envoy.client.ui; + +import java.awt.EventQueue; +import java.io.IOException; +import java.util.Properties; + +import javax.swing.JOptionPane; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; + +import envoy.client.Client; +import envoy.client.Config; + +/** + * Starts Envoy and sets a user. + * + * Project: envoy-client
+ * File: Startup.java
+ * Created: 12 Oct 2019
+ * + * @author Leon Hofmeister + * @author Maximilian Käfer + * @since Envoy 0.1 + */ +public class Startup { + + private static String userName; + + 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); + } + + userName = JOptionPane.showInputDialog("Please enter your username"); + + Client client = new Client(config, userName); + + EventQueue.invokeLater(() -> { + try { + ChatWindow frame = new ChatWindow(client); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } +} From 00f70f19fbefd142391dba820607d0af9f5e24a8 Mon Sep 17 00:00:00 2001 From: Maxi Date: Sat, 12 Oct 2019 17:35:58 +0200 Subject: [PATCH 2/3] Added the setting of the senderID in the xml. --- src/main/java/envoy/client/Client.java | 10 ++++++++++ src/main/java/envoy/client/ui/ChatWindow.java | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 51ee5c0..c87d616 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -148,4 +148,14 @@ public class Client { client.close(); return users.getUser().get(0); } + + /** + * Returns the user ID. + * @return userID + * @since Envoy 0.1 + */ + public long getSenderID() { + return user.getID(); + + } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 8f71776..0d20f74 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -138,7 +138,7 @@ public class ChatWindow extends JFrame { // TODO: Acquire proper sender id if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try { - final Message message = client.createMessage(1, recipientID, messageEnterTextfield.getText()); + final Message message = client.createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText()); client.sendMessage(message); appendMessageToChat(message); messageEnterTextfield.setText(""); From ff8beae4f6c06883e42229c89ac624767104fea6 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 13 Oct 2019 17:25:20 +0200 Subject: [PATCH 3/3] Fixed formatting and Javadoc Closes #5 --- src/main/java/envoy/client/Client.java | 32 ++++++++----------- src/main/java/envoy/client/ui/ChatWindow.java | 10 ++---- src/main/java/envoy/client/ui/Startup.java | 22 ++++++------- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index c87d616..53a7865 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -36,7 +36,7 @@ public class Client { private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; private Config config; - private User user; + private User user; public Client(Config config, String username) { this.config = config; @@ -54,9 +54,9 @@ public class Client { * 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 + * @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(() -> { @@ -92,7 +92,7 @@ public class Client { * @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 + * @return prepared {@link Message} object */ public Message createMessage(long senderID, long recipientID, String textContent) { Message.MetaData metaData = objectFactory.createMessageMetaData(); @@ -129,18 +129,18 @@ public class Client { client.close(); return users; } - + /** - * Returns the user by name. + * Returns a {@link User} with a specific name by name. * - * @param name - the name of the user - * @return an user object + * @param name - the name of the {@link User} + * @return a {@link User} with the specified name * @since Envoy 0.1 */ private User getUser(String name) { javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client - .target(String.format("%s:%d/envoy-server/rest/user/sender?name=" + name, config.getServer(), config.getPort())); + WebTarget target = client.target(String + .format("%s:%d/envoy-server/rest/user/sender?name=" + name, config.getServer(), config.getPort())); Response response = target.request("application/xml").get(); Users users = response.readEntity(Users.class); System.out.println("Response code: " + response.getStatus()); @@ -148,14 +148,10 @@ public class Client { client.close(); return users.getUser().get(0); } - + /** - * Returns the user ID. - * @return userID + * @return the user id of this client * @since Envoy 0.1 */ - public long getSenderID() { - return user.getID(); - - } + public long getSenderID() { return user.getID(); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 0d20f74..0e9bc40 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -2,13 +2,10 @@ package envoy.client.ui; import java.awt.Color; import java.awt.ComponentOrientation; -import java.awt.EventQueue; import java.awt.Font; 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; @@ -23,7 +20,6 @@ import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; import envoy.client.Client; -import envoy.client.Config; import envoy.schema.Message; import envoy.schema.User; import envoy.schema.Users; @@ -51,7 +47,7 @@ public class ChatWindow extends JFrame { public ChatWindow(Client client) { this.client = client; - + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); setTitle("Envoy"); @@ -138,7 +134,8 @@ public class ChatWindow extends JFrame { // TODO: Acquire proper sender id if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try { - final Message message = client.createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText()); + final Message message = client + .createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText()); client.sendMessage(message); appendMessageToChat(message); messageEnterTextfield.setText(""); @@ -214,5 +211,4 @@ public class ChatWindow extends JFrame { + message.getMetaData().getSender() + " " + "
" + "

" + getFirstTextContent(message) + ""); } - } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index d2848a9..b525861 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -5,15 +5,12 @@ import java.io.IOException; import java.util.Properties; import javax.swing.JOptionPane; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.Response; import envoy.client.Client; import envoy.client.Config; /** - * Starts Envoy and sets a user. + * Starts the Envoy client and prompts the user to enter their name. * * Project: envoy-client
* File: Startup.java
@@ -24,9 +21,7 @@ import envoy.client.Config; * @since Envoy 0.1 */ public class Startup { - - private static String userName; - + public static void main(String[] args) { Config config = new Config(); if (args.length > 0) { @@ -42,15 +37,18 @@ public class Startup { } } - if(!config.isInitialized()) { + if (!config.isInitialized()) { System.err.println("Server or port are not defined. Exiting..."); System.exit(1); } - userName = JOptionPane.showInputDialog("Please enter your username"); - + String userName = JOptionPane.showInputDialog("Please enter your username"); + if (userName == null || userName.isEmpty()) { + System.err.println("User name is not set or empty. Exiting..."); + System.exit(1); + } Client client = new Client(config, userName); - + EventQueue.invokeLater(() -> { try { ChatWindow frame = new ChatWindow(client); @@ -60,4 +58,4 @@ public class Startup { } }); } -} +} \ No newline at end of file