Browse Source

Merge 3b16646eb0c4f3a4c974eb00e08c5e8309bfec19 into 1ee920429b1ab2627f2bc0b8738939ba346daeda

Lawrence Wu 6 years ago
parent
commit
d71a8780af
No account linked to committer's email

+ 2
- 2
Client/Client.iml View File

@@ -5,8 +5,6 @@
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">
7 7
       <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8
-      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9
-      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10 8
       <excludeFolder url="file://$MODULE_DIR$/target" />
11 9
     </content>
12 10
     <orderEntry type="inheritedJdk" />
@@ -14,5 +12,7 @@
14 12
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15 13
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16 14
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
15
+    <orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.10.0" level="project" />
16
+    <orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
17 17
   </component>
18 18
 </module>

+ 12
- 1
Client/pom.xml View File

@@ -10,6 +10,17 @@
10 10
     <modelVersion>4.0.0</modelVersion>
11 11
 
12 12
     <artifactId>Client</artifactId>
13
-
13
+    <dependencies>
14
+        <dependency>
15
+            <groupId>com.fasterxml.jackson.core</groupId>
16
+            <artifactId>jackson-databind</artifactId>
17
+            <version>2.8.6</version>
18
+        </dependency>
19
+        <dependency>
20
+            <groupId>com.squareup.okhttp3</groupId>
21
+            <artifactId>okhttp</artifactId>
22
+            <version>3.10.0</version>
23
+        </dependency>
24
+    </dependencies>
14 25
 
15 26
 </project>

+ 68
- 0
Client/src/main/java/Message.java View File

@@ -0,0 +1,68 @@
1
+public class Message {
2
+
3
+    private String sequence;
4
+    private String timeStamp;
5
+    private String fromId;
6
+    private String toId;
7
+    private String message;
8
+
9
+    public Message(String sequence, String fromId, String toId, String message) {
10
+        this.sequence = sequence;
11
+        this.fromId = fromId;
12
+        this.toId = toId;
13
+        this.message = message;
14
+    }
15
+    public Message(String fromId, String toId, String message){
16
+        this.fromId = fromId;
17
+        this.toId = toId;
18
+        this.message = message;
19
+    }
20
+    public String getSequence() {
21
+        return sequence;
22
+    }
23
+
24
+    public void setSequence(String sequence) {
25
+        this.sequence = sequence;
26
+    }
27
+
28
+    public String getTimeStamp() {
29
+        return timeStamp;
30
+    }
31
+
32
+    public void setTimeStamp(String timeStamp) {
33
+        this.timeStamp = timeStamp;
34
+    }
35
+
36
+    public String getFromId() {
37
+        return fromId;
38
+    }
39
+
40
+    public void setFromId(String fromId) {
41
+        this.fromId = fromId;
42
+    }
43
+
44
+    public String getToId() {
45
+        return toId;
46
+    }
47
+
48
+    public void setToId(String toId) {
49
+        this.toId = toId;
50
+    }
51
+
52
+    public String getMessage() {
53
+        return message;
54
+    }
55
+
56
+    public void setMessage(String message) {
57
+        this.message = message;
58
+    }
59
+
60
+    @Override
61
+    public String toString(){
62
+        return "sequence: " + this.sequence +
63
+                "\ntimestamp: " + timeStamp +
64
+                "\nfromid: " + this.fromId +
65
+                "\ntoid: " + this.toId +
66
+                "\nmessage: " + this.message;
67
+    }
68
+}

+ 35
- 3
Client/src/main/java/SimpleShell.java View File

@@ -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;
@@ -8,9 +10,11 @@ import java.util.List;
8 10
 public class SimpleShell {
9 11
 
10 12
 
11
-    public static void prettyPrint(String output) {
13
+    public static void prettyPrint(String output) throws IOException {
12 14
         // yep, make an effort to format things nicely, eh?
13
-        System.out.println(output);
15
+        ObjectMapper objectMapper = new ObjectMapper();
16
+        Object json = objectMapper.readValue(output, Object.class);
17
+        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
14 18
     }
15 19
     public static void main(String[] args) throws java.io.IOException {
16 20
 
@@ -25,7 +29,7 @@ public class SimpleShell {
25 29
         //we break out with <ctrl c>
26 30
         while (true) {
27 31
             //read what the user enters
28
-            System.out.println("cmd? ");
32
+            System.out.println("cmd?");
29 33
             commandLine = console.readLine();
30 34
 
31 35
             //input parsed into array of strings(command and arguments)
@@ -71,6 +75,34 @@ public class SimpleShell {
71 75
                     SimpleShell.prettyPrint(results);
72 76
                     continue;
73 77
                 }
78
+                if (list.get(0).equals("send")){
79
+                    if (list.get(2).equals("to")){
80
+                        ObjectMapper mapper = new ObjectMapper();
81
+                        String friend = list.get(3);
82
+                        String message = "";
83
+                        for (int i = 4; i < list.size(); i++){
84
+                            message += list.get(i) + " ";
85
+                        }
86
+                        String fromId = list.get(1);
87
+                        Message sendMessage = new Message(fromId, friend, message);
88
+                        String messageload = mapper.writeValueAsString(sendMessage);
89
+                        String results = webber.postMessage(messageload);
90
+                        SimpleShell.prettyPrint(results);
91
+                    }
92
+                    else {
93
+                        ObjectMapper mapper = new ObjectMapper();
94
+                        String message = "";
95
+                        for (int i = 2; i < list.size(); i++) {
96
+                            message += list.get(i) + " ";
97
+                        }
98
+                        String fromId = list.get(1);
99
+                        Message messages = new Message(fromId, "", message);
100
+                        String messageload = mapper.writeValueAsString(messages);
101
+                        String results = webber.postMessage(messageload);
102
+                        SimpleShell.prettyPrint(results);
103
+                    }
104
+                }
105
+
74 106
                 // you need to add a bunch more.
75 107
 
76 108
                 //!! command returns the last command in history

+ 44
- 0
Client/src/main/java/UserId.java View File

@@ -0,0 +1,44 @@
1
+public class UserId {
2
+
3
+    private String userId;
4
+    private String name;
5
+    private String gitHub;
6
+
7
+    public UserId(String userId, String name, String gitHub) {
8
+        this.userId = userId;
9
+        this.name = name;
10
+        this.gitHub = gitHub;
11
+    }
12
+    public UserId(String name, String gitHub){
13
+        this.name = name;
14
+        this.gitHub = gitHub;
15
+    }
16
+    public String getUserId() {
17
+        return userId;
18
+    }
19
+
20
+    public void setUserId(String userId) {
21
+        this.userId = userId;
22
+    }
23
+
24
+    public String getName() {
25
+        return name;
26
+    }
27
+
28
+    public void setName(String name) {
29
+        this.name = name;
30
+    }
31
+
32
+    public String getGitHub() {
33
+        return gitHub;
34
+    }
35
+
36
+    public void setGitHub(String gitHub) {
37
+        this.gitHub = gitHub;
38
+    }
39
+
40
+    @Override
41
+    public String toString(){
42
+        return String.format("User ID: %s, Name: %s, GitHub: %s", this.userId, this.name, this.gitHub);
43
+    }
44
+}

+ 62
- 7
Client/src/main/java/YouAreEll.java View File

@@ -1,23 +1,78 @@
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+import okhttp3.*;
4
+
5
+import java.io.IOException;
6
+
1 7
 public class YouAreEll {
2 8
 
9
+    private String url = "http://zipcode.rocks:8085";
10
+    OkHttpClient client;
11
+    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
12
+    ObjectMapper objectMapper;
13
+
3 14
     YouAreEll() {
15
+        objectMapper = new ObjectMapper();
16
+         client = new OkHttpClient();
4 17
     }
5 18
 
6
-    public static void main(String[] args) {
19
+    public static void main(String[] args) throws IOException {
7 20
         YouAreEll urlhandler = new YouAreEll();
21
+        ObjectMapper mapper = new ObjectMapper();
22
+        UserId me = new UserId("Larry", "wulawrence");
23
+        String payload = mapper.writeValueAsString(me);
24
+//        urlhandler.MakeURLCall("/ids", "POST", payload);
25
+        Message message = new Message("wulawrence", "JoeHendricks415", "Look behind you");
26
+        String messageload = mapper.writeValueAsString(message);
27
+//        urlhandler.MakeURLCall("/ids/wulawrence/messages", "POST", messageload);
28
+
8 29
         System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9 30
         System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
10 31
     }
11
-
12
-    public String get_ids() {
32
+    public String get_ids() throws IOException {
13 33
         return MakeURLCall("/ids", "GET", "");
14 34
     }
15
-
16
-    public String get_messages() {
35
+    public String get_messages() throws IOException {
17 36
         return MakeURLCall("/messages", "GET", "");
18 37
     }
38
+    public String postName(String payload) throws IOException {
39
+        return MakeURLCall("/ids", "POST", payload);
40
+    }
41
+    public String postMessage(String payload) throws IOException {
42
+        return MakeURLCall("/ids/wulawrence/messages", "POST", payload);
43
+    }
44
+    public String putName(String payload) throws IOException {
45
+        return MakeURLCall("/ids", "PUT", payload);
46
+    }
47
+    public String putMessage(String payload) throws IOException {
48
+        return MakeURLCall("/ids/wulawrence/messages", "PUT", payload);
49
+    }
50
+
51
+    public String MakeURLCall(String mainurl, String method, String jpayload) throws IOException{
52
+        String fullUrl = url + mainurl;
53
+
54
+
55
+        if (method.equalsIgnoreCase("GET")){
56
+            RequestBody body = RequestBody.create(JSON, jpayload);
57
+            Request request = new Request.Builder().url(fullUrl).build();
58
+            Response response = client.newCall(request).execute();
59
+            return response.body().string();
60
+        }
61
+        if (method.equalsIgnoreCase("POST")){
62
+            RequestBody body = RequestBody.create(JSON, jpayload);
63
+            Request request = new Request.Builder().url(fullUrl).post(body).build();
64
+            Response response = client.newCall(request).execute();
65
+            return response.body().string();
66
+
67
+        }
68
+
69
+        if (method.equalsIgnoreCase("PUT")){
70
+            RequestBody body = RequestBody.create(JSON, jpayload);
71
+            Request request = new Request.Builder().url(fullUrl).put(body).build();
72
+            Response response = client.newCall(request).execute();
73
+            return response.body().string();
19 74
 
20
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
21
-        return "nada";
75
+        }
76
+        return "invald method: " + method;
22 77
     }
23 78
 }