Browse Source

cmd works, but have to reformat with Pattern class

Bo Lee 6 years ago
parent
commit
48723bff62

+ 25
- 29
Client/src/main/java/Message.java View File

1
-import com.mashape.unirest.http.HttpResponse;
2
-import com.mashape.unirest.http.Unirest;
3
 import com.mashape.unirest.http.exceptions.UnirestException;
1
 import com.mashape.unirest.http.exceptions.UnirestException;
4
 
2
 
5
-import java.sql.Time;
6
-
7
 public class Message {
3
 public class Message {
8
 
4
 
9
 
5
 
10
     //final HttpResponse<String> MESSAGES = Unirest.get("http://zipcode.rocks:8085/messages").asString();
6
     //final HttpResponse<String> MESSAGES = Unirest.get("http://zipcode.rocks:8085/messages").asString();
11
 
7
 
12
     private String sequence;
8
     private String sequence;
13
-    private String timeStamp;
14
-    private String fromId;
15
-    private String toId;
9
+    private String timestamp;
10
+    private String fromid;
11
+    private String toid;
16
     private String message;
12
     private String message;
17
 
13
 
18
 
14
 
20
     public Message() throws UnirestException {
16
     public Message() throws UnirestException {
21
     }
17
     }
22
 
18
 
23
-    public Message(String sequence, String timeStamp, String fromId, String toId, String message) {
19
+    public Message(String sequence, String timestamp, String fromId, String toid, String message) {
24
         this.sequence = sequence;
20
         this.sequence = sequence;
25
-        this.timeStamp = timeStamp;
26
-        this.fromId = fromId;
27
-        this.toId = toId;
21
+        this.timestamp = timestamp;
22
+        this.fromid = fromId;
23
+        this.toid = toid;
28
         this.message = message;
24
         this.message = message;
29
     }
25
     }
30
 
26
 
31
-    public Message(String fromId, String toId, String message) {
32
-        this.fromId = fromId;
33
-        this.toId = toId;
27
+    public Message(String fromId, String toid, String message) {
28
+        this.fromid = fromId;
29
+        this.toid = toid;
34
         this.message = message;
30
         this.message = message;
35
     }
31
     }
36
 
32
 
42
         this.sequence = sequence;
38
         this.sequence = sequence;
43
     }
39
     }
44
 
40
 
45
-    public String getTimeStamp() {
46
-        return timeStamp;
41
+    public String getTimestamp() {
42
+        return timestamp;
47
     }
43
     }
48
 
44
 
49
-    public void setTimeStamp(String timeStamp) {
50
-        this.timeStamp = timeStamp;
45
+    public void setTimestamp(String timestamp) {
46
+        this.timestamp = timestamp;
51
     }
47
     }
52
 
48
 
53
-    public String getFromId() {
54
-        return fromId;
49
+    public String getFromid() {
50
+        return fromid;
55
     }
51
     }
56
 
52
 
57
-    public void setFromId(String fromId) {
58
-        this.fromId = fromId;
53
+    public void setFromid(String fromid) {
54
+        this.fromid = fromid;
59
     }
55
     }
60
 
56
 
61
-    public String getToId() {
62
-        return toId;
57
+    public String getToid() {
58
+        return toid;
63
     }
59
     }
64
 
60
 
65
-    public void setToId(String toId) {
66
-        this.toId = toId;
61
+    public void setToid(String toid) {
62
+        this.toid = toid;
67
     }
63
     }
68
 
64
 
69
     public String getMessage() {
65
     public String getMessage() {
78
     public String toString() {
74
     public String toString() {
79
         return "Message{" +
75
         return "Message{" +
80
                 "sequence:'" + sequence + '\'' +
76
                 "sequence:'" + sequence + '\'' +
81
-                ", timeStamp:'" + timeStamp + '\'' +
82
-                ", fromId:'" + fromId + '\'' +
83
-                ", toId:'" + toId + '\'' +
77
+                ", timestamp:'" + timestamp + '\'' +
78
+                ", fromid:'" + fromid + '\'' +
79
+                ", toid:'" + toid + '\'' +
84
                 ", message:'" + message + '\'' +
80
                 ", message:'" + message + '\'' +
85
                 '}';
81
                 '}';
86
     }
82
     }

+ 37
- 4
Client/src/main/java/SimpleShell.java View File

10
 import java.util.ArrayList;
10
 import java.util.ArrayList;
11
 import java.util.List;
11
 import java.util.List;
12
 import java.util.Map;
12
 import java.util.Map;
13
+import java.util.regex.Matcher;
14
+import java.util.regex.Pattern;
13
 
15
 
14
 public class SimpleShell {
16
 public class SimpleShell {
15
 
17
 
20
 
22
 
21
 
23
 
22
     public static void prettyPrint(String output) throws IOException {
24
     public static void prettyPrint(String output) throws IOException {
23
-        ObjectMapper objectMapper = new ObjectMapper();
24
-        Map<String, String> map = objectMapper.readValue(output, new TypeReference<Map<String, String>>(){});
25
-
26
         // yep, make an effort to format things nicely, eh?
25
         // yep, make an effort to format things nicely, eh?
27
-        System.out.println(map.toString());
26
+        System.out.println(output);
28
     }
27
     }
29
     public static void main(String[] args) throws java.io.IOException {
28
     public static void main(String[] args) throws java.io.IOException {
30
 
29
 
35
         ProcessBuilder pb = new ProcessBuilder();
34
         ProcessBuilder pb = new ProcessBuilder();
36
         List<String> history = new ArrayList<String>();
35
         List<String> history = new ArrayList<String>();
37
         int index = 0;
36
         int index = 0;
37
+        //REGEX
38
+        String post = "\\bPOST\\b.\\w\\w [a-z0-9_-]{3,16}$";
39
+        Pattern postPattern = Pattern.compile(post);
40
+        Matcher postMatcher = postPattern.matcher(post);
38
         //we break out with <ctrl c>
41
         //we break out with <ctrl c>
39
         while (true) {
42
         while (true) {
40
             //read what the user enters
43
             //read what the user enters
41
             System.out.println("cmd? ");
44
             System.out.println("cmd? ");
42
             commandLine = console.readLine();
45
             commandLine = console.readLine();
43
 
46
 
47
+            if(commandLine.equals("help")){
48
+                System.out.println("history\t\t\tids\t\t\tmessages\nmessage-log\t\tPOST-id\t\tPOST-message\n");
49
+                continue;
50
+            }
44
             //input parsed into array of strings(command and arguments)
51
             //input parsed into array of strings(command and arguments)
45
             String[] commands = commandLine.split(" ");
52
             String[] commands = commandLine.split(" ");
46
             List<String> list = new ArrayList<String>();
53
             List<String> list = new ArrayList<String>();
85
                     continue;
92
                     continue;
86
                 }
93
                 }
87
                 // you need to add a bunch more.
94
                 // you need to add a bunch more.
95
+                if(list.contains("message-log")){
96
+                    String results = webber.messageParse();
97
+                    SimpleShell.prettyPrint(results);
98
+                    continue;
99
+                }
100
+
101
+                if (list.contains("POST-id")) {
102
+                    System.out.println("What is your name?");
103
+                    String name = console.readLine();;
104
+                    System.out.println("What is your GitHub User ID?");
105
+                    String gitID = console.readLine();;
106
+                    webber.postId(name, gitID);
107
+                    continue;
108
+
109
+                }
110
+                if(list.contains("POST-message")){
111
+                    System.out.println("What is your GitHub User Id?");
112
+                    String fromGitID = console.readLine();;
113
+                    System.out.println("Who are you sending the message to?");
114
+                    String toGitID = console.readLine();;
115
+                    System.out.println("What is your message?");
116
+                    String message = console.readLine();;
117
+                    webber.postMessage(fromGitID, toGitID, message);
118
+                    continue;
119
+                }
120
+
88
 
121
 
89
                 //!! command returns the last command in history
122
                 //!! command returns the last command in history
90
                 if (list.get(list.size() - 1).equals("!!")) {
123
                 if (list.get(list.size() - 1).equals("!!")) {

+ 28
- 9
Client/src/main/java/YouAreEll.java View File

1
 import com.fasterxml.jackson.core.JsonProcessingException;
1
 import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.core.type.TypeReference;
2
 import com.fasterxml.jackson.databind.ObjectMapper;
3
 import com.fasterxml.jackson.databind.ObjectMapper;
3
 import com.mashape.unirest.http.Unirest;
4
 import com.mashape.unirest.http.Unirest;
4
 import com.mashape.unirest.http.exceptions.UnirestException;
5
 import com.mashape.unirest.http.exceptions.UnirestException;
5
 import com.mashape.unirest.request.GetRequest;
6
 import com.mashape.unirest.request.GetRequest;
6
 import com.mashape.unirest.request.HttpRequestWithBody;
7
 import com.mashape.unirest.request.HttpRequestWithBody;
7
 
8
 
9
+import java.io.IOException;
10
+import java.util.List;
11
+
8
 public class YouAreEll {
12
 public class YouAreEll {
9
 
13
 
10
     YouAreEll() {
14
     YouAreEll() {
12
 
16
 
13
 
17
 
14
 
18
 
15
-    public static void main(String[] args) throws JsonProcessingException {
19
+    public static void main(String[] args) throws IOException {
16
         YouAreEll urlhandler = new YouAreEll();
20
         YouAreEll urlhandler = new YouAreEll();
17
      //   System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
21
      //   System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
18
-     //   System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
22
+      //  System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
19
      //   urlhandler.postId("Bo", "bell7692");
23
      //   urlhandler.postId("Bo", "bell7692");
20
      //   urlhandler.postMessage("bell7692", "kfennimore", "The best is yet to come");
24
      //   urlhandler.postMessage("bell7692", "kfennimore", "The best is yet to come");
21
-        urlhandler.get_messages();
25
+     // urlhandler.get_messages();
26
+        urlhandler.messageParse();
22
 
27
 
23
 
28
 
24
     }
29
     }
30
 
35
 
31
     }
36
     }
32
 
37
 
33
-    public void postMessage(String fromId, String toId, String message) throws JsonProcessingException {
38
+    public void postMessage(String fromid, String toid, String message) throws JsonProcessingException {
34
         ObjectMapper objectMapper = new ObjectMapper();
39
         ObjectMapper objectMapper = new ObjectMapper();
35
-        Message myMessage = new Message(fromId, toId, message);
40
+        Message myMessage = new Message(fromid, toid, message);
36
         String payload = objectMapper.writeValueAsString(myMessage);
41
         String payload = objectMapper.writeValueAsString(myMessage);
37
-        String url = "/ids/" +toId +"/messages";
42
+        String url = "/ids/" +toid +"/messages";
38
         MakeURLCall(url, "POST", payload);
43
         MakeURLCall(url, "POST", payload);
39
     }
44
     }
40
     public String get_ids() {
45
     public String get_ids() {
41
         return MakeURLCall("/ids", "GET", "");
46
         return MakeURLCall("/ids", "GET", "");
42
     }
47
     }
43
 
48
 
44
-//    public String get_20_messages(){
45
-//
46
-//    }
49
+
50
+    public String messageParse() throws IOException {
51
+        String str = MakeURLCall("/messages", "GET", "");
52
+        ObjectMapper objectMapper = new ObjectMapper();
53
+        List<Message> message = objectMapper.readValue(str, new TypeReference<List<Message>>(){});
54
+
55
+        StringBuilder sb = new StringBuilder();
56
+        for (int i = 0; i < message.size(); i++) {
57
+            sb.append("Time: " +message.get(i).getTimestamp() + "\n");
58
+            sb.append("From: " +message.get(i).getFromid() + "\n");
59
+            sb.append("To: " +message.get(i).getToid() + "\n");
60
+            sb.append("Message: " +message.get(i).getMessage() + "\n\n");
61
+        }
62
+        System.out.println(sb.toString());
63
+        return sb.toString();
64
+
65
+    }
47
     public String get_messages() {
66
     public String get_messages() {
48
         return MakeURLCall("/messages", "GET", "");
67
         return MakeURLCall("/messages", "GET", "");
49
     }
68
     }