|
@@ -1,16 +1,54 @@
|
|
1
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
2
|
+import com.fasterxml.jackson.databind.JavaType;
|
|
3
|
+
|
1
|
4
|
import java.io.BufferedReader;
|
2
|
5
|
import java.io.IOException;
|
3
|
6
|
import java.io.InputStream;
|
4
|
7
|
import java.io.InputStreamReader;
|
5
|
8
|
import java.util.ArrayList;
|
|
9
|
+import java.util.Collections;
|
6
|
10
|
import java.util.List;
|
|
11
|
+import java.util.regex.Matcher;
|
|
12
|
+import java.util.regex.Pattern;
|
7
|
13
|
|
8
|
14
|
public class SimpleShell {
|
9
|
15
|
|
10
|
16
|
|
11
|
|
- public static void prettyPrint(String output) {
|
|
17
|
+ public static void prettyPrint(String output, ObjectType type) {
|
12
|
18
|
// yep, make an effort to format things nicely, eh?
|
13
|
|
- System.out.println(output);
|
|
19
|
+
|
|
20
|
+ if(type.equals(ObjectType.MESSAGE)){
|
|
21
|
+ try {
|
|
22
|
+ ArrayList<Message> messages = Mapper.mapper.readValue(output, new TypeReference<ArrayList<Message>>() {});
|
|
23
|
+ System.out.println();
|
|
24
|
+ messages.forEach(System.out::println);
|
|
25
|
+ }
|
|
26
|
+ catch(IOException ioe){
|
|
27
|
+ System.out.println(ioe.getMessage());
|
|
28
|
+ }
|
|
29
|
+ }
|
|
30
|
+ else if (type.equals(ObjectType.ID)){
|
|
31
|
+ try {
|
|
32
|
+ ArrayList<Id> ids = Mapper.mapper.readValue(output, new TypeReference<ArrayList<Id>>() {});
|
|
33
|
+ System.out.println();
|
|
34
|
+ ids.forEach(System.out::println);
|
|
35
|
+ }
|
|
36
|
+ catch(IOException ioe){
|
|
37
|
+ System.out.println(ioe.getMessage());
|
|
38
|
+ }
|
|
39
|
+ }
|
|
40
|
+ else System.out.println("Something has gone wrong");
|
|
41
|
+
|
|
42
|
+// try {
|
|
43
|
+// ArrayList<JSONObject> messages = Mapper.mapper.readValue(output, new TypeReference<ArrayList<JSONObject>>() {});
|
|
44
|
+// for(JSONObject j : messages){
|
|
45
|
+// System.out.println(j);
|
|
46
|
+// }
|
|
47
|
+// }
|
|
48
|
+// catch(IOException ioe){
|
|
49
|
+// System.out.println(ioe.getMessage());
|
|
50
|
+// }
|
|
51
|
+
|
14
|
52
|
}
|
15
|
53
|
public static void main(String[] args) throws java.io.IOException {
|
16
|
54
|
|
|
@@ -41,11 +79,7 @@ public class SimpleShell {
|
41
|
79
|
}
|
42
|
80
|
|
43
|
81
|
//loop through to see if parsing worked
|
44
|
|
- for (int i = 0; i < commands.length; i++) {
|
45
|
|
- //System.out.println(commands[i]); //***check to see if parsing/split worked***
|
46
|
|
- list.add(commands[i]);
|
47
|
|
-
|
48
|
|
- }
|
|
82
|
+ Collections.addAll(list, commands);
|
49
|
83
|
System.out.print(list); //***check to see if list was added correctly***
|
50
|
84
|
history.addAll(list);
|
51
|
85
|
try {
|
|
@@ -59,16 +93,51 @@ public class SimpleShell {
|
59
|
93
|
// Specific Commands.
|
60
|
94
|
|
61
|
95
|
// ids
|
62
|
|
- if (list.contains("ids")) {
|
63
|
|
- String results = webber.get_ids();
|
64
|
|
- SimpleShell.prettyPrint(results);
|
|
96
|
+ if (list.get(0).equals("ids")) {
|
|
97
|
+ if (list.size()==3){
|
|
98
|
+ String userName = list.get(1);
|
|
99
|
+ String gitHub = list.get(2);
|
|
100
|
+ String payload = Mapper.mapper.writeValueAsString(new Id(userName,gitHub));
|
|
101
|
+ webber.MakeURLCall("/ids","POST", payload);
|
|
102
|
+ }
|
|
103
|
+ else if (list.size()==1) {
|
|
104
|
+ String results = webber.get_ids();
|
|
105
|
+ SimpleShell.prettyPrint(results, ObjectType.ID);
|
|
106
|
+ }
|
|
107
|
+ else System.out.println("Invalid Command");
|
65
|
108
|
continue;
|
66
|
109
|
}
|
67
|
110
|
|
68
|
111
|
// messages
|
69
|
|
- if (list.contains("messages")) {
|
70
|
|
- String results = webber.get_messages();
|
71
|
|
- SimpleShell.prettyPrint(results);
|
|
112
|
+ if (list.get(0).equals("messages")) {
|
|
113
|
+ if(list.size()==2){
|
|
114
|
+ String id = list.get(1);
|
|
115
|
+ SimpleShell.prettyPrint(webber.MakeURLCall("/ids/" + id + "/messages", "GET", ""),ObjectType.MESSAGE);
|
|
116
|
+ }
|
|
117
|
+ else if(list.size()==1) {
|
|
118
|
+ String results = webber.get_messages();
|
|
119
|
+ SimpleShell.prettyPrint(results, ObjectType.MESSAGE);
|
|
120
|
+ }
|
|
121
|
+ else System.out.println("Invalid Command");
|
|
122
|
+ continue;
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ if(list.get(0).equals("send")){
|
|
126
|
+ Pattern messageNoTo = Pattern.compile("'(.*)'");
|
|
127
|
+ Pattern messageTo = Pattern.compile("'(.*)' (to) (.*)");
|
|
128
|
+ Matcher withTo = messageTo.matcher(commandLine);
|
|
129
|
+ Matcher noTo = messageNoTo.matcher(commandLine);
|
|
130
|
+ String from = list.get(1);
|
|
131
|
+ if(withTo.find()){
|
|
132
|
+ String to = withTo.group(3);
|
|
133
|
+ String payload = Mapper.mapper.writeValueAsString(new Message(from, to, withTo.group(1)));
|
|
134
|
+ webber.MakeURLCall("/ids/" + to + "/messages", "POST", payload);
|
|
135
|
+ }
|
|
136
|
+ else if(noTo.find()) {
|
|
137
|
+ String payload = Mapper.mapper.writeValueAsString(new Message(from, noTo.group(1)));
|
|
138
|
+ webber.MakeURLCall("/ids/" + from + "/messages", "POST", payload);
|
|
139
|
+ }
|
|
140
|
+ else System.out.println("Invalid message format");
|
72
|
141
|
continue;
|
73
|
142
|
}
|
74
|
143
|
// you need to add a bunch more.
|