Parcourir la source

Had to copy and paste from my original file because it was not pointing to the proper repository

PeterMcCormick il y a 6 ans
Parent
révision
62e8d03fa5

+ 7
- 3
Client/Client.iml Voir le fichier

@@ -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 Voir le fichier

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

+ 71
- 0
Client/src/main/java/Message.java Voir le fichier

@@ -0,0 +1,71 @@
1
+public class Message {
2
+
3
+    private String sequence;
4
+    private String timestamp = "2018-03-21T01:00:00:0Z";
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(String fromid, String toid, String message) {
19
+        this.fromid = fromid;
20
+        this.toid = toid;
21
+        this.message = message;
22
+    }
23
+
24
+    public Message(String fromid, String message) {
25
+        this.fromid = fromid;
26
+        this.message = message;
27
+    }
28
+
29
+
30
+    public String getSequence() {
31
+        return sequence;
32
+    }
33
+
34
+    public void setSequence(String sequence) {
35
+        this.sequence = sequence;
36
+    }
37
+
38
+    public String getTimestamp() {
39
+        return timestamp;
40
+    }
41
+
42
+    public void setTimestamp(String timestamp) {
43
+        this.timestamp = timestamp;
44
+    }
45
+
46
+    public String getFormid() {
47
+        return fromid;
48
+    }
49
+
50
+    public void setFormid(String formid) {
51
+        this.fromid = formid;
52
+    }
53
+
54
+    public String getToid() {
55
+        return toid;
56
+    }
57
+
58
+    public void setToid(String toid) {
59
+        this.toid = toid;
60
+    }
61
+
62
+    public String getMessage() {
63
+        return message;
64
+    }
65
+
66
+    public void setMessage(String message) {
67
+        this.message = message;
68
+    }
69
+}
70
+
71
+

+ 24
- 5
Client/src/main/java/SimpleShell.java Voir le fichier

@@ -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 indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
19
+            System.out.println(indented);
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,28 @@ 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("ids") && list.size() == 3) {
87
+                    String name = list.get(1);
88
+                    String gitHubId = list.get(2);
89
+                    Id newUserId = new Id(name, gitHubId);
90
+                    String newIdInfo = objectMapper.writeValueAsString(newUserId);
91
+                    webber.MakeURLCall("/ids", "POST", newIdInfo);
92
+                    continue;
93
+                }
76 94
                 //!! command returns the last command in history
77 95
                 if (list.get(list.size() - 1).equals("!!")) {
78 96
                     pb.command(history.get(history.size() - 2));
@@ -121,4 +139,5 @@ public class SimpleShell {
121 139
 
122 140
     }
123 141
 
124
-}
142
+}
143
+

+ 40
- 1
Client/src/main/java/YouAreEll.java Voir le fichier

@@ -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 Voir le fichier

@@ -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 Voir le fichier

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