Using JAX-RS to retrieve user list
This commit is contained in:
parent
09dba0fa4f
commit
5c495e5bd8
@ -1,12 +1,8 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.ws.rs.client.Client;
|
import javax.ws.rs.client.Client;
|
||||||
@ -19,28 +15,12 @@ import javax.xml.bind.JAXBException;
|
|||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
import javax.xml.datatype.DatatypeConfigurationException;
|
||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
import org.w3c.dom.NodeList;
|
|
||||||
import org.xml.sax.InputSource;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import envoy.client.ui.ChatWindow;
|
import envoy.client.ui.ChatWindow;
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.Messages;
|
import envoy.schema.Messages;
|
||||||
import envoy.schema.User;
|
|
||||||
import envoy.schema.Users;
|
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
|
import envoy.schema.Users;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
@ -144,43 +124,16 @@ public class EnvoyClient {
|
|||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getUsersListXml () throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
|
public Users getUsersListXml() {
|
||||||
|
Client client = ClientBuilder.newClient();
|
||||||
/*HttpClient client = new DefaultHttpClient();
|
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/user",
|
||||||
HttpGet request = new HttpGet(String.format("%s:%s/envoy-server/rest/user",
|
|
||||||
serverProps.getProperty("server"),
|
serverProps.getProperty("server"),
|
||||||
serverProps.getProperty("port")));
|
serverProps.getProperty("port")));
|
||||||
HttpResponse response = client.execute(request);
|
Response response = target.request("application/xml").get();
|
||||||
|
Users users = response.readEntity(Users.class);
|
||||||
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
System.out.println("Response code: " + response.getStatus());
|
||||||
System.out.println(rd.readLine());
|
response.close();
|
||||||
|
client.close();
|
||||||
String inputLine;
|
return users;
|
||||||
StringBuffer response1 = new StringBuffer();
|
|
||||||
while((inputLine = rd.readLine()) != null) {
|
|
||||||
response1.append(inputLine);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
|
||||||
.parse(String.format("%s:%s/envoy-server/rest/user",
|
|
||||||
serverProps.getProperty("server"),
|
|
||||||
serverProps.getProperty("port")));
|
|
||||||
NodeList errNodes = doc.getElementsByTagName("User");
|
|
||||||
|
|
||||||
for (int i = 0; i < errNodes.getLength(); i++) {
|
|
||||||
if(errNodes.getLength() > 0) {
|
|
||||||
Element err = (Element)errNodes.item(i);
|
|
||||||
//System.out.println("Name: " + err.getElementsByTagName("Name").item(0).getTextContent());
|
|
||||||
//System.out.println("Id: " + err.getElementsByTagName("ID").item(0).getTextContent());
|
|
||||||
User user = new User();
|
|
||||||
user.setName(err.getElementsByTagName("Name").item(0).getTextContent());
|
|
||||||
user.setID(Long.parseLong(err.getElementsByTagName("ID").item(0).getTextContent()));
|
|
||||||
chatWindow.addUserToList(user);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
chatWindow.printUserListElements();
|
|
||||||
chatWindow.loadUserJList();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,9 +7,7 @@ import java.awt.Font;
|
|||||||
import java.awt.GridBagConstraints;
|
import java.awt.GridBagConstraints;
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.List;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
@ -19,6 +17,7 @@ import javax.swing.JOptionPane;
|
|||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTextArea;
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
@ -28,6 +27,7 @@ import org.xml.sax.SAXException;
|
|||||||
import envoy.client.EnvoyClient;
|
import envoy.client.EnvoyClient;
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
|
import envoy.schema.Users;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
@ -44,8 +44,6 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
public DefaultListModel<String> listModel = new DefaultListModel<>();
|
public DefaultListModel<String> listModel = new DefaultListModel<>();
|
||||||
|
|
||||||
public ArrayList<User> userList = new ArrayList<User>();
|
|
||||||
|
|
||||||
public ChatWindow() {
|
public ChatWindow() {
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setBounds(100, 100, 600, 800);
|
setBounds(100, 100, 600, 800);
|
||||||
@ -129,8 +127,7 @@ public class ChatWindow extends JFrame {
|
|||||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||||
|
|
||||||
postButton.addActionListener((evt) -> {
|
postButton.addActionListener((evt) -> {
|
||||||
if (!messageEnterTextfield.getText().isEmpty())
|
if (!messageEnterTextfield.getText().isEmpty()) try {
|
||||||
try {
|
|
||||||
final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText());
|
final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText());
|
||||||
envoyClient.sendMessage(message);
|
envoyClient.sendMessage(message);
|
||||||
appendMessageToChat(message);
|
appendMessageToChat(message);
|
||||||
@ -138,20 +135,22 @@ public class ChatWindow extends JFrame {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
JOptionPane.showMessageDialog(this,
|
JOptionPane.showMessageDialog(this,
|
||||||
"An exception occured while sending a message. See the log for more details.",
|
"An exception occured while sending a message. See the log for more details.",
|
||||||
"Exception occured", JOptionPane.ERROR_MESSAGE);
|
"Exception occured",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
new Thread(() -> loadUserJList()).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadUserJList() {
|
public void loadUserJList() {
|
||||||
|
Users users = envoyClient.getUsersListXml();
|
||||||
|
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
// User List
|
// User List
|
||||||
JList<User> userJList = new JList<>();
|
JList<User> userJList = new JList<>();
|
||||||
DefaultListModel<User> userJListModel = new DefaultListModel<>();
|
DefaultListModel<User> userJListModel = new DefaultListModel<>();
|
||||||
for (int i = 0; i < userList.size(); i++) {
|
users.getUser().forEach(user -> userJListModel.addElement(user));
|
||||||
userJListModel.addElement(userList.get(i));
|
|
||||||
}
|
|
||||||
userJList.setModel(userJListModel);
|
userJList.setModel(userJListModel);
|
||||||
|
|
||||||
GridBagConstraints gbc_userList = new GridBagConstraints();
|
GridBagConstraints gbc_userList = new GridBagConstraints();
|
||||||
@ -164,6 +163,7 @@ public class ChatWindow extends JFrame {
|
|||||||
System.out.println("test");
|
System.out.println("test");
|
||||||
|
|
||||||
contentPane.add(userJList, gbc_userList);
|
contentPane.add(userJList, gbc_userList);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -172,9 +172,7 @@ public class ChatWindow extends JFrame {
|
|||||||
* @param message The message from which to return the text content
|
* @param message The message from which to return the text content
|
||||||
* @return The first content of type 'text'
|
* @return The first content of type 'text'
|
||||||
*/
|
*/
|
||||||
public String getFirstTextContent(Message message) {
|
public String getFirstTextContent(Message message) { return message.getContent().get(0).getText(); }
|
||||||
return message.getContent().get(0).getText();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends a message with sender and message content to the message list.
|
* Appends a message with sender and message content to the message list.
|
||||||
@ -187,18 +185,6 @@ public class ChatWindow extends JFrame {
|
|||||||
+ "</span></html>");
|
+ "</span></html>");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addUserToList(User user) {
|
|
||||||
userList.add(user);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void printUserListElements() {
|
|
||||||
for (int i = 0; i < userList.size(); i++) {
|
|
||||||
System.out.println(userList.get(i).getName());
|
|
||||||
System.out.println(userList.get(i).getID());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
|
throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
|
||||||
EventQueue.invokeLater(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
@ -209,7 +195,5 @@ public class ChatWindow extends JFrame {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
envoyClient.getUsersListXml();
|
|
||||||
System.out.println("asd");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user