Bläddra i källkod

finished part 1

Vincent Gasbarro 6 år sedan
förälder
incheckning
33f33c0154

+ 6
- 3
Client/Client.iml Visa fil

@@ -1,16 +1,19 @@
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" />
10 8
       <excludeFolder url="file://$MODULE_DIR$/target" />
11 9
     </content>
12 10
     <orderEntry type="inheritedJdk" />
13 11
     <orderEntry type="sourceFolder" forTests="false" />
12
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:fluent-hc:4.5.5" level="project" />
13
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.5" level="project" />
14
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.9" level="project" />
15
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
16
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
14 17
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15 18
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16 19
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />

+ 13
- 0
Client/pom.xml Visa fil

@@ -7,6 +7,19 @@
7 7
         <groupId>com.zipcoder.ZipChat</groupId>
8 8
         <version>1.0-SNAPSHOT</version>
9 9
     </parent>
10
+    <dependencies>
11
+        <!--dependency>
12
+            <groupId>org.apache.maven.plugins</groupId>
13
+            <artifactId>maven-dependency-plugin</artifactId>
14
+            <version>3.0.2</version>
15
+        </dependency>-->
16
+        <dependency>
17
+            <groupId>org.apache.httpcomponents</groupId>
18
+            <artifactId>fluent-hc</artifactId>
19
+            <version>4.5.5</version>
20
+        </dependency>
21
+    </dependencies>
22
+
10 23
     <modelVersion>4.0.0</modelVersion>
11 24
 
12 25
     <artifactId>Client</artifactId>

+ 36
- 0
Client/src/main/java/Identifier.java Visa fil

@@ -0,0 +1,36 @@
1
+public class Identifier {
2
+
3
+    private String userid;
4
+    private String name;
5
+    private String github;
6
+
7
+    public Identifier(String aName, String aGithub) {
8
+        this.userid = " ";
9
+        this.name = aName;
10
+        this.github = aGithub;
11
+    }
12
+
13
+    public String getUserid() {
14
+        return userid;
15
+    }
16
+
17
+    public void setUserid(String userid) {
18
+        this.userid = userid;
19
+    }
20
+
21
+    public String getName() {
22
+        return name;
23
+    }
24
+
25
+    public void setName(String name) {
26
+        this.name = name;
27
+    }
28
+
29
+    public String getGithub() {
30
+        return github;
31
+    }
32
+
33
+    public void setGithubid(String github) {
34
+        this.github = github;
35
+    }
36
+}

+ 64
- 0
Client/src/main/java/Message.java Visa fil

@@ -0,0 +1,64 @@
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 fromid, String toid, String message) {
10
+        this.sequence = " ";
11
+        this.timestamp = "2018-03-21T01:00:00.0Z";
12
+        this.fromid = fromid;
13
+        this.toid = toid;
14
+        this.message = message;
15
+    }
16
+
17
+    public Message(String fromid, String message) {
18
+        this.sequence = " ";
19
+        this.timestamp = "2018-03-21T01:00:00.0Z";
20
+        this.fromid = fromid;
21
+        this.toid = "";
22
+        this.message = message;
23
+    }
24
+
25
+    public String getSequence() {
26
+        return sequence;
27
+    }
28
+
29
+    public void setSequence(String sequence) {
30
+        this.sequence = sequence;
31
+    }
32
+
33
+    public String getTimestamp() {
34
+        return timestamp;
35
+    }
36
+
37
+    public void setTimestamp(String timestamp) {
38
+        this.timestamp = timestamp;
39
+    }
40
+
41
+    public String getFromid() {
42
+        return fromid;
43
+    }
44
+
45
+    public void setFromid(String fromid) {
46
+        this.fromid = fromid;
47
+    }
48
+
49
+    public String getToid() {
50
+        return toid;
51
+    }
52
+
53
+    public void setToid(String toid) {
54
+        this.toid = toid;
55
+    }
56
+
57
+    public String getMessage() {
58
+        return message;
59
+    }
60
+
61
+    public void setMessage(String message) {
62
+        this.message = message;
63
+    }
64
+}

+ 72
- 4
Client/src/main/java/SimpleShell.java Visa fil

@@ -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,8 +12,18 @@ 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
+
16
+        ObjectMapper mapper = new ObjectMapper();
17
+
18
+        try {
19
+            Object json = mapper.readValue(output, Object.class);
20
+            String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
21
+            System.out.println(pretty);
22
+        } catch (IOException ioe) {
23
+            ioe.printStackTrace();
24
+        }
14 25
     }
26
+
15 27
     public static void main(String[] args) throws java.io.IOException {
16 28
 
17 29
         YouAreEll webber = new YouAreEll();
@@ -21,6 +33,7 @@ public class SimpleShell {
21 33
 
22 34
         ProcessBuilder pb = new ProcessBuilder();
23 35
         List<String> history = new ArrayList<String>();
36
+        ObjectMapper mapper = new ObjectMapper();
24 37
         int index = 0;
25 38
         //we break out with <ctrl c>
26 39
         while (true) {
@@ -58,6 +71,15 @@ public class SimpleShell {
58 71
 
59 72
                 // Specific Commands.
60 73
 
74
+                if (list.contains("ids") && list.size() > 2) {
75
+                    String name = list.get(1);
76
+                    String githubid = list.get(2);
77
+                    Identifier id = new Identifier(name, githubid);
78
+                    String json = mapper.writeValueAsString(id);
79
+                    webber.MakeURLCall("/ids", "POST", json);
80
+                    continue;
81
+                }
82
+
61 83
                 // ids
62 84
                 if (list.contains("ids")) {
63 85
                     String results = webber.get_ids();
@@ -65,11 +87,57 @@ public class SimpleShell {
65 87
                     continue;
66 88
                 }
67 89
 
90
+                if (list.contains("send")) {
91
+                    String fromid = list.get(1);
92
+                    StringBuilder message = new StringBuilder();
93
+                    int indexEndMessage = 0;
94
+
95
+                    for (int i = 3; i < list.size(); i++) {
96
+                        if (list.get(i).contains("'")) {
97
+                            indexEndMessage = i;
98
+                            break;
99
+                        }
100
+                        indexEndMessage = 2;
101
+                    }
102
+
103
+                    for (int i = 2; i < indexEndMessage; i++) {
104
+                        if (i == 2) {
105
+                            message.append(list.get(i).substring(1));
106
+                            message.append(" ");
107
+                        } else {
108
+                            message.append(list.get(i));
109
+                            message.append(" ");
110
+                        }
111
+                    }
112
+
113
+                    message.append(list.get(indexEndMessage).substring(0, list.get(indexEndMessage).length()-1));
114
+                    String messageString = message.toString();
115
+
116
+                    if (list.size() - 1 == indexEndMessage) {
117
+                        Message obj = new Message(fromid, messageString);
118
+                        String json = mapper.writeValueAsString(obj);
119
+                        webber.MakeURLCall("/ids/" + fromid + "/messages", "POST", json);
120
+                    } else {
121
+                        String toid = list.get(list.size() - 1);
122
+                        Message obj = new Message(fromid, toid, messageString);
123
+                        String json = mapper.writeValueAsString(obj);
124
+                        webber.MakeURLCall("/ids/" + fromid + "/messages", "POST", json);
125
+                    }
126
+                    continue;
127
+                }
128
+
68 129
                 // messages
69 130
                 if (list.contains("messages")) {
70
-                    String results = webber.get_messages();
71
-                    SimpleShell.prettyPrint(results);
72
-                    continue;
131
+                    if (list.size() == 1) {
132
+                        String results = webber.get_messages();
133
+                        SimpleShell.prettyPrint(results);
134
+                        continue;
135
+                    } else {
136
+                        String urlpath = "/ids/" + list.get(1) + "/messages";
137
+                        String results = webber.MakeURLCall(urlpath, "GET", "");
138
+                        SimpleShell.prettyPrint(results);
139
+                        continue;
140
+                    }
73 141
                 }
74 142
                 // you need to add a bunch more.
75 143
 

+ 34
- 1
Client/src/main/java/YouAreEll.java Visa fil

@@ -1,3 +1,8 @@
1
+import org.apache.http.client.fluent.Request;
2
+import org.apache.http.entity.ContentType;
3
+
4
+import java.io.IOException;
5
+
1 6
 public class YouAreEll {
2 7
 
3 8
     YouAreEll() {
@@ -18,6 +23,34 @@ public class YouAreEll {
18 23
     }
19 24
 
20 25
     public String MakeURLCall(String mainurl, String method, String jpayload) {
21
-        return "nada";
26
+
27
+        String url = "http://zipcode.rocks:8085" + mainurl;
28
+
29
+        switch (method) {
30
+            case "GET":
31
+                return getMethod(url);
32
+            case "POST":
33
+                return postMethod(url, jpayload);
34
+            default:
35
+                return null;
36
+        }
37
+    }
38
+
39
+    private String getMethod(String url) {
40
+        try {
41
+            return Request.Get(url).execute().returnContent().toString();
42
+        } catch (IOException ioe) {
43
+            ioe.printStackTrace();
44
+        }
45
+        return null;
46
+    }
47
+
48
+    private String postMethod(String url, String jpayload) {
49
+        try {
50
+            return Request.Post(url).bodyString(jpayload, ContentType.APPLICATION_JSON).execute().returnContent().toString();
51
+        } catch (IOException ioe) {
52
+            ioe.printStackTrace();
53
+        }
54
+        return null;
22 55
     }
23 56
 }

+ 2
- 0
pom.xml Visa fil

@@ -18,5 +18,7 @@
18 18
             <artifactId>jackson-databind</artifactId>
19 19
             <version>2.8.6</version>
20 20
         </dependency>
21
+
21 22
     </dependencies>
23
+
22 24
 </project>