diff --git a/Client/Client.iml b/Client/Client.iml
index c3ea736..643da58 100644
--- a/Client/Client.iml
+++ b/Client/Client.iml
@@ -1,18 +1,22 @@
-
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Client/pom.xml b/Client/pom.xml
index ff427b8..64cb159 100644
--- a/Client/pom.xml
+++ b/Client/pom.xml
@@ -11,5 +11,17 @@
Client
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.10
+
+
+ org.codehaus.jackson
+ jackson-mapper-asl
+ 1.9.13
+
+
\ No newline at end of file
diff --git a/Client/src/main/java/controllers/IdController.java b/Client/src/main/java/controllers/IdController.java
index 15b8253..8375f8d 100644
--- a/Client/src/main/java/controllers/IdController.java
+++ b/Client/src/main/java/controllers/IdController.java
@@ -1,22 +1,57 @@
package controllers;
+import java.io.IOException;
import java.util.ArrayList;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
import models.Id;
+import org.codehaus.jackson.map.ObjectMapper;
public class IdController {
Id myId;
public ArrayList getIds() {
- return null;
+ ArrayList idsList = new ArrayList();
+ String json = TransactionController.MakeURLCall("/ids", "GET", "");
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+ Id[] ids = mapper.readValue(json, Id[].class);
+ //parameter is json which takes in all url calls.
+ for(int i = 0; i < ids.length; i++){
+ idsList.add(ids[i]);
+ }
+ return idsList;
+ }catch (IOException i){
+ i.printStackTrace();
+ return null;
+ }
}
public Id postId(Id id) {
- return null;
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ String json = mapper.writeValueAsString(id);
+ String response = TransactionController.MakeURLCall("/ids", "POST", json);
+ return mapper.readValue(response, Id.class);
+ }catch (IOException i){
+ i.printStackTrace();
+ return null;
+ }
}
public Id putId(Id id) {
- return null;
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ String json = mapper.writeValueAsString(id);
+ String response = TransactionController.MakeURLCall("/ids", "PUT", json);
+ return mapper.readValue(response, Id.class);
+ }catch (IOException i){
+ i.printStackTrace();
+ return null;
+ }
+ }
+ public Id getIdByGit(String githubid){
+ return getIds().stream().filter(id -> id.getGithub().equals(githubid)).collect(Collectors.toList()).get(0);
}
-
}
\ No newline at end of file
diff --git a/Client/src/main/java/controllers/MessageController.java b/Client/src/main/java/controllers/MessageController.java
index f9462ed..f9222f4 100644
--- a/Client/src/main/java/controllers/MessageController.java
+++ b/Client/src/main/java/controllers/MessageController.java
@@ -1,10 +1,13 @@
package controllers;
-import java.util.ArrayList;
-import java.util.HashSet;
-
import models.Id;
import models.Message;
+import org.codehaus.jackson.map.ObjectMapper;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.stream.Collectors;
public class MessageController {
@@ -12,20 +15,46 @@ public class MessageController {
// why a HashSet??
public ArrayList getMessages() {
- return null;
+ ArrayList messagesList = new ArrayList();
+ String json = TransactionController.MakeURLCall("/messages", "GET", "");
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+ Message[] messages = mapper.readValue(json, Message[].class);
+ for (int i = 0; i < messages.length; i++) {
+ messagesList.add(messages[i]);
+ }
+ return messagesList;
+ } catch (IOException i) {
+ i.printStackTrace();
+ return null;
+ }
}
- public ArrayList getMessagesForId(Id Id) {
- return null;
+
+ public ArrayList getMessagesForId(Id id) {
+ return (ArrayList) getMessages().stream().filter(m -> m.getfromid().equals(id.getGithub())).collect(Collectors.toList());
}
+
public Message getMessageForSequence(String seq) {
- return null;
+ return getMessages().stream().filter(m -> m.getSequence().equals(seq)).collect(Collectors.toList()).get(0);
}
public ArrayList getMessagesFromFriend(Id myId, Id friendId) {
- return null;
+ return (ArrayList) getMessages().stream()
+ .filter(m ->
+ (m.getfromid().equals(myId.getGithub())) && m.gettoid().equals(friendId.getGithub()))
+ .collect(Collectors.toList());
}
- public Message postMessage(Id myId, Id toId, Message msg) {
- return null;
+ public Message postMessage(String myId, String toId, String msg) {
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ String json = mapper.writeValueAsString(new Message(msg, myId, toId));
+ String mainUrl = String.format("/ids/%s/messages", myId);
+ String response = TransactionController.MakeURLCall(mainUrl, "POST", json);
+ return mapper.readValue(response, Message.class);
+ }catch (IOException i){
+ i.printStackTrace();
+ return null;
+ }
}
-
+
}
\ No newline at end of file
diff --git a/Client/src/main/java/controllers/TransactionController.java b/Client/src/main/java/controllers/TransactionController.java
index acacc62..756f199 100644
--- a/Client/src/main/java/controllers/TransactionController.java
+++ b/Client/src/main/java/controllers/TransactionController.java
@@ -1,7 +1,76 @@
package controllers;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
public class TransactionController {
- private String rootURL = "http://zipcode.rocks:8085";
+ private static final String rootURL = "http://zipcode.rocks:8085";
public TransactionController() {}
+
+ public static String MakeURLCall(String mainurl, String method, String jpayload){
+ String response = "";
+ try{
+ HttpURLConnection conn = initConnection(mainurl, method, jpayload);
+ response = readServerResponse(conn);
+ conn.disconnect();
+ //closes connection to server
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return response;
+ }
+
+ public static HttpURLConnection initConnection(String mainurl, String method, String jpayload){
+ try{
+ URL url = new URL(rootURL + mainurl);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+
+ conn.setDoOutput(true);
+ conn.setRequestMethod(method);
+ if(method.equals("POST") || method.equals("PUT")){
+ conn = writeJsonToConn(conn, jpayload);
+ }
+
+ return conn;
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public static String readServerResponse(HttpURLConnection conn){
+ //takes server output and puts it into a string
+ StringBuilder result = new StringBuilder();
+ try{
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ //conn.getInputStream is the input but InputStreamReader reads that input
+ String output;
+ while ((output = br.readLine()) != null) {
+ result.append(output);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return result.toString();
+ }
+
+ public static HttpURLConnection writeJsonToConn(HttpURLConnection conn, String jpayload){
+ conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
+ conn.setRequestProperty("Accept", "application/json");
+ try{
+ OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream());
+ os.write(jpayload);
+ os.flush();;
+ os.close();
+ //conn is an open connection to the server running at the time in question
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return conn;
+ }
}
diff --git a/Client/src/main/java/models/Id.java b/Client/src/main/java/models/Id.java
index 71f2ebf..74375d0 100644
--- a/Client/src/main/java/models/Id.java
+++ b/Client/src/main/java/models/Id.java
@@ -4,7 +4,50 @@
* POJO for an Id object
*/
public class Id {
-
- public Id (String name, String githubId) {}
+ String name;
+ String github;
+ String userid;
+ private Id(){}
+ //looks like this isnt used but required for object mapper
+
+ public Id (String name, String githubId) {
+ this.name = name;
+ this.github = githubId;
+ this.userid = null;
+ }
+
+ public Id (String name, String githubId, String userid) {
+ this.name = name;
+ this.github = githubId;
+ this.userid = userid;
+ }
+
+ public Id (Id id){
+ this(id.getName(), id.getGithub(), id.getUserid());
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getGithub() {
+ return github;
+ }
+
+ public void setGithub(String github) {
+ this.github = github;
+ }
+
+ public String getUserid() {
+ return userid;
+ }
+
+ public void setUserid(String userid) {
+ this.userid = userid;
+ }
}
\ No newline at end of file
diff --git a/Client/src/main/java/models/Message.java b/Client/src/main/java/models/Message.java
index da313ae..5063521 100644
--- a/Client/src/main/java/models/Message.java
+++ b/Client/src/main/java/models/Message.java
@@ -4,7 +4,72 @@
* POJO for an Message object
*/
public class Message {
+ private String message;
+ private String fromid;
+ private String toid;
+ private String sequence;
+ private String timestamp;
- public Message (String message, String fromId, String toId) {}
+ public Message(String message, String fromid, String toid, String sequence, String timestamp){
+ this.message = message;
+ this.fromid = fromid;
+ this.toid = toid;
+ this.sequence = sequence;
+ this.timestamp = timestamp;
+ }
+
+ private Message(){}
+ //looks like this isnt used but required for object mapper
+
+ public Message(String message, String fromid, String toid){
+ this(message, fromid, toid, "-", null);
+ }
+
+ public Message(Message message){
+ this(message.getMessage(), message.getfromid(), message.gettoid(), message.getSequence(), message.getTimestamp());
+ }
+
+ public String getSequence() {
+ return sequence;
+ }
+
+ public void setSequence(String sequence) {
+ this.sequence = sequence;
+ }
+
+ public String getTimestamp() {
+ return timestamp;
+ }
+
+ public void setTimestamp(String timestamp) {
+ this.timestamp = timestamp;
+ }
+ // public Message (String message, String fromId, String toId) {
+//
+// }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public String getfromid() {
+ return fromid;
+ }
+
+ public void setfromid(String fromid) {
+ this.fromid = fromid;
+ }
+
+ public String gettoid() {
+ return toid;
+ }
+
+ public void settoid(String toid) {
+ this.toid = toid;
+ }
}
\ No newline at end of file
diff --git a/Client/src/main/java/utils/HelperMethods.java b/Client/src/main/java/utils/HelperMethods.java
new file mode 100644
index 0000000..ac5068f
--- /dev/null
+++ b/Client/src/main/java/utils/HelperMethods.java
@@ -0,0 +1,43 @@
+package utils;
+
+import models.Id;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class HelperMethods {
+ public static String getId(ArrayList ids, String github){
+ String userid = "";
+
+ for(Id id : ids){
+ if(id.getGithub().equals(github)){
+ userid = id.getUserid();
+ break;
+ }
+ }
+ return userid;
+ }
+
+ public static String buildMessage(String commandLine) {
+ return commandLine.split("'")[1];
+ }
+ //another way to do the build message but the above is more efficient and below just doesnt work that well
+// public static String buildMessage(List list){
+// StringBuilder sb = new StringBuilder();
+// Boolean inMsg = false;
+// for(String item : list){
+// if(item.charAt(0) == '\''){
+// sb.append(item);
+// inMsg = true;
+// }else if(inMsg){
+// sb.append(" " + item);
+// }else if (item.charAt(item.length()-1) == '\''){
+// inMsg = false;
+// sb.append(item);
+// break;
+// }
+// }
+// String output = sb.toString();
+// return output.substring(1, output.length()-1);
+// }
+}
diff --git a/Client/src/main/java/views/IdTextView.java b/Client/src/main/java/views/IdTextView.java
index e61c754..c640711 100644
--- a/Client/src/main/java/views/IdTextView.java
+++ b/Client/src/main/java/views/IdTextView.java
@@ -3,11 +3,14 @@
import models.Id;
public class IdTextView {
+ Id id;
public IdTextView(Id idToDisplay) {
-
+ this.id = idToDisplay;
}
@Override public String toString() {
- return null;
+ return String.format("User:\n" + "\tname:\t\t%s\n" +
+ "\tgithub:\t\t%s\n" + "\tuserid:\t\t%s\n",
+ id.getName(), id.getGithub(), id.getUserid());
}
}
\ No newline at end of file
diff --git a/Client/src/main/java/views/MessageTextView.java b/Client/src/main/java/views/MessageTextView.java
index fe96921..0f6d414 100644
--- a/Client/src/main/java/views/MessageTextView.java
+++ b/Client/src/main/java/views/MessageTextView.java
@@ -3,11 +3,16 @@
import models.Message;
public class MessageTextView {
+Message message;
public MessageTextView(Message msgToDisplay) {
-
+ this.message = msgToDisplay;
}
@Override public String toString() {
- return null;
+ return String.format("Message:\n" + "\tfromid:\t\t%s\n" +
+ "\ttoid:\t\t%s\n" + "\tmessage:\t\t%s\n" +
+ "\ttimestamp:\t%s\n" + "\tsequence:\\t\\t%s\\n",
+ message.getfromid(), message.gettoid(), message.getMessage(),
+ message.getTimestamp(), message.getSequence());
}
}
\ No newline at end of file
diff --git a/Client/src/main/java/views/SimpleShell.java b/Client/src/main/java/views/SimpleShell.java
index 3865bb2..9afe268 100644
--- a/Client/src/main/java/views/SimpleShell.java
+++ b/Client/src/main/java/views/SimpleShell.java
@@ -4,20 +4,33 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
+import com.fasterxml.jackson.databind.ObjectMapper;
import controllers.IdController;
import controllers.MessageController;
+import models.Id;
+import models.Message;
import youareell.YouAreEll;
// Simple Shell is a Console view for youareell.YouAreEll.
public class SimpleShell {
- public static void prettyPrint(String output) {
- // yep, make an effort to format things nicely, eh?
- System.out.println(output);
+ public static void prettyPrintId(ArrayList ids) {
+ for(Id id : ids) {
+ IdTextView itv = new IdTextView(id);
+ System.out.println(itv.toString());
+ }
+ }
+
+ public static void prettyPrintMsg(ArrayList message) {
+ for (Message msg : message) {
+ MessageTextView mtv = new MessageTextView(msg);
+ System.out.println(mtv.toString());
+ }
}
public static void main(String[] args) throws java.io.IOException {
@@ -68,15 +81,21 @@ public static void main(String[] args) throws java.io.IOException {
// ids
if (list.contains("ids")) {
- String results = webber.get_ids();
- SimpleShell.prettyPrint(results);
+ ArrayList ids = webber.interpretIds(list);
+ SimpleShell.prettyPrintId(ids);
continue;
}
// messages
if (list.contains("messages")) {
- String results = webber.get_messages();
- SimpleShell.prettyPrint(results);
+ ArrayList messages = webber.interpretMessages(list);
+ SimpleShell.prettyPrintMsg(messages);
+ continue;
+ }
+
+ if(list.contains("send")){
+ ArrayList messages = webber.interpretSendMessage(list, commandLine);
+ SimpleShell.prettyPrintMsg(messages);
continue;
}
// you need to add a bunch more.
diff --git a/Client/src/main/java/youareell/YouAreEll.java b/Client/src/main/java/youareell/YouAreEll.java
index acfe78a..a8ed302 100644
--- a/Client/src/main/java/youareell/YouAreEll.java
+++ b/Client/src/main/java/youareell/YouAreEll.java
@@ -1,6 +1,23 @@
package youareell;
+
+import com.sun.tools.corba.se.idl.toJavaPortable.Helper;
import controllers.*;
+import models.Id;
+import models.Message;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import utils.HelperMethods;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
public class YouAreEll {
@@ -20,15 +37,86 @@ public static void main(String[] args) {
System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
}
- public String get_ids() {
- return MakeURLCall("/ids", "GET", "");
+// public String get_ids() {
+// return MakeURLCall("/ids", "GET", "");
+// }
+//
+// public String get_messages() {
+// return MakeURLCall("/messages", "GET", "");
+// }
+
+ public String MakeURLCall(String mainurl, String method, String jpayload) {
+ String result = "";
+ HttpGet request = new HttpGet("http://zipcode.rocks:8085"+mainurl);
+ final CloseableHttpClient httpClient = HttpClients.createDefault();
+ try{
+ CloseableHttpResponse response = httpClient.execute(request); {
+
+ // Get HttpResponse Status
+ System.out.println(response.getStatusLine().toString());
+
+ HttpEntity entity = response.getEntity();
+ Header headers = entity.getContentType();
+ System.out.println(headers);
+
+ if (entity != null) {
+ // return it as a String
+ result = EntityUtils.toString(entity);
+ System.out.println(result);
+ }
+
+ }
+ }catch (ClientProtocolException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+
+ return result;
}
- public String get_messages() {
- return MakeURLCall("/messages", "GET", "");
+ public ArrayList interpretIds (List list){
+ ArrayList idsList = idCtrl.getIds();
+
+ if(list.get(0).equals("ids") && list.size() == 3){
+ String userId = HelperMethods.getId(idsList, list.get(2));
+ idsList = new ArrayList();
+
+ if (!userId.equals(""))
+ idsList.add(idCtrl.putId(new Id(list.get(1), list.get(2), userId)));
+ else
+ idsList.add(idCtrl.postId(new Id(list.get(1), list.get(2))));
+ }
+ return idsList;
}
- public String MakeURLCall(String mainurl, String method, String jpayload) {
- return "nada";
+ public ArrayList interpretMessages (List list){
+ ArrayList messages = msgCtrl.getMessages();
+
+ if(list.size() == 3 && list.get(1).equals("seq")){
+ messages = new ArrayList<>();
+ messages.add(msgCtrl.getMessageForSequence(list.get(2)));
+ }else if (list.size() == 2){
+ Id myId = idCtrl.getIdByGit(list.get(1));
+ messages = msgCtrl.getMessagesForId(myId);
+ }else if (list.size() == 3){
+ Id myId = idCtrl.getIdByGit(list.get(1));
+ Id friendId = idCtrl.getIdByGit(list.get(2));
+ messages = msgCtrl.getMessagesFromFriend(myId, friendId);
+ }
+ return messages;
+ }
+
+ public ArrayList interpretSendMessage(List list, String commandLine){
+ ArrayList messages = new ArrayList<>();
+ String message = HelperMethods.buildMessage(commandLine);
+
+ if(commandLine.matches("send [A-Za-z0-9]+ '[A-Za-z0-9,. ]+'")){
+ messages.add(msgCtrl.postMessage(list.get(1), "", message));
+ }else if (commandLine.matches("send [A-Za-z0-9]+ '[A-Za-z0-9,. ]+' to [A-Za-z0-9]+")){
+ messages.add(msgCtrl.postMessage(list.get(1), list.get(list.size()-1), message));
+ }
+ return messages;
}
}
diff --git a/YouAreEll.iml b/YouAreEll.iml
index 5f1ec7b..084de76 100644
--- a/YouAreEll.iml
+++ b/YouAreEll.iml
@@ -8,8 +8,8 @@
-
-
-
+
+
+
\ No newline at end of file