From b1467a5cc381d5935a1dda96c858dc1c0f512d47 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 26 Oct 2019 18:31:49 +0200 Subject: [PATCH] Moved GET request implementations into a single method --- src/main/java/envoy/client/Client.java | 66 +++++++++++++------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index ed8ebb1..0ebce3c 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -60,7 +60,7 @@ public class Client { public void sendMessage(Message message) { new Thread(() -> { // Wrap single message into messages list - Messages messages = wrapMessage(message); + Messages messages = wrapMessages(message); // Print message XML to console JAXBContext jc; @@ -109,22 +109,10 @@ public class Client { return message; } - public Messages wrapMessage(Message... messages) { - Messages wrapper = objectFactory.createMessages(); - wrapper.getMessage().addAll(Arrays.asList(messages)); - return wrapper; - } + public Users getUsersListXml() { return get(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()), Users.class); } - public Users getUsersListXml() { - javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client - .target(String.format("%s:%d/envoy-server/rest/user", 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; + public Messages getUnreadMessages(long userId) { + return get(String.format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId), Messages.class); } /** @@ -132,35 +120,49 @@ public class Client { * * @param name - the name of the {@link User} * @return a {@link User} with the specified name - * @since Envoy 0.1 + * @since Envoy v0.1-alpha */ 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=%s", config.getServer(), config.getPort(), name)); - 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); + return get(String.format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name), Users.class).getUser() + .get(0); } - public Messages getUnreadMessages(long userId) { + /** + * Invokes the GET method of a web service. + * + * @param the type of the object returned by the web service + * @param uri the URI of the web service + * @param responseClass the class of the object returned by the web service + * @return the object returned by the web service + * @since Envoy v0.1-alpha + */ + private T get(String uri, Class responseClass) { javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client.target(String - .format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId)); + WebTarget target = client.target(uri); Response response = target.request("application/xml").get(); - Messages unreadMessages = response.readEntity(Messages.class); + T responseObject = response.readEntity(responseClass); System.out.println("Response code: " + response.getStatus()); response.close(); client.close(); - return unreadMessages; + return responseObject; + } + + /** + * Wraps one or more {@link Message} objects into a {@link Messages} object. + * + * @param messages the {@link Message} objects to wrap + * @return {@link Messages} object with all messages as its children + * @since Envoy v0.1-alpha + */ + private Messages wrapMessages(Message... messages) { + Messages wrapper = objectFactory.createMessages(); + wrapper.getMessage().addAll(Arrays.asList(messages)); + return wrapper; } /** * @return the sender object that represents this client - * @since Envoy 0.1 + * @since Envoy v0.1-alpha */ public User getSender() { return sender; }