Renamed EnvoyClient to Client, added Config class
This commit is contained in:
parent
aab15213d7
commit
fe95e6bfb6
@ -1,11 +1,8 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
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.ClientBuilder;
|
||||||
import javax.ws.rs.client.Entity;
|
import javax.ws.rs.client.Entity;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
@ -24,7 +21,7 @@ import envoy.schema.Users;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* 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>
|
* Created: <strong>28 Sep 2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
@ -32,24 +29,14 @@ import envoy.schema.Users;
|
|||||||
* @since Envoy 0.1
|
* @since Envoy 0.1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class EnvoyClient {
|
public class Client {
|
||||||
|
|
||||||
private ObjectFactory objectFactory = new ObjectFactory();
|
private ObjectFactory objectFactory = new ObjectFactory();
|
||||||
private DatatypeFactory datatypeFactory;
|
private DatatypeFactory datatypeFactory;
|
||||||
|
private Config config;
|
||||||
|
|
||||||
private static final Properties serverProps = new Properties();
|
public Client(Config config) {
|
||||||
|
this.config = config;
|
||||||
static {
|
|
||||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
||||||
try {
|
|
||||||
serverProps.load(loader.getResourceAsStream("server.properties"));
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public EnvoyClient() {
|
|
||||||
try {
|
try {
|
||||||
datatypeFactory = DatatypeFactory.newInstance();
|
datatypeFactory = DatatypeFactory.newInstance();
|
||||||
} catch (DatatypeConfigurationException e) {
|
} catch (DatatypeConfigurationException e) {
|
||||||
@ -83,12 +70,11 @@ public class EnvoyClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send message
|
// Send message
|
||||||
Client client = ClientBuilder.newClient();
|
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||||
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send",
|
WebTarget target = client
|
||||||
serverProps.getProperty("server"),
|
.target(String.format("%s:%d/envoy-server/rest/message/send", config.getServer(), config.getPort()));
|
||||||
serverProps.getProperty("port")));
|
|
||||||
|
|
||||||
Response response = target.request().post(Entity.entity(messages, "application/xml"));
|
Response response = target.request().post(Entity.entity(messages, "application/xml"));
|
||||||
System.out.println("Response code: " + response.getStatus());
|
System.out.println("Response code: " + response.getStatus());
|
||||||
response.close();
|
response.close();
|
||||||
client.close();
|
client.close();
|
||||||
@ -128,12 +114,11 @@ public class EnvoyClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Users getUsersListXml() {
|
public Users getUsersListXml() {
|
||||||
Client client = ClientBuilder.newClient();
|
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||||
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/user",
|
WebTarget target = client
|
||||||
serverProps.getProperty("server"),
|
.target(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()));
|
||||||
serverProps.getProperty("port")));
|
Response response = target.request("application/xml").get();
|
||||||
Response response = target.request("application/xml").get();
|
Users users = response.readEntity(Users.class);
|
||||||
Users users = response.readEntity(Users.class);
|
|
||||||
System.out.println("Response code: " + response.getStatus());
|
System.out.println("Response code: " + response.getStatus());
|
||||||
response.close();
|
response.close();
|
||||||
client.close();
|
client.close();
|
46
src/main/java/envoy/client/Config.java
Normal file
46
src/main/java/envoy/client/Config.java
Normal 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; }
|
||||||
|
}
|
@ -8,6 +8,7 @@ import java.awt.GridBagConstraints;
|
|||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
@ -20,12 +21,9 @@ import javax.swing.JTextArea;
|
|||||||
import javax.swing.ListSelectionModel;
|
import javax.swing.ListSelectionModel;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
|
|
||||||
import org.apache.http.client.ClientProtocolException;
|
import envoy.client.Client;
|
||||||
import org.xml.sax.SAXException;
|
import envoy.client.Config;
|
||||||
|
|
||||||
import envoy.client.EnvoyClient;
|
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
import envoy.schema.Users;
|
import envoy.schema.Users;
|
||||||
@ -43,9 +41,10 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 6865098428255463649L;
|
private static final long serialVersionUID = 6865098428255463649L;
|
||||||
|
|
||||||
private long recipientID = 0;
|
private long recipientID = 0;
|
||||||
private JPanel contentPane = new JPanel();
|
private JPanel contentPane = new JPanel();
|
||||||
private EnvoyClient envoyClient = new EnvoyClient();
|
|
||||||
|
private static Client client;
|
||||||
|
|
||||||
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
|
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
|
||||||
|
|
||||||
@ -136,8 +135,8 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
// TODO: Acquire proper sender id
|
// TODO: Acquire proper sender id
|
||||||
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
|
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
|
||||||
final Message message = envoyClient.createMessage(1, recipientID, messageEnterTextfield.getText());
|
final Message message = client.createMessage(1, recipientID, messageEnterTextfield.getText());
|
||||||
envoyClient.sendMessage(message);
|
client.sendMessage(message);
|
||||||
appendMessageToChat(message);
|
appendMessageToChat(message);
|
||||||
messageEnterTextfield.setText("");
|
messageEnterTextfield.setText("");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -187,7 +186,7 @@ public class ChatWindow extends JFrame {
|
|||||||
*/
|
*/
|
||||||
private void loadUserList(JList<User> userList) {
|
private void loadUserList(JList<User> userList) {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
Users users = envoyClient.getUsersListXml();
|
Users users = client.getUsersListXml();
|
||||||
DefaultListModel<User> userListModel = new DefaultListModel<>();
|
DefaultListModel<User> userListModel = new DefaultListModel<>();
|
||||||
users.getUser().forEach(user -> userListModel.addElement(user));
|
users.getUser().forEach(user -> userListModel.addElement(user));
|
||||||
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
||||||
@ -213,8 +212,28 @@ public class ChatWindow extends JFrame {
|
|||||||
+ getFirstTextContent(message) + "</span></html>");
|
+ getFirstTextContent(message) + "</span></html>");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) {
|
||||||
throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
|
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(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
try {
|
try {
|
||||||
ChatWindow frame = new ChatWindow();
|
ChatWindow frame = new ChatWindow();
|
||||||
|
Reference in New Issue
Block a user