瀏覽代碼

it mostly works

NedRedmond 6 年之前
父節點
當前提交
28367037ac

+ 16
- 6
Client/Client.iml 查看文件

@@ -1,18 +1,28 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
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" />
8
+      <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
10 9
       <excludeFolder url="file://$MODULE_DIR$/target" />
11 10
     </content>
12 11
     <orderEntry type="inheritedJdk" />
13 12
     <orderEntry type="sourceFolder" forTests="false" />
14
-    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15
-    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16
-    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
13
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
14
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
+    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
16
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.7" level="project" />
17
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
18
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7" level="project" />
19
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
20
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
21
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
22
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
23
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.2" level="project" />
24
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.3.2" level="project" />
25
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.3.6" level="project" />
26
+    <orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
17 27
   </component>
18 28
 </module>

+ 13
- 0
Client/pom.xml 查看文件

@@ -10,6 +10,19 @@
10 10
     <modelVersion>4.0.0</modelVersion>
11 11
 
12 12
     <artifactId>Client</artifactId>
13
+    <dependencies>
14
+        <dependency>
15
+            <groupId>junit</groupId>
16
+            <artifactId>junit</artifactId>
17
+            <version>4.12</version>
18
+            <scope>test</scope>
19
+        </dependency>
20
+        <dependency>
21
+            <groupId>com.mashape.unirest</groupId>
22
+            <artifactId>unirest-java</artifactId>
23
+            <version>1.4.9</version>
24
+        </dependency>
25
+    </dependencies>
13 26
 
14 27
 
15 28
 </project>

+ 48
- 5
Client/src/main/java/SimpleShell.java 查看文件

@@ -1,8 +1,12 @@
1
+import com.mashape.unirest.http.exceptions.UnirestException;
2
+import json_objects.Message;
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.Arrays;
6 10
 import java.util.List;
7 11
 
8 12
 public class SimpleShell {
@@ -20,17 +24,19 @@ public class SimpleShell {
20 24
                 (new InputStreamReader(System.in));
21 25
 
22 26
         ProcessBuilder pb = new ProcessBuilder();
23
-        List<String> history = new ArrayList<String>();
27
+        List<String> history = new ArrayList<>();
24 28
         int index = 0;
29
+
25 30
         //we break out with <ctrl c>
26 31
         while (true) {
32
+
27 33
             //read what the user enters
28 34
             System.out.println("cmd? ");
29 35
             commandLine = console.readLine();
30 36
 
31 37
             //input parsed into array of strings(command and arguments)
32 38
             String[] commands = commandLine.split(" ");
33
-            List<String> list = new ArrayList<String>();
39
+            List<String> list = new ArrayList<>();
34 40
 
35 41
             //if the user entered a return, just loop again
36 42
             if (commandLine.equals(""))
@@ -46,8 +52,10 @@ public class SimpleShell {
46 52
                 list.add(commands[i]);
47 53
 
48 54
             }
55
+
49 56
             System.out.print(list); //***check to see if list was added correctly***
50 57
             history.addAll(list);
58
+
51 59
             try {
52 60
                 //display history of shell with index
53 61
                 if (list.get(list.size() - 1).equals("history")) {
@@ -66,11 +74,37 @@ public class SimpleShell {
66 74
                 }
67 75
 
68 76
                 // messages
69
-                if (list.contains("messages")) {
77
+                if (list.contains("messages") && list.size() == 2) {
78
+                    String results = "";
79
+                    try {
80
+                        results = webber.get_messages_id(list.get(1));
81
+                        } catch (Exception e) {
82
+                        System.out.println("Invalid ID. Try again.");
83
+                        }
84
+                    SimpleShell.prettyPrint(results);
85
+                    continue;
86
+                }
87
+
88
+                if (list.contains("messages") && list.size() == 1) {
70 89
                     String results = webber.get_messages();
71 90
                     SimpleShell.prettyPrint(results);
72 91
                     continue;
73 92
                 }
93
+
94
+                if (list.contains("send")) {
95
+                    if (list.size() < 3) {
96
+                        System.out.println("Proper syntax is: send your_github_id 'Hello World'");
97
+                    } else {
98
+                        String toId = "";
99
+
100
+                        Message msg = new Message();
101
+                        msg.setMessage(getString(list));
102
+                        msg.setFromid(list.get(1));
103
+                        String results = webber.send_id_message(list.get(1), msg);
104
+                        SimpleShell.prettyPrint(results);
105
+                        continue;
106
+                    }
107
+                }
74 108
                 // you need to add a bunch more.
75 109
 
76 110
                 //!! command returns the last command in history
@@ -100,14 +134,18 @@ public class SimpleShell {
100 134
                     System.out.println(line);
101 135
                 br.close();
102 136
 
103
-
104 137
             }
105 138
 
106 139
             //catch ioexception, output appropriate message, resume waiting for input
107 140
             catch (IOException e) {
108 141
                 System.out.println("Input Error, Please try again!");
142
+            } catch (ClassNotFoundException e) {
143
+                e.printStackTrace();
144
+            } catch (UnirestException e) {
145
+                e.printStackTrace();
109 146
             }
110
-            // So what, do you suppose, is the meaning of this comment?
147
+
148
+            // So what do you suppose is the meaning of this comment?
111 149
             /** The steps are:
112 150
              * 1. parse the input to obtain the command and any parameters
113 151
              * 2. create a ProcessBuilder object
@@ -121,4 +159,9 @@ public class SimpleShell {
121 159
 
122 160
     }
123 161
 
162
+    private static String getString(List<String> list) {
163
+        List<String> sublist = list.subList(2, list.size());
164
+        return String.join(" ", sublist);
165
+    }
166
+
124 167
 }

+ 87
- 10
Client/src/main/java/YouAreEll.java 查看文件

@@ -1,23 +1,100 @@
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.core.type.TypeReference;
3
+import com.fasterxml.jackson.databind.ObjectMapper;
4
+import com.mashape.unirest.http.HttpResponse;
5
+import com.mashape.unirest.http.JsonNode;
6
+import com.mashape.unirest.http.Unirest;
7
+import com.mashape.unirest.http.exceptions.UnirestException;
8
+import json_objects.Id;
9
+import json_objects.Message;
10
+
11
+import java.io.IOException;
12
+import java.net.URL;
13
+import java.util.List;
14
+import java.util.stream.Stream;
15
+
1 16
 public class YouAreEll {
2 17
 
18
+    ObjectMapper jacksonMapper = new ObjectMapper();
19
+
3 20
     YouAreEll() {
21
+        Unirest.setObjectMapper(new com.mashape.unirest.http.ObjectMapper() {
22
+            com.fasterxml.jackson.databind.ObjectMapper mapper
23
+                    = new com.fasterxml.jackson.databind.ObjectMapper();
24
+
25
+            public String writeValue(Object value) {
26
+                try {
27
+                    return mapper.writeValueAsString(value);
28
+                } catch (JsonProcessingException e) {
29
+                    e.printStackTrace();
30
+                }
31
+                return null;
32
+            }
33
+
34
+            public <T> T readValue(String value, Class<T> valueType) {
35
+                try {
36
+                    return mapper.readValue(value, valueType);
37
+                } catch (IOException e) {
38
+                    e.printStackTrace();
39
+                }
40
+                return null;
41
+            }
42
+        });
4 43
     }
5 44
 
6
-    public static void main(String[] args) {
45
+    public static void main(String[] args) throws IOException, ClassNotFoundException {
7 46
         YouAreEll urlhandler = new YouAreEll();
8
-        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9
-        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
47
+        System.out.println(urlhandler.get_ids());
48
+        System.out.println(urlhandler.get_messages());
49
+    }
50
+
51
+    public String get_ids() throws IOException, ClassNotFoundException {
52
+        return makeUrlCall("/ids", "GET", new TypeReference<List<Id>>(){});
53
+    }
54
+
55
+    public String get_messages() throws IOException, ClassNotFoundException {
56
+        return makeUrlCall("/messages", "GET", new TypeReference<List<Message>>(){});
57
+    }
58
+
59
+    public String get_messages_id(String id) throws IOException, ClassNotFoundException {
60
+        return makeUrlCall(String.format("/" + id + "/messages"), "GET", new TypeReference<List<Id>>(){});
61
+    }
62
+
63
+    public String send_id_message(String id, Message message) throws IOException, ClassNotFoundException, UnirestException {
64
+        return makePostCall(
65
+                String.format("/ids/" + id + "/messages"),
66
+                message
67
+        );
68
+    }
69
+
70
+    public String urlConcat(String mainurl) {
71
+        return "http://zipcode.rocks:8085" + mainurl;
10 72
     }
11 73
 
12
-    public String get_ids() {
13
-        return MakeURLCall("/ids", "GET", "");
74
+    public String makeUrlCall(String mainurl, String method, TypeReference javaClass) throws IOException, ClassNotFoundException {
75
+        return makeGetCall(mainurl, javaClass);
14 76
     }
15 77
 
16
-    public String get_messages() {
17
-        return MakeURLCall("/messages", "GET", "");
78
+    public String makeGetCall(String mainurl, TypeReference javaClass) throws ClassNotFoundException, IOException {
79
+        List<Object> results = jacksonMapper.readValue(new URL(urlConcat(mainurl)), javaClass);
80
+        System.out.println(jacksonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(javaClass));
81
+        return jacksonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(results);
18 82
     }
19 83
 
20
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
21
-        return "nada";
84
+    public String makePostCall(String mainurl, Message messsage) throws ClassNotFoundException, IOException, UnirestException {
85
+        HttpResponse<JsonNode> post = Unirest.post(urlConcat(mainurl))
86
+                .header("accept", "application/json")
87
+                .header("Content-Type", "application/json")
88
+                .body(messsage)
89
+                .asJson();
90
+        return jacksonMapper.writeValueAsString(messsage);
22 91
     }
23
-}
92
+
93
+    public <T> Stream<T> makePayloadStream(String mainUrl, TypeReference javaClass) throws IOException {
94
+        List<T> results = jacksonMapper.readValue(new URL(urlConcat(mainUrl)), javaClass);
95
+        return results.stream();
96
+    }
97
+
98
+
99
+
100
+}

+ 31
- 0
Client/src/main/java/json_objects/Id.java 查看文件

@@ -0,0 +1,31 @@
1
+package json_objects;
2
+
3
+public class Id {
4
+    String userid;
5
+    String name;
6
+    String github;
7
+
8
+    public String getUserid() {
9
+        return userid;
10
+    }
11
+
12
+    public void setUserid(String userid) {
13
+        this.userid = userid;
14
+    }
15
+
16
+    public String getName() {
17
+        return name;
18
+    }
19
+
20
+    public void setName(String name) {
21
+        this.name = name;
22
+    }
23
+
24
+    public String getGithub() {
25
+        return github;
26
+    }
27
+
28
+    public void setGithub(String github) {
29
+        this.github = github;
30
+    }
31
+}

+ 49
- 0
Client/src/main/java/json_objects/Message.java 查看文件

@@ -0,0 +1,49 @@
1
+package json_objects;
2
+
3
+public class Message {
4
+    String sequence;
5
+    String timestamp;
6
+    String fromid;
7
+    String toid;
8
+    String message;
9
+
10
+    public String getSequence() {
11
+        return sequence;
12
+    }
13
+
14
+    public void setSequence(String sequence) {
15
+        this.sequence = sequence;
16
+    }
17
+
18
+    public String getTimestamp() {
19
+        return timestamp;
20
+    }
21
+
22
+    public void setTimestamp(String timestamp) {
23
+        this.timestamp = timestamp;
24
+    }
25
+
26
+    public String getFromid() {
27
+        return fromid;
28
+    }
29
+
30
+    public void setFromid(String fromid) {
31
+        this.fromid = fromid;
32
+    }
33
+
34
+    public String getToid() {
35
+        return toid;
36
+    }
37
+
38
+    public void setToid(String toid) {
39
+        this.toid = toid;
40
+    }
41
+
42
+    public String getMessage() {
43
+        return message;
44
+    }
45
+
46
+    public void setMessage(String message) {
47
+        this.message = message;
48
+    }
49
+}

+ 4
- 4
README.md 查看文件

@@ -19,7 +19,7 @@
19 19
 
20 20
 
21 21
 
22
-* You can send a message to the global timeline by POSTing a Message JSON object to the URL below.
22
+* You can send a message to the global timeline by POSTing a json_objects.Message JSON object to the URL below.
23 23
 	* If you leave the `to id` field empty, the message is `to the world`.
24 24
 	* If you fill out the the JSON template with a valid github_id in the `to id` field of the JSON payload, then that message is addressed to that friend.
25 25
 	* Yes, all messages can be seen by users of the system.
@@ -27,8 +27,8 @@
27 27
 
28 28
 
29 29
 
30
-* When you send a new Message or Id JSON object to the server, it records it, and fills in one or two fields. 
31
-	* A Message gets an assigned sequence number and a timestamp of when it was received by the server.
30
+* When you send a new json_objects.Message or json_objects.Id JSON object to the server, it records it, and fills in one or two fields. 
31
+	* A json_objects.Message gets an assigned sequence number and a timestamp of when it was received by the server.
32 32
 	* An ID object gets a "user id" field assigned to it.
33 33
 	* Any sequence number, timestamp or userid you put into a JSON template gets overwritten by the server when you POST it. 
34 34
 
@@ -95,7 +95,7 @@ and send it as the body of a POST request to  `http://zipcode.rocks:8085/ids/`
95 95
  
96 96
 ## Messages
97 97
 
98
-#### Message comands in shell
98
+#### json_objects.Message comands in shell
99 99
 in the shell
100 100
 * `messages` should return the last 20 messages, nicely formatted.
101 101
 * `messages your_github_id` should return the last 20 messages sent to you.

+ 13
- 4
YouAreEll.iml 查看文件

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">
@@ -8,8 +8,17 @@
8 8
     </content>
9 9
     <orderEntry type="inheritedJdk" />
10 10
     <orderEntry type="sourceFolder" forTests="false" />
11
-    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
12
-    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
13
-    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
11
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.7" level="project" />
12
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
13
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7" level="project" />
14
+    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
15
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
16
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
17
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
18
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
19
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.2" level="project" />
20
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.3.2" level="project" />
21
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.3.6" level="project" />
22
+    <orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
14 23
   </component>
15 24
 </module>

+ 31
- 1
pom.xml 查看文件

@@ -4,6 +4,11 @@
4 4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 5
     <modelVersion>4.0.0</modelVersion>
6 6
 
7
+    <properties>
8
+        <maven.compiler.source>1.8</maven.compiler.source>
9
+        <maven.compiler.target>1.8</maven.compiler.target>
10
+    </properties>
11
+
7 12
     <groupId>com.zipcoder.ZipChat</groupId>
8 13
     <artifactId>YouAreEll</artifactId>
9 14
     <packaging>pom</packaging>
@@ -16,7 +21,32 @@
16 21
         <dependency>
17 22
             <groupId>com.fasterxml.jackson.core</groupId>
18 23
             <artifactId>jackson-databind</artifactId>
19
-            <version>2.8.6</version>
24
+            <version>2.9.7</version>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>com.mashape.unirest</groupId>
28
+            <artifactId>unirest-java</artifactId>
29
+            <version>1.4.9</version>
30
+        </dependency>
31
+        <dependency>
32
+            <groupId>org.apache.httpcomponents</groupId>
33
+            <artifactId>httpclient</artifactId>
34
+            <version>4.3.6</version>
35
+        </dependency>
36
+        <dependency>
37
+            <groupId>org.apache.httpcomponents</groupId>
38
+            <artifactId>httpasyncclient</artifactId>
39
+            <version>4.0.2</version>
40
+        </dependency>
41
+        <dependency>
42
+            <groupId>org.apache.httpcomponents</groupId>
43
+            <artifactId>httpmime</artifactId>
44
+            <version>4.3.6</version>
45
+        </dependency>
46
+        <dependency>
47
+            <groupId>org.json</groupId>
48
+            <artifactId>json</artifactId>
49
+            <version>20140107</version>
20 50
         </dependency>
21 51
     </dependencies>
22 52
 </project>