Moved main method to ChatWindow, made message sending asynchronous
This commit is contained in:
parent
b0d4416053
commit
37ebc77d71
@ -1,47 +0,0 @@
|
|||||||
package envoy;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
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.JAXBException;
|
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
|
||||||
import javax.xml.datatype.DatatypeFactory;
|
|
||||||
|
|
||||||
import envoy.schema.Message;
|
|
||||||
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</strong>
|
|
||||||
*/
|
|
||||||
public class EnvoyClient {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws DatatypeConfigurationException, JAXBException {
|
|
||||||
ObjectFactory factory = new ObjectFactory();
|
|
||||||
|
|
||||||
Message.MetaData metaData = factory.createMessageMetaData();
|
|
||||||
metaData.setSender("Kai");
|
|
||||||
metaData.setRecipient("Maxi");
|
|
||||||
metaData.setState(false);
|
|
||||||
metaData.setDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(Instant.now().toString()));
|
|
||||||
|
|
||||||
Message.Content content = factory.createMessageContent();
|
|
||||||
content.setType("text");
|
|
||||||
content.setText("Hello, World");
|
|
||||||
|
|
||||||
Message message = factory.createMessage();
|
|
||||||
message.setMetaData(metaData);
|
|
||||||
message.getContent().add(content);
|
|
||||||
|
|
||||||
Client client = ClientBuilder.newClient();
|
|
||||||
WebTarget target = client.target("http://localhost:8080/envoy-server/rest/message/send");
|
|
||||||
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
|
||||||
response.close();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,5 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.awt.EventQueue;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
import javax.ws.rs.client.Client;
|
import javax.ws.rs.client.Client;
|
||||||
@ -10,11 +7,9 @@ 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;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
import javax.xml.datatype.DatatypeConfigurationException;
|
||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
|
|
||||||
import envoy.client.ui.ChatWindow;
|
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
|
|
||||||
@ -22,68 +17,54 @@ import envoy.schema.ObjectFactory;
|
|||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>EnvoyClient.java</strong><br>
|
* File: <strong>EnvoyClient.java</strong><br>
|
||||||
* Created: <strong>28 Sep 2019</strong><br>
|
* Created: <strong>28 Sep 2019</strong><br>
|
||||||
* Author: <strong>Kai S. K. Engelbart and Maximilian Käfer</strong>
|
* Author: <strong>Kai S. K. Engelbart & Maximilian Käfer</strong>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class EnvoyClient {
|
public class EnvoyClient {
|
||||||
|
|
||||||
public static String content1 = "";
|
private DatatypeFactory datatypeFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls Class ChatWidow
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) throws DatatypeConfigurationException, JAXBException {
|
|
||||||
EventQueue.invokeLater(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
ChatWindow frame = new ChatWindow();
|
|
||||||
frame.setVisible(true);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* specifies data <br>
|
|
||||||
* sends xml to server <br>
|
|
||||||
* gets response from server (currently 204)
|
|
||||||
*/
|
|
||||||
public static void sendMessage() throws DatatypeConfigurationException, JAXBException {
|
|
||||||
|
|
||||||
ObjectFactory factory = new ObjectFactory();
|
|
||||||
|
|
||||||
Message.MetaData metaData = factory.createMessageMetaData();
|
|
||||||
metaData.setSender("Kai");
|
|
||||||
metaData.setRecipient("Maxi");
|
|
||||||
metaData.setState(false);
|
|
||||||
metaData.setDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(Instant.now().toString()));
|
|
||||||
|
|
||||||
Message.Content content = factory.createMessageContent();
|
public EnvoyClient() {
|
||||||
content.setType("text");
|
try {
|
||||||
content.setText(content1);
|
datatypeFactory = DatatypeFactory.newInstance();
|
||||||
|
} catch (DatatypeConfigurationException e) {
|
||||||
Message message = factory.createMessage();
|
e.printStackTrace();
|
||||||
message.setMetaData(metaData);
|
}
|
||||||
message.getContent().add(content);
|
|
||||||
|
|
||||||
Client client = ClientBuilder.newClient();
|
|
||||||
WebTarget target = client.target("http://localhost:8080/envoy-server/rest/message/send");
|
|
||||||
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
|
||||||
System.out.println("Response code: " + response.getStatus());
|
|
||||||
response.close();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sets content of xml to content from ChatWindow
|
* 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 setContent (String content2) {
|
public void sendMessage(String sender, String recipient, String textContent) {
|
||||||
content1 = content2;
|
new Thread(() -> {
|
||||||
|
ObjectFactory factory = new ObjectFactory();
|
||||||
|
|
||||||
|
Message.MetaData metaData = factory.createMessageMetaData();
|
||||||
|
metaData.setSender(sender);
|
||||||
|
metaData.setRecipient(recipient);
|
||||||
|
metaData.setState(false);
|
||||||
|
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||||
|
|
||||||
|
Message.Content content = factory.createMessageContent();
|
||||||
|
content.setType("text");
|
||||||
|
content.setText(textContent);
|
||||||
|
|
||||||
|
Message message = factory.createMessage();
|
||||||
|
message.setMetaData(metaData);
|
||||||
|
message.getContent().add(content);
|
||||||
|
|
||||||
|
Client client = ClientBuilder.newClient();
|
||||||
|
WebTarget target = client.target("http://localhost:8080/envoy-server/rest/message/send");
|
||||||
|
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
||||||
|
System.out.println("Response code: " + response.getStatus());
|
||||||
|
response.close();
|
||||||
|
client.close();
|
||||||
|
}).start();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
@ -1,108 +1,99 @@
|
|||||||
package envoy.client.ui;
|
package envoy.client.ui;
|
||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.Color;
|
||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.JScrollPane;
|
|
||||||
import javax.swing.JTextArea;
|
|
||||||
import javax.swing.JTextPane;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
|
||||||
|
|
||||||
import envoy.client.EnvoyClient;
|
|
||||||
|
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import java.awt.GridBagConstraints;
|
import javax.swing.JFrame;
|
||||||
import java.awt.Color;
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
|
import envoy.client.EnvoyClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>ChatWindow.java</strong><br>
|
* File: <strong>ChatWindow.java</strong><br>
|
||||||
* Created: <strong>28 Sep 2019</strong><br>
|
* Created: <strong>28 Sep 2019</strong><br>
|
||||||
* Author: <strong>Maximilian Käfer </strong>
|
* Author: <strong>Maximilian Käfer & Kai S. K. Engelbart</strong>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ChatWindow extends JFrame {
|
public class ChatWindow extends JFrame {
|
||||||
|
|
||||||
private JPanel contentPane;
|
private static final long serialVersionUID = 6865098428255463649L;
|
||||||
EnvoyClient envoyClient = new EnvoyClient();
|
|
||||||
|
private JPanel contentPane = new JPanel();
|
||||||
|
private EnvoyClient envoyClient = new EnvoyClient();
|
||||||
|
|
||||||
public ChatWindow() {
|
public ChatWindow() {
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setBounds(100, 100, 600, 800);
|
setBounds(100, 100, 600, 800);
|
||||||
contentPane = new JPanel();
|
setTitle("Envoy");
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
contentPane.setBackground(new Color(220, 220, 220));
|
contentPane.setBackground(new Color(220, 220, 220));
|
||||||
contentPane.setForeground(new Color(0, 0, 0));
|
contentPane.setForeground(Color.white);
|
||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
GridBagLayout gbl_contentPane = new GridBagLayout();
|
GridBagLayout gbl_contentPane = new GridBagLayout();
|
||||||
gbl_contentPane.columnWidths = new int[]{1, 1, 1};
|
gbl_contentPane.columnWidths = new int[] { 1, 1, 1 };
|
||||||
gbl_contentPane.rowHeights = new int[]{1, 1, 1};
|
gbl_contentPane.rowHeights = new int[] { 1, 1, 1 };
|
||||||
gbl_contentPane.columnWeights = new double[]{0.3, 1.0, 0.1};
|
gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 };
|
||||||
gbl_contentPane.rowWeights = new double[]{0.05, 1, 0.07};
|
gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 };
|
||||||
contentPane.setLayout(gbl_contentPane);
|
contentPane.setLayout(gbl_contentPane);
|
||||||
|
|
||||||
|
|
||||||
// Message enter field ----------------------------------------------------------------------------
|
|
||||||
JTextArea messageEnterTextfield = new JTextArea();
|
|
||||||
messageEnterTextfield.setLineWrap(true);
|
|
||||||
|
|
||||||
GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints();
|
|
||||||
gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH;
|
|
||||||
gbc_moveSelectionMessageEnterTextfield.gridx = 1;
|
|
||||||
gbc_moveSelectionMessageEnterTextfield.gridy = 2;
|
|
||||||
|
|
||||||
//gbc_moveSelectionMessageEnterTextfield.gridwidth = 10;
|
|
||||||
|
|
||||||
gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10);
|
|
||||||
|
|
||||||
contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
|
|
||||||
|
|
||||||
|
// Message enter field
|
||||||
// Post Button -----------------------------------------------------------------------------------
|
JTextArea messageEnterTextfield = new JTextArea();
|
||||||
|
messageEnterTextfield.setLineWrap(true);
|
||||||
|
|
||||||
|
GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints();
|
||||||
|
gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_moveSelectionMessageEnterTextfield.gridx = 1;
|
||||||
|
gbc_moveSelectionMessageEnterTextfield.gridy = 2;
|
||||||
|
|
||||||
|
gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10);
|
||||||
|
|
||||||
|
contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
|
||||||
|
|
||||||
|
// Post Button
|
||||||
JButton postButton = new JButton("Post");
|
JButton postButton = new JButton("Post");
|
||||||
postButton.setForeground(new Color(255, 255, 255));
|
postButton.setForeground(new Color(255, 255, 255));
|
||||||
postButton.setBackground(new Color(0, 100, 0));
|
postButton.setBackground(new Color(0, 100, 0));
|
||||||
|
|
||||||
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
|
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
|
||||||
|
|
||||||
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
|
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
|
||||||
gbc_moveSelectionPostButton.gridx = 2;
|
gbc_moveSelectionPostButton.gridx = 2;
|
||||||
gbc_moveSelectionPostButton.gridy = 2;
|
gbc_moveSelectionPostButton.gridy = 2;
|
||||||
|
|
||||||
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
||||||
|
|
||||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||||
|
|
||||||
|
postButton.addActionListener((evt) -> {
|
||||||
postButton.addActionListener(new ActionListener() {
|
if (!messageEnterTextfield.getText().isEmpty()) try {
|
||||||
public void actionPerformed(ActionEvent e) {
|
envoyClient.sendMessage("Kai", "Maxi", messageEnterTextfield.getText());
|
||||||
try {
|
} catch (Exception e) {
|
||||||
if(messageEnterTextfield.getText().isEmpty() == false) {
|
JOptionPane.showMessageDialog(this,
|
||||||
envoyClient.setContent(messageEnterTextfield.getText());
|
"An exception occured while sending a message. See the log for more details.",
|
||||||
envoyClient.sendMessage();
|
"Exception occured",
|
||||||
}
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
e.printStackTrace();
|
||||||
} catch (DatatypeConfigurationException | JAXBException e1) {
|
}
|
||||||
// TODO Auto-generated catch block
|
});
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public static void main(String[] args) {
|
||||||
|
EventQueue.invokeLater(() -> {
|
||||||
|
try {
|
||||||
|
ChatWindow frame = new ChatWindow();
|
||||||
|
frame.setVisible(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user