Brian He 6 jaren geleden
bovenliggende
commit
6d0e65a139

+ 7
- 0
Client/Client.iml Bestand weergeven

@@ -15,5 +15,12 @@
15 15
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.4" level="project" />
16 16
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
17 17
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.4" level="project" />
18
+    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
19
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.2" level="project" />
20
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.4" level="project" />
21
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.1.1" level="project" />
22
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.4.4" level="project" />
23
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.5.2" level="project" />
24
+    <orderEntry type="library" name="Maven: org.json:json:20160212" level="project" />
18 25
   </component>
19 26
 </module>

+ 6
- 2
Client/pom.xml Bestand weergeven

@@ -24,7 +24,11 @@
24 24
             <version>2.9.4</version>
25 25
         </dependency>
26 26
 
27
-    </dependencies>
28
-
27
+        <dependency>
28
+            <groupId>com.mashape.unirest</groupId>
29
+            <artifactId>unirest-java</artifactId>
30
+            <version>1.4.9</version>
31
+        </dependency>
29 32
 
33
+    </dependencies>
30 34
 </project>

+ 23
- 13
Client/src/main/java/Id.java Bestand weergeven

@@ -1,21 +1,26 @@
1 1
 public class Id {
2 2
 
3
-    private String userId;
3
+    private String userid;
4 4
     private String name;
5
-    private String githubId;
5
+    private String github;
6 6
 
7
-    public Id(String userId, String name, String githubId) {
8
-        this.userId = userId;
7
+    public Id(String userId, String name, String github) {
8
+        this.userid = userId;
9 9
         this.name = name;
10
-        this.githubId = githubId;
10
+        this.github = github;
11 11
     }
12 12
 
13
-    public String getUserId() {
14
-        return userId;
13
+    public Id(String name, String github) {
14
+        this.name = name;
15
+        this.github = github;
16
+    }
17
+
18
+    public String getUserid() {
19
+        return userid;
15 20
     }
16 21
 
17
-    public void setUserId(String userId) {
18
-        this.userId = userId;
22
+    public void setUserid(String userid) {
23
+        this.userid = userid;
19 24
     }
20 25
 
21 26
     public String getName() {
@@ -26,11 +31,16 @@ public class Id {
26 31
         this.name = name;
27 32
     }
28 33
 
29
-    public String getGithubId() {
30
-        return githubId;
34
+    public String getGithub() {
35
+        return github;
36
+    }
37
+
38
+    public void setGithub(String github) {
39
+        this.github = github;
31 40
     }
32 41
 
33
-    public void setGithubId(String githubId) {
34
-        this.githubId = githubId;
42
+    @Override
43
+    public String toString() {
44
+        return "UserId: " + this.userid + "Name: " + this.name + ", Github: " + this.github;
35 45
     }
36 46
 }

+ 20
- 13
Client/src/main/java/Message.java Bestand weergeven

@@ -1,16 +1,23 @@
1 1
 public class Message {
2
+
2 3
     private String sequence;
3
-    private String timeStamp;
4
-    private String fromId;
5
-    private String toId;
4
+    private String timestamp;
5
+    private String fromid;
6
+    private String toid;
6 7
     private String message;
7 8
 
8
-    public Message (String sequence, String timeStamp, String fromId, String toId, String message) {
9
+    public Message(String sequence, String timeStamp, String fromId, String toId, String message) {
9 10
         this.sequence = sequence;
10
-        this.timeStamp = timeStamp;
11
-        this.fromId = fromId;
12
-        this.toId = toId;
11
+        this.timestamp = timeStamp;
12
+        this.fromid = fromId;
13
+        this.toid = toId;
14
+        this.message = message;
15
+    }
16
+
17
+    public Message(String toId, String message, String fromId) {
18
+        this.toid = toId;
13 19
         this.message = message;
20
+        this.fromid = fromId;
14 21
     }
15 22
 
16 23
     public String getSequence() {
@@ -22,27 +29,27 @@ public class Message {
22 29
     }
23 30
 
24 31
     public String getTimeStamp() {
25
-        return timeStamp;
32
+        return timestamp;
26 33
     }
27 34
 
28 35
     public void setTimeStamp(String timeStamp) {
29
-        this.timeStamp = timeStamp;
36
+        this.timestamp = timeStamp;
30 37
     }
31 38
 
32 39
     public String getFromId() {
33
-        return fromId;
40
+        return fromid;
34 41
     }
35 42
 
36 43
     public void setFromId(String fromId) {
37
-        this.fromId = fromId;
44
+        this.fromid = fromId;
38 45
     }
39 46
 
40 47
     public String getToId() {
41
-        return toId;
48
+        return toid;
42 49
     }
43 50
 
44 51
     public void setToId(String toId) {
45
-        this.toId = toId;
52
+        this.toid = toId;
46 53
     }
47 54
 
48 55
     public String getMessage() {

+ 70
- 7
Client/src/main/java/SimpleShell.java Bestand weergeven

@@ -1,3 +1,5 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+
1 3
 import java.io.BufferedReader;
2 4
 import java.io.IOException;
3 5
 import java.io.InputStream;
@@ -10,19 +12,28 @@ public class SimpleShell {
10 12
 
11 13
     public static void prettyPrint(String output) {
12 14
         // yep, make an effort to format things nicely, eh?
13
-        System.out.println(output);
15
+        ObjectMapper mapper = new ObjectMapper();
16
+        try {
17
+            Object json = mapper.readValue(output, Object.class);
18
+            String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
19
+            System.out.println(indented);
20
+        } catch (IOException e) {
21
+            e.printStackTrace();
22
+        }
14 23
     }
24
+
15 25
     public static void main(String[] args) throws java.io.IOException {
16 26
 
17 27
         YouAreEll webber = new YouAreEll();
18 28
         String commandLine;
19
-        BufferedReader console = new BufferedReader
20
-                (new InputStreamReader(System.in));
21
-
29
+        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
22 30
         ProcessBuilder pb = new ProcessBuilder();
23 31
         List<String> history = new ArrayList<String>();
32
+        ObjectMapper objectMapper = new ObjectMapper();
33
+
24 34
         int index = 0;
25 35
         //we break out with <ctrl c>
36
+
26 37
         while (true) {
27 38
             //read what the user enters
28 39
             System.out.println("cmd? ");
@@ -33,8 +44,10 @@ public class SimpleShell {
33 44
             List<String> list = new ArrayList<String>();
34 45
 
35 46
             //if the user entered a return, just loop again
36
-            if (commandLine.equals(""))
47
+            if (commandLine.equals("")) {
37 48
                 continue;
49
+            }
50
+
38 51
             if (commandLine.equals("exit")) {
39 52
                 System.out.println("bye!");
40 53
                 break;
@@ -57,7 +70,6 @@ public class SimpleShell {
57 70
                 }
58 71
 
59 72
                 // Specific Commands.
60
-
61 73
                 // ids
62 74
                 if (list.contains("ids")) {
63 75
                     String results = webber.get_ids();
@@ -66,12 +78,63 @@ public class SimpleShell {
66 78
                 }
67 79
 
68 80
                 // messages
69
-                if (list.contains("messages")) {
81
+                if (list.contains("messages") && list.size() == 1) {
70 82
                     String results = webber.get_messages();
71 83
                     SimpleShell.prettyPrint(results);
72 84
                     continue;
73 85
                 }
74 86
                 // you need to add a bunch more.
87
+                if(list.contains("ids") && list.size() == 3) {
88
+                    String name = list.get(1);
89
+                    String github = list.get(2);
90
+                    Id id = new Id(name, github);
91
+                    String idInfo = objectMapper.writeValueAsString(id);
92
+                    webber.MakeURLCall("/ids", "POST", idInfo);
93
+                    continue;
94
+                }
95
+
96
+                if(list.contains("messages") && list.size() == 2) {
97
+                    String results = webber.MakeURLCall("/ids/" + list.get(1) + "/messages", "GET", "");
98
+                    SimpleShell.prettyPrint(results);
99
+                    continue;
100
+                }
101
+
102
+                if(list.contains("send") && list.size() > 2 && !list.get(list.size() - 2).equals("to")) {
103
+                    StringBuilder message = new StringBuilder();
104
+                    String github = list.get(1);
105
+                    for(int i = 2; i < list.size(); i++) {
106
+                        if(i == 2) {
107
+                            message.append(list.get(i).substring(1) + " ");
108
+                        } else if(i == list.size() - 1) {
109
+                            message.append(list.get(i).substring(0, list.get(i).length() - 1));
110
+                        } else {
111
+                            message.append(list.get(i) + " ");
112
+                        }
113
+                    }
114
+                    Message messageToSend = new Message(list.get(1), message.toString(), "");
115
+                    String messageToString = objectMapper.writeValueAsString(messageToSend);
116
+                    webber.MakeURLCall("/ids/" + github + "/messages", "POST", messageToString);
117
+                    continue;
118
+                }
119
+
120
+                if(list.contains("send") && list.get(list.size() - 2).equals("to")) {
121
+                    StringBuilder message = new StringBuilder();
122
+                    String fromid = list.get(1);
123
+                    String toid = list.get(list.size() - 1);
124
+                    for (int i = 2; i < list.size() - 2; i++) {
125
+                        if (i == 2) {
126
+                            message.append(list.get(i).substring(1) + " ");
127
+                        } else if (i == list.size() - 3) {
128
+                            message.append(list.get(i).substring(0, list.get(i).length() - 1));
129
+                        } else {
130
+                            message.append(list.get(i) + " ");
131
+                        }
132
+                    }
133
+                    Message messageToSend = new Message(toid, message.toString(), fromid);
134
+                    String messageToString = objectMapper.writeValueAsString(messageToSend);
135
+                    webber.MakeURLCall("/ids/" + toid + "/messages", "POST", messageToString);
136
+                    continue;
137
+                }
75 138
 
76 139
                 //!! command returns the last command in history
77 140
                 if (list.get(list.size() - 1).equals("!!")) {

+ 40
- 0
Client/src/main/java/YouAreEll.java Bestand weergeven

@@ -1,12 +1,36 @@
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+import com.mashape.unirest.http.Unirest;
4
+import com.mashape.unirest.http.exceptions.UnirestException;
5
+import com.mashape.unirest.request.GetRequest;
6
+
1 7
 public class YouAreEll {
2 8
 
3 9
     YouAreEll() {
4 10
     }
5 11
 
6 12
     public static void main(String[] args) {
13
+        ObjectMapper objectMapper = new ObjectMapper();
7 14
         YouAreEll urlhandler = new YouAreEll();
8 15
         System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9 16
         System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
17
+
18
+        Id id = new Id("Brian", "bth1994TestAgain");
19
+        Message message = new Message("bth1994", "Pst", "bth1994");
20
+
21
+        try {
22
+            String idInfo = objectMapper.writeValueAsString(id);
23
+            urlhandler.MakeURLCall("/ids", "POST", idInfo);
24
+        } catch (JsonProcessingException e) {
25
+            e.printStackTrace();
26
+        }
27
+
28
+        try {
29
+            String messageInfo = objectMapper.writeValueAsString(message);
30
+            urlhandler.MakeURLCall("/ids/bth1994/messages", "POST", messageInfo);
31
+        } catch (JsonProcessingException e) {
32
+                e.printStackTrace();
33
+        }
10 34
     }
11 35
 
12 36
     public String get_ids() {
@@ -18,6 +42,22 @@ public class YouAreEll {
18 42
     }
19 43
 
20 44
     public String MakeURLCall(String mainurl, String method, String jpayload) {
45
+        String url = "http://zipcode.rocks:8085" + mainurl;
46
+        GetRequest request = Unirest.get(url);
47
+
48
+        if(method.equals("GET")) {
49
+            try {
50
+                return request.asJson().getBody().toString();
51
+            } catch (UnirestException ue) {
52
+                ue.printStackTrace();
53
+            }
54
+        } else if(method.equals("POST")) {
55
+            try {
56
+                return Unirest.post(url).body(jpayload).asJson().getBody().toString();
57
+            } catch (UnirestException ue) {
58
+                ue.printStackTrace();
59
+            }
60
+        }
21 61
         return "nada";
22 62
     }
23 63
 }