Browse Source

Merge 48723bff627630717c958dfaa616b7b777a45abc into 1ee920429b1ab2627f2bc0b8738939ba346daeda

bell7692 6 years ago
parent
commit
a084c5b513
No account linked to committer's email

+ 9
- 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,14 @@
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.mashape.unirest:unirest-java:1.4.9" level="project" />
16
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
17
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
18
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
19
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
20
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.2" level="project" />
21
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.3.2" level="project" />
22
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.3.6" level="project" />
23
+    <orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
17 24
   </component>
18 25
 </module>

+ 32
- 0
Client/pom.xml View File

@@ -11,5 +11,37 @@
11 11
 
12 12
     <artifactId>Client</artifactId>
13 13
 
14
+    <dependencies>
15
+        <dependency>
16
+            <groupId>com.fasterxml.jackson.core</groupId>
17
+            <artifactId>jackson-databind</artifactId>
18
+            <version>2.8.6</version>
19
+        </dependency>
20
+        <dependency>
21
+            <groupId>com.mashape.unirest</groupId>
22
+            <artifactId>unirest-java</artifactId>
23
+            <version>1.4.9</version>
24
+        </dependency>
25
+        <dependency>
26
+            <groupId>org.apache.httpcomponents</groupId>
27
+            <artifactId>httpclient</artifactId>
28
+            <version>4.3.6</version>
29
+        </dependency>
30
+        <dependency>
31
+            <groupId>org.apache.httpcomponents</groupId>
32
+            <artifactId>httpasyncclient</artifactId>
33
+            <version>4.0.2</version>
34
+        </dependency>
35
+        <dependency>
36
+            <groupId>org.apache.httpcomponents</groupId>
37
+            <artifactId>httpmime</artifactId>
38
+            <version>4.3.6</version>
39
+        </dependency>
40
+        <dependency>
41
+            <groupId>org.json</groupId>
42
+            <artifactId>json</artifactId>
43
+            <version>20140107</version>
44
+        </dependency>
14 45
 
46
+    </dependencies>
15 47
 </project>

+ 58
- 0
Client/src/main/java/Id.java View File

@@ -0,0 +1,58 @@
1
+import com.mashape.unirest.http.HttpResponse;
2
+import com.mashape.unirest.http.Unirest;
3
+import com.mashape.unirest.http.exceptions.UnirestException;
4
+
5
+public class Id {
6
+
7
+    private String userId;
8
+    private String name;
9
+    private String github;
10
+
11
+
12
+    public Id() throws UnirestException {
13
+    }
14
+
15
+    public Id(String name, String github) {
16
+        this.name = name;
17
+        this.github = github;
18
+    }
19
+
20
+    public Id(String userId, String name, String github) {
21
+        this.userId = userId;
22
+        this.name = name;
23
+        this.github = github;
24
+    }
25
+
26
+    public String getUserId() {
27
+        return userId;
28
+    }
29
+
30
+    public void setUserId(String userId) {
31
+        this.userId = userId;
32
+    }
33
+
34
+    public String getName() {
35
+        return name;
36
+    }
37
+
38
+    public void setName(String name) {
39
+        this.name = name;
40
+    }
41
+
42
+    public String getGithub() {
43
+        return github;
44
+    }
45
+
46
+    public void setGithub(String github) {
47
+        this.github = github;
48
+    }
49
+
50
+    @Override
51
+    public String toString() {
52
+        return "Id{" +
53
+                "userId:'" + userId + '\'' +
54
+                ", name:'" + name + '\'' +
55
+                ", github:'" + github + '\'' +
56
+                '}';
57
+    }
58
+}

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

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

+ 69
- 8
Client/src/main/java/SimpleShell.java View File

@@ -1,14 +1,27 @@
1
-import java.io.BufferedReader;
2
-import java.io.IOException;
3
-import java.io.InputStream;
4
-import java.io.InputStreamReader;
1
+import com.fasterxml.jackson.core.type.TypeReference;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+import com.mashape.unirest.http.HttpResponse;
4
+import com.mashape.unirest.http.Unirest;
5
+import com.mashape.unirest.http.exceptions.UnirestException;
6
+
7
+import java.io.*;
8
+import java.net.MalformedURLException;
9
+import java.net.URL;
5 10
 import java.util.ArrayList;
6 11
 import java.util.List;
12
+import java.util.Map;
13
+import java.util.regex.Matcher;
14
+import java.util.regex.Pattern;
7 15
 
8 16
 public class SimpleShell {
9 17
 
10 18
 
11
-    public static void prettyPrint(String output) {
19
+
20
+    public SimpleShell() throws UnirestException {
21
+    }
22
+
23
+
24
+    public static void prettyPrint(String output) throws IOException {
12 25
         // yep, make an effort to format things nicely, eh?
13 26
         System.out.println(output);
14 27
     }
@@ -16,18 +29,25 @@ public class SimpleShell {
16 29
 
17 30
         YouAreEll webber = new YouAreEll();
18 31
         String commandLine;
19
-        BufferedReader console = new BufferedReader
20
-                (new InputStreamReader(System.in));
21
-
32
+        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
33
+        //ProcessBuilder class used to help create operating system process
22 34
         ProcessBuilder pb = new ProcessBuilder();
23 35
         List<String> history = new ArrayList<String>();
24 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);
25 41
         //we break out with <ctrl c>
26 42
         while (true) {
27 43
             //read what the user enters
28 44
             System.out.println("cmd? ");
29 45
             commandLine = console.readLine();
30 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
+            }
31 51
             //input parsed into array of strings(command and arguments)
32 52
             String[] commands = commandLine.split(" ");
33 53
             List<String> list = new ArrayList<String>();
@@ -72,6 +92,32 @@ public class SimpleShell {
72 92
                     continue;
73 93
                 }
74 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
+
75 121
 
76 122
                 //!! command returns the last command in history
77 123
                 if (list.get(list.size() - 1).equals("!!")) {
@@ -87,6 +133,9 @@ public class SimpleShell {
87 133
                 }
88 134
 
89 135
                 // wait, wait, what curiousness is this?
136
+                //Process class is an executing program
137
+                //Used to perform input, output, waiting for the process to complete,
138
+                //checking exit status of the pocess and destroying process
90 139
                 Process process = pb.start();
91 140
 
92 141
                 //obtain the input stream
@@ -116,6 +165,18 @@ public class SimpleShell {
116 165
              * 5. output the contents returned by the command
117 166
              */
118 167
 
168
+//            ProcessBuilder pb1 = new ProcessBuilder();
169
+//            pb1.start();
170
+//            OutputStream outputStream = new OutputStream() {
171
+//                @Override
172
+//                public void write(int b) throws IOException {
173
+//
174
+//                }
175
+//            };
176
+
177
+
178
+
179
+
119 180
         }
120 181
 
121 182
 

+ 82
- 3
Client/src/main/java/YouAreEll.java View File

@@ -1,23 +1,102 @@
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.Unirest;
5
+import com.mashape.unirest.http.exceptions.UnirestException;
6
+import com.mashape.unirest.request.GetRequest;
7
+import com.mashape.unirest.request.HttpRequestWithBody;
8
+
9
+import java.io.IOException;
10
+import java.util.List;
11
+
1 12
 public class YouAreEll {
2 13
 
3 14
     YouAreEll() {
4 15
     }
5 16
 
6
-    public static void main(String[] args) {
17
+
18
+
19
+    public static void main(String[] args) throws IOException {
7 20
         YouAreEll urlhandler = new YouAreEll();
8
-        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9
-        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
21
+     //   System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
22
+      //  System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
23
+     //   urlhandler.postId("Bo", "bell7692");
24
+     //   urlhandler.postMessage("bell7692", "kfennimore", "The best is yet to come");
25
+     // urlhandler.get_messages();
26
+        urlhandler.messageParse();
27
+
28
+
29
+    }
30
+    public void postId (String name, String githubID) throws JsonProcessingException {
31
+        ObjectMapper objectMapper = new ObjectMapper();
32
+        Id myId = new Id(name, githubID);
33
+        String payload = objectMapper.writeValueAsString(myId);
34
+        MakeURLCall("/ids", "POST", payload);
35
+
10 36
     }
11 37
 
38
+    public void postMessage(String fromid, String toid, String message) throws JsonProcessingException {
39
+        ObjectMapper objectMapper = new ObjectMapper();
40
+        Message myMessage = new Message(fromid, toid, message);
41
+        String payload = objectMapper.writeValueAsString(myMessage);
42
+        String url = "/ids/" +toid +"/messages";
43
+        MakeURLCall(url, "POST", payload);
44
+    }
12 45
     public String get_ids() {
13 46
         return MakeURLCall("/ids", "GET", "");
14 47
     }
15 48
 
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
+    }
16 66
     public String get_messages() {
17 67
         return MakeURLCall("/messages", "GET", "");
18 68
     }
19 69
 
20 70
     public String MakeURLCall(String mainurl, String method, String jpayload) {
71
+        String url= "http://zipcode.rocks:8085" + mainurl;
72
+        GetRequest request = Unirest.get(url);
73
+        HttpRequestWithBody requestWithBody = Unirest.post(url);
74
+        HttpRequestWithBody requestWithBody1 = Unirest.put(url);
75
+
76
+        if(method.equals("GET")){
77
+            try {
78
+                return request.asJson().getBody().toString();
79
+            } catch (UnirestException e) {
80
+                e.printStackTrace();
81
+            }
82
+        }
83
+
84
+        else if(method.equals("POST")){
85
+            try {
86
+                return requestWithBody.body(jpayload).asJson().getBody().toString();
87
+            } catch (UnirestException e) {
88
+                e.printStackTrace();
89
+            }
90
+        }
91
+
92
+//        else if (method.equals("PUT")){
93
+//            try {
94
+//                return requestWithBody1.body(jpayload).asJson().getBody().toString();
95
+//            } catch (UnirestException e) {
96
+//                e.printStackTrace();
97
+//            }
98
+//        }
99
+
21 100
         return "nada";
22 101
     }
23 102
 }

+ 9
- 0
YouAreEll.iml View File

@@ -11,5 +11,14 @@
11 11
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
12 12
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
13 13
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" 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>

+ 25
- 0
pom.xml View File

@@ -18,5 +18,30 @@
18 18
             <artifactId>jackson-databind</artifactId>
19 19
             <version>2.8.6</version>
20 20
         </dependency>
21
+        <dependency>
22
+            <groupId>com.mashape.unirest</groupId>
23
+            <artifactId>unirest-java</artifactId>
24
+            <version>1.4.9</version>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>org.apache.httpcomponents</groupId>
28
+            <artifactId>httpclient</artifactId>
29
+            <version>4.3.6</version>
30
+        </dependency>
31
+        <dependency>
32
+            <groupId>org.apache.httpcomponents</groupId>
33
+            <artifactId>httpasyncclient</artifactId>
34
+            <version>4.0.2</version>
35
+        </dependency>
36
+        <dependency>
37
+            <groupId>org.apache.httpcomponents</groupId>
38
+            <artifactId>httpmime</artifactId>
39
+            <version>4.3.6</version>
40
+        </dependency>
41
+        <dependency>
42
+            <groupId>org.json</groupId>
43
+            <artifactId>json</artifactId>
44
+            <version>20140107</version>
45
+        </dependency>
21 46
     </dependencies>
22 47
 </project>