commit
5610174340
@ -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();
|
||||
}
|
||||
}
|
96
src/main/java/envoy/client/EnvoyClient.java
Normal file
96
src/main/java/envoy/client/EnvoyClient.java
Normal file
@ -0,0 +1,96 @@
|
||||
package envoy.client;
|
||||
|
||||
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.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
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 & Maximilian Käfer</strong>
|
||||
*/
|
||||
|
||||
public class EnvoyClient {
|
||||
|
||||
private DatatypeFactory datatypeFactory;
|
||||
|
||||
public EnvoyClient() {
|
||||
try {
|
||||
datatypeFactory = DatatypeFactory.newInstance();
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 sendMessage(Message message) {
|
||||
new Thread(() -> {
|
||||
// Print message XML to console
|
||||
JAXBContext jc;
|
||||
try {
|
||||
jc = JAXBContext.newInstance("envoy.schema");
|
||||
Marshaller m = jc.createMarshaller();
|
||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
m.marshal(message, System.out);
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Send message
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target("http://kske.feste-ip.net:43315/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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link Message} object serializable to XML.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public Message createMessage(String senderID, String recipientID, String textContent) {
|
||||
ObjectFactory factory = new ObjectFactory();
|
||||
Message.MetaData metaData = factory.createMessageMetaData();
|
||||
metaData.setSender(senderID);
|
||||
metaData.setRecipient(recipientID);
|
||||
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);
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
168
src/main/java/envoy/client/ui/ChatWindow.java
Normal file
168
src/main/java/envoy/client/ui/ChatWindow.java
Normal file
@ -0,0 +1,168 @@
|
||||
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 javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import envoy.client.EnvoyClient;
|
||||
import envoy.schema.Message;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>ChatWindow.java</strong><br>
|
||||
* Created: <strong>28 Sep 2019</strong><br>
|
||||
* Author: <strong>Maximilian Käfer & Kai S. K. Engelbart</strong>
|
||||
*/
|
||||
public class ChatWindow extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 6865098428255463649L;
|
||||
|
||||
private JPanel contentPane = new JPanel();
|
||||
private EnvoyClient envoyClient = new EnvoyClient();
|
||||
|
||||
public DefaultListModel<String> listModel = new DefaultListModel<>();
|
||||
|
||||
public ChatWindow() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 600, 800);
|
||||
setTitle("Envoy");
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
contentPane.setBackground(new Color(0, 0, 0));
|
||||
contentPane.setForeground(Color.white);
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setContentPane(contentPane);
|
||||
GridBagLayout gbl_contentPane = new GridBagLayout();
|
||||
gbl_contentPane.columnWidths = 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.rowWeights = new double[] { 0.05, 1, 0.07 };
|
||||
contentPane.setLayout(gbl_contentPane);
|
||||
|
||||
JList<String> elementList = new JList<>();
|
||||
elementList.setFocusTraversalKeysEnabled(false);
|
||||
|
||||
elementList.setSelectionForeground(new Color(255, 255, 255));
|
||||
elementList.setSelectionBackground(new Color(102, 0, 153));
|
||||
elementList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
|
||||
elementList.setForeground(new Color(255, 255, 255));
|
||||
elementList.setBackground(new Color(51, 51, 51));
|
||||
|
||||
elementList.setModel(listModel);
|
||||
elementList.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||
elementList.setFixedCellHeight(60);
|
||||
elementList.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
|
||||
JScrollPane scrollPane = new JScrollPane();
|
||||
scrollPane.setForeground(new Color(0, 0, 0));
|
||||
scrollPane.setBackground(new Color(51, 51, 51));
|
||||
scrollPane.setViewportView(elementList);
|
||||
scrollPane.setBorder(null);
|
||||
|
||||
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
|
||||
gbc_scrollPane.fill = GridBagConstraints.BOTH;
|
||||
gbc_scrollPane.gridwidth = 2;
|
||||
gbc_scrollPane.gridx = 1;
|
||||
gbc_scrollPane.gridy = 1;
|
||||
|
||||
gbc_scrollPane.insets = new Insets(10, 10, 10, 10);
|
||||
|
||||
contentPane.add(scrollPane, gbc_scrollPane);
|
||||
|
||||
// Message enter field
|
||||
JTextArea messageEnterTextfield = new JTextArea();
|
||||
messageEnterTextfield.setCaretColor(new Color(255, 255, 255));
|
||||
messageEnterTextfield.setForeground(new Color(255, 255, 255));
|
||||
messageEnterTextfield.setBackground(new Color(51, 51, 51));
|
||||
messageEnterTextfield.setLineWrap(true);
|
||||
messageEnterTextfield.setBorder(null);
|
||||
messageEnterTextfield.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||
messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
|
||||
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");
|
||||
postButton.setForeground(new Color(255, 255, 255));
|
||||
postButton.setBackground(new Color(102, 51, 153));
|
||||
postButton.setBorderPainted(false);
|
||||
|
||||
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
|
||||
|
||||
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
|
||||
gbc_moveSelectionPostButton.gridx = 2;
|
||||
gbc_moveSelectionPostButton.gridy = 2;
|
||||
|
||||
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
||||
|
||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||
|
||||
postButton.addActionListener((evt) -> {
|
||||
if (!messageEnterTextfield.getText().isEmpty()) try {
|
||||
final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText());
|
||||
envoyClient.sendMessage(message);
|
||||
appendMessageToChat(message);
|
||||
messageEnterTextfield.setText("");
|
||||
} catch (Exception e) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"An exception occured while sending a message. See the log for more details.",
|
||||
"Exception occured",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the first text content from a message.
|
||||
*
|
||||
* @param message The message from which to return the text content
|
||||
* @return The first content of type 'text'
|
||||
*/
|
||||
public String getFirstTextContent(Message message) {
|
||||
return message.getContent().get(0).getText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a message with sender and message content to the message list.
|
||||
*
|
||||
* @param message The message to append
|
||||
*/
|
||||
private void appendMessageToChat(Message message) {
|
||||
listModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>" + message.getMetaData().getSender()
|
||||
+ "</b> </small>" + "<br>" + "<p style=\"color:white\">" + getFirstTextContent(message)
|
||||
+ "</span></html>");
|
||||
}
|
||||
|
||||
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