ソースを参照

Merge 157716d57c00b9a6c8cb21e4a25dba3568ffbc8e into bd1fca498ef63809a3a7a9e8867a8c20219bafbb

PeterMcCormick 6 年 前
コミット
2a554444ee
コミット者のEメールアドレスに関連付けられたアカウントが存在しません
共有7 個のファイルを変更した256 個の追加9 個の削除を含む
  1. 7
    3
      Client/Client.iml
  2. 60
    0
      Client/src/main/java/Id.java
  3. 86
    0
      Client/src/main/java/Message.java
  4. 46
    5
      Client/src/main/java/SimpleShell.java
  5. 40
    1
      Client/src/main/java/YouAreEll.java
  6. 6
    0
      YouAreEll.iml
  7. 11
    0
      pom.xml

+ 7
- 3
Client/Client.iml ファイルの表示

@@ -1,12 +1,10 @@
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_9">
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" />
10 8
       <excludeFolder url="file://$MODULE_DIR$/target" />
11 9
     </content>
12 10
     <orderEntry type="inheritedJdk" />
@@ -14,5 +12,11 @@
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
+    <orderEntry type="library" scope="TEST" name="Maven: com.squareup.okhttp3:mockwebserver:3.10.0" level="project" />
18
+    <orderEntry type="library" scope="TEST" name="Maven: org.bouncycastle:bcprov-jdk15on:1.50" level="project" />
19
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
20
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
17 21
   </component>
18 22
 </module>

+ 60
- 0
Client/src/main/java/Id.java ファイルの表示

@@ -0,0 +1,60 @@
1
+
2
+public class Id {
3
+
4
+    private String id;
5
+    private String name;
6
+    private String github;
7
+
8
+    public String getId() {
9
+        return id;
10
+    }
11
+
12
+    public void setId(String id) {
13
+        this.id = id;
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
+
32
+    public Id(String id, String name, String github) {
33
+
34
+        this.id = id;
35
+        this.name = name;
36
+        this.github = github;
37
+
38
+    }
39
+
40
+    public Id(String name, String github) {
41
+        this.name = name;
42
+        this.github = github;
43
+    }
44
+
45
+    public Id() {
46
+
47
+    }
48
+
49
+    @Override
50
+    public String toString() {
51
+        return "Id{" +
52
+                "id='" + this.id + '\'' +
53
+                ", name='" + this.name + '\'' +
54
+                ", gitHubId='" + this.github + '\'' +
55
+                '}';
56
+    }
57
+}
58
+
59
+
60
+

+ 86
- 0
Client/src/main/java/Message.java ファイルの表示

@@ -0,0 +1,86 @@
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) {
10
+        this.sequence = sequence;
11
+        this.timestamp = timestamp;
12
+        this.fromid = fromid;
13
+        this.toid = toid;
14
+        this.message = message;
15
+
16
+    }
17
+
18
+    public Message() {
19
+
20
+    }
21
+
22
+    public Message(String fromid, String toid, String message) {
23
+        this.fromid = fromid;
24
+        this.toid = toid;
25
+        this.message = message;
26
+    }
27
+
28
+    @Override
29
+    public String toString() {
30
+        return "Message{" +
31
+                "sequence='" + sequence + '\'' +
32
+                ", timestamp='" + timestamp + '\'' +
33
+                ", fromid='" + fromid + '\'' +
34
+                ", toid='" + toid + '\'' +
35
+                ", message='" + message + '\'' +
36
+                '}';
37
+    }
38
+
39
+    public Message(String fromid, String message) {
40
+        this.fromid = fromid;
41
+        this.message = message;
42
+    }
43
+
44
+
45
+    public String getSequence() {
46
+        return sequence;
47
+    }
48
+
49
+    public void setSequence(String sequence) {
50
+        this.sequence = sequence;
51
+    }
52
+
53
+    public String getTimestamp() {
54
+        return timestamp;
55
+    }
56
+
57
+    public void setTimestamp(String timestamp) {
58
+        this.timestamp = timestamp;
59
+    }
60
+
61
+    public String getFormid() {
62
+        return fromid;
63
+    }
64
+
65
+    public void setFormid(String formid) {
66
+        this.fromid = formid;
67
+    }
68
+
69
+    public String getToid() {
70
+        return toid;
71
+    }
72
+
73
+    public void setToid(String toid) {
74
+        this.toid = toid;
75
+    }
76
+
77
+    public String getMessage() {
78
+        return message;
79
+    }
80
+
81
+    public void setMessage(String message) {
82
+        this.message = message;
83
+    }
84
+}
85
+
86
+

+ 46
- 5
Client/src/main/java/SimpleShell.java ファイルの表示

@@ -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,7 +12,15 @@ 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 prettyPrintSets = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
19
+            System.out.println(prettyPrintSets);
20
+        } catch (IOException e) {
21
+            e.printStackTrace();
22
+        }
23
+
14 24
     }
15 25
     public static void main(String[] args) throws java.io.IOException {
16 26
 
@@ -18,7 +28,7 @@ public class SimpleShell {
18 28
         String commandLine;
19 29
         BufferedReader console = new BufferedReader
20 30
                 (new InputStreamReader(System.in));
21
-
31
+        ObjectMapper objectMapper = new ObjectMapper();
22 32
         ProcessBuilder pb = new ProcessBuilder();
23 33
         List<String> history = new ArrayList<String>();
24 34
         int index = 0;
@@ -59,20 +69,50 @@ public class SimpleShell {
59 69
                 // Specific Commands.
60 70
 
61 71
                 // ids
62
-                if (list.contains("ids")) {
72
+                if (list.contains("ids") && list.size() == 1) {
63 73
                     String results = webber.get_ids();
64 74
                     SimpleShell.prettyPrint(results);
65 75
                     continue;
66 76
                 }
67 77
 
68 78
                 // messages
69
-                if (list.contains("messages")) {
79
+                if (list.contains("messages") && list.size() == 1) {
70 80
                     String results = webber.get_messages();
71 81
                     SimpleShell.prettyPrint(results);
72 82
                     continue;
73 83
                 }
74 84
                 // you need to add a bunch more.
75 85
 
86
+                if (list.contains("messages") && list.size() == 2) {
87
+                    String myGithub = list.get(1);
88
+                    String results = webber.MakeURLCall("/ids/" +  myGithub + "/messages",
89
+                            "GET", "");
90
+                    SimpleShell.prettyPrint(results);
91
+                    continue;
92
+                }
93
+
94
+                if (list.contains("send")) {
95
+                    String groupedMessage = "";
96
+                    if (list.get(list.size()-2).equals("to"));
97
+                    for (int i = 2; i < list.size()-2; i++) {
98
+                        groupedMessage += list.get(i) + " ";
99
+                    }
100
+                    String trimmedMessage = groupedMessage.trim();
101
+                    Message newMessage = new Message(list.get(1), list.get(list.size()-1), trimmedMessage);
102
+                    String newMessageInfo = objectMapper.writeValueAsString(newMessage);
103
+                    webber.MakeURLCall("/ids/" + list.get(1) + "/messages", "POST", newMessageInfo);
104
+                    SimpleShell.prettyPrint(newMessageInfo);
105
+                    continue;
106
+                }
107
+
108
+                if (list.contains("ids") && list.size() == 3) {
109
+                    String name = list.get(1);
110
+                    String gitHubId = list.get(2);
111
+                    Id newUserId = new Id(name, gitHubId);
112
+                    String newIdInfo = objectMapper.writeValueAsString(newUserId);
113
+                    webber.MakeURLCall("/ids", "POST", newIdInfo);
114
+                    continue;
115
+                }
76 116
                 //!! command returns the last command in history
77 117
                 if (list.get(list.size() - 1).equals("!!")) {
78 118
                     pb.command(history.get(history.size() - 2));
@@ -121,4 +161,5 @@ public class SimpleShell {
121 161
 
122 162
     }
123 163
 
124
-}
164
+}
165
+

+ 40
- 1
Client/src/main/java/YouAreEll.java ファイルの表示

@@ -1,5 +1,11 @@
1
+import okhttp3.*;
2
+
3
+import java.io.IOException;
4
+
1 5
 public class YouAreEll {
2 6
 
7
+    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
8
+
3 9
     YouAreEll() {
4 10
     }
5 11
 
@@ -17,7 +23,40 @@ public class YouAreEll {
17 23
         return MakeURLCall("/messages", "GET", "");
18 24
     }
19 25
 
20
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
26
+    public String MakeURLCall (String mainurl, String method, String jpayload) {
27
+        String url = "http://zipcode.rocks:8085" + mainurl;
28
+        OkHttpClient client = new OkHttpClient();
29
+
30
+        if (method.equals("GET")) {
31
+            Request request = new Request.Builder().url(url).build();
32
+            try (Response response = client.newCall(request).execute()) {
33
+                return response.body().string();
34
+            } catch (IOException e) {
35
+                e.printStackTrace();
36
+            }
37
+        }
38
+        else if (method.equals("POST")) {
39
+            RequestBody body = RequestBody.create(JSON, jpayload);
40
+            Request request = new Request.Builder().url(url).post(body).build();
41
+            try (Response response = client.newCall(request).execute()) {
42
+                return response.body().string();
43
+            } catch (IOException e){
44
+                e.printStackTrace();
45
+            }
46
+        }
47
+        else if (method.equals("PUT")) {
48
+            RequestBody body = RequestBody.create(JSON, jpayload);
49
+            Request request = new Request.Builder().url(url).post(body).build();
50
+            try (Response response = client.newCall(request).execute()) {
51
+                return response.body().string();
52
+            } catch (IOException e) {
53
+                e.printStackTrace();
54
+            }
55
+        }
21 56
         return "nada";
22 57
     }
58
+
59
+
23 60
 }
61
+
62
+

+ 6
- 0
YouAreEll.iml ファイルの表示

@@ -11,5 +11,11 @@
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.squareup.okhttp3:okhttp:3.10.0" level="project" />
15
+    <orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
16
+    <orderEntry type="library" scope="TEST" name="Maven: com.squareup.okhttp3:mockwebserver:3.10.0" level="project" />
17
+    <orderEntry type="library" scope="TEST" name="Maven: org.bouncycastle:bcprov-jdk15on:1.50" level="project" />
18
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
19
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14 20
   </component>
15 21
 </module>

+ 11
- 0
pom.xml ファイルの表示

@@ -18,5 +18,16 @@
18 18
             <artifactId>jackson-databind</artifactId>
19 19
             <version>2.8.6</version>
20 20
         </dependency>
21
+        <dependency>
22
+            <groupId>com.squareup.okhttp3</groupId>
23
+            <artifactId>okhttp</artifactId>
24
+            <version>3.10.0</version>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>com.squareup.okhttp3</groupId>
28
+            <artifactId>mockwebserver</artifactId>
29
+            <version>3.10.0</version>
30
+            <scope>test</scope>
31
+        </dependency>
21 32
     </dependencies>
22 33
 </project>