Procházet zdrojové kódy

Merge 3b5226b056e5212ab42308c13bf75856a5674ca9 into 1ee920429b1ab2627f2bc0b8738939ba346daeda

Brian před 6 roky
rodič
revize
47a0840571
No account linked to committer's email

+ 13
- 5
Client/Client.iml Zobrazit soubor

@@ -5,14 +5,22 @@
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" />
13 11
     <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" />
12
+    <orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" />
13
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0.4" level="project" />
14
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.2" level="project" />
15
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.4" level="project" />
16
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
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" />
17 25
   </component>
18 26
 </module>

+ 19
- 0
Client/pom.xml Zobrazit soubor

@@ -10,6 +10,25 @@
10 10
     <modelVersion>4.0.0</modelVersion>
11 11
 
12 12
     <artifactId>Client</artifactId>
13
+    <dependencies>
13 14
 
15
+        <dependency>
16
+            <groupId>commons-httpclient</groupId>
17
+            <artifactId>commons-httpclient</artifactId>
18
+            <version>3.1</version>
19
+        </dependency>
14 20
 
21
+        <dependency>
22
+            <groupId>com.fasterxml.jackson.core</groupId>
23
+            <artifactId>jackson-databind</artifactId>
24
+            <version>2.9.4</version>
25
+        </dependency>
26
+
27
+        <dependency>
28
+            <groupId>com.mashape.unirest</groupId>
29
+            <artifactId>unirest-java</artifactId>
30
+            <version>1.4.9</version>
31
+        </dependency>
32
+
33
+    </dependencies>
15 34
 </project>

+ 46
- 0
Client/src/main/java/Id.java Zobrazit soubor

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

+ 62
- 0
Client/src/main/java/Message.java Zobrazit soubor

@@ -0,0 +1,62 @@
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 timeStamp, String fromId, String toId, String message) {
10
+        this.sequence = sequence;
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;
19
+        this.message = message;
20
+        this.fromid = fromId;
21
+    }
22
+
23
+    public String getSequence() {
24
+        return sequence;
25
+    }
26
+
27
+    public void setSequence(String sequence) {
28
+        this.sequence = sequence;
29
+    }
30
+
31
+    public String getTimeStamp() {
32
+        return timestamp;
33
+    }
34
+
35
+    public void setTimeStamp(String timeStamp) {
36
+        this.timestamp = timeStamp;
37
+    }
38
+
39
+    public String getFromId() {
40
+        return fromid;
41
+    }
42
+
43
+    public void setFromId(String fromId) {
44
+        this.fromid = fromId;
45
+    }
46
+
47
+    public String getToId() {
48
+        return toid;
49
+    }
50
+
51
+    public void setToId(String toId) {
52
+        this.toid = toId;
53
+    }
54
+
55
+    public String getMessage() {
56
+        return message;
57
+    }
58
+
59
+    public void setMessage(String message) {
60
+        this.message = message;
61
+    }
62
+}

+ 75
- 8
Client/src/main/java/SimpleShell.java Zobrazit soubor

@@ -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,62 @@ 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") && !list.get(list.size() - 3).contains("\'")) {
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
+                if(list.contains("send") && list.get(list.size() - 2).equals("to") && list.get(list.size() - 3).contains("\'")) {
120
+                    StringBuilder message = new StringBuilder();
121
+                    String fromid = list.get(1);
122
+                    String toid = list.get(list.size() - 1);
123
+                    for (int i = 2; i < list.size() - 2; i++) {
124
+                        if (i == 2) {
125
+                            message.append(list.get(i).substring(1) + " ");
126
+                        } else if (i == list.size() - 3) {
127
+                            message.append(list.get(i).substring(0, list.get(i).length() - 1));
128
+                        } else {
129
+                            message.append(list.get(i) + " ");
130
+                        }
131
+                    }
132
+                    Message messageToSend = new Message(toid, message.toString(), fromid);
133
+                    String messageToString = objectMapper.writeValueAsString(messageToSend);
134
+                    webber.MakeURLCall("/ids/" + toid + "/messages", "POST", messageToString);
135
+                    continue;
136
+                }
75 137
 
76 138
                 //!! command returns the last command in history
77 139
                 if (list.get(list.size() - 1).equals("!!")) {
@@ -87,7 +149,12 @@ public class SimpleShell {
87 149
                 }
88 150
 
89 151
                 // wait, wait, what curiousness is this?
90
-                Process process = pb.start();
152
+                Process process = null;
153
+                try {
154
+                    process = pb.start();
155
+                } catch(IOException e) {
156
+                    e.printStackTrace();
157
+                }
91 158
 
92 159
                 //obtain the input stream
93 160
                 InputStream is = process.getInputStream();

+ 40
- 0
Client/src/main/java/YouAreEll.java Zobrazit soubor

@@ -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
 }