Quellcode durchsuchen

almost fully functional. Some command line issues#

JaseG256 vor 6 Jahren
Ursprung
Commit
9ffd21dc78

+ 7
- 3
Client/Client.iml Datei anzeigen

@@ -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_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" />
@@ -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: org.apache.httpcomponents:httpclient:4.5.5" level="project" />
16
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.9" level="project" />
17
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
18
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
19
+    <orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.10.0" level="project" />
20
+    <orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
17 21
   </component>
18 22
 </module>

+ 12
- 0
Client/pom.xml Datei anzeigen

@@ -10,6 +10,18 @@
10 10
     <modelVersion>4.0.0</modelVersion>
11 11
 
12 12
     <artifactId>Client</artifactId>
13
+    <build>
14
+        <plugins>
15
+            <plugin>
16
+                <groupId>org.apache.maven.plugins</groupId>
17
+                <artifactId>maven-compiler-plugin</artifactId>
18
+                <configuration>
19
+                    <source>8</source>
20
+                    <target>8</target>
21
+                </configuration>
22
+            </plugin>
23
+        </plugins>
24
+    </build>
13 25
 
14 26
 
15 27
 </project>

+ 64
- 0
Client/src/main/java/Controller.java Datei anzeigen

@@ -0,0 +1,64 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+import okhttp3.*;
3
+
4
+import java.io.IOException;
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
8
+public abstract class Controller<T extends Model> {
9
+
10
+    protected OkHttpClient client = null;
11
+
12
+    protected ObjectMapper objectMapper = new ObjectMapper();
13
+
14
+    protected final String uRL = "http://zipcode.rocks:8085";
15
+
16
+    protected String responseBody;
17
+
18
+    protected static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
19
+
20
+    protected List<T> list = new ArrayList<>();
21
+
22
+    protected StringBuilder builder = new StringBuilder();
23
+
24
+    protected String jsonObject;
25
+
26
+    public String doPostRequest(String url, String json) {
27
+        client = new OkHttpClient();
28
+
29
+        RequestBody body = RequestBody.create(JSON, json);
30
+        Request request = new Request.Builder().url(uRL).post(body).build();
31
+
32
+        try {
33
+            Response response = client.newCall(request).execute();
34
+            responseBody = response.body().string();
35
+        } catch (IOException e) {
36
+            e.printStackTrace();
37
+        }
38
+        return responseBody;
39
+
40
+    }
41
+
42
+    public String get(String resource) throws IOException{
43
+        client = new OkHttpClient();
44
+        Request request = new Request.Builder().url(uRL + resource).get().build();
45
+
46
+        Response response = null;
47
+
48
+        try {
49
+            response = client.newCall(request).execute();
50
+            responseBody = response.body().string();
51
+
52
+        } catch (IOException e) {
53
+            e.printStackTrace();
54
+        }
55
+        return responseBody;
56
+    }
57
+
58
+
59
+
60
+    public OkHttpClient getClient() { return client; }
61
+
62
+    public String getuRL() { return uRL; }
63
+
64
+}

+ 40
- 0
Client/src/main/java/IDs.java Datei anzeigen

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

+ 48
- 0
Client/src/main/java/IdController.java Datei anzeigen

@@ -0,0 +1,48 @@
1
+
2
+import java.io.BufferedReader;
3
+import java.io.IOException;
4
+import java.io.InputStreamReader;
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
8
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
9
+import com.fasterxml.jackson.core.JsonProcessingException;
10
+import com.fasterxml.jackson.core.type.TypeReference;
11
+import com.fasterxml.jackson.databind.ObjectMapper;
12
+import okhttp3.*;
13
+
14
+public class IdController extends Controller<IDs>{
15
+
16
+     IDs iDs;
17
+
18
+
19
+    public String getAllIDs(String resource) throws IOException{
20
+        responseBody = get(resource);
21
+        return unpackIDs(responseBody);
22
+    }
23
+
24
+    public String postID(String mainrl, String name, String github) {
25
+        return doPostRequest(mainrl, packID(name, github));
26
+    }
27
+
28
+
29
+
30
+    public String unpackIDs(String responseBody) throws IOException {
31
+            list = objectMapper.readValue(responseBody, new TypeReference<List<IDs>>() {});
32
+
33
+            list.forEach(iDs1 -> System.out.println(builder.append(iDs1 + "\n").toString()));
34
+
35
+            return builder.toString();
36
+    }
37
+
38
+    public String packID(String name, String github) {
39
+        iDs = new IDs(name, github);
40
+        try {
41
+            jsonObject = objectMapper.writeValueAsString(iDs);
42
+        } catch (JsonProcessingException e) {
43
+            e.printStackTrace();
44
+        }
45
+        return jsonObject;
46
+    }
47
+
48
+}

+ 85
- 0
Client/src/main/java/MessageController.java Datei anzeigen

@@ -0,0 +1,85 @@
1
+
2
+import com.fasterxml.jackson.core.JsonProcessingException;
3
+import com.fasterxml.jackson.core.type.TypeReference;
4
+import okhttp3.OkHttpClient;
5
+import okhttp3.Request;
6
+import okhttp3.Response;
7
+import okhttp3.MediaType;
8
+import okhttp3.RequestBody;
9
+
10
+import java.io.IOException;
11
+import java.util.List;
12
+
13
+public class MessageController extends Controller<Messages> {
14
+
15
+    private Messages messages;
16
+
17
+    public String getAllMessages(String resource) throws IOException{
18
+        responseBody = get(resource);
19
+        return unpackMessages(responseBody);
20
+    }
21
+
22
+    public String getMyMessages(String resource) throws IOException{
23
+        responseBody = get(resource);
24
+        return unpackMessages(responseBody);
25
+    }
26
+
27
+    public String postMessageToAll(String url, String fromid, String message) {
28
+        return doPostRequest(url, packMessagesToAll(fromid, message));
29
+    }
30
+
31
+    public String postMessageToFriend(String url, String fromid,String toid, String message) {
32
+        return doPostRequest(url, packMessagesToFriend(fromid, toid, message));
33
+    }
34
+
35
+    public String getMessages(String resource) throws IOException{
36
+        client = new OkHttpClient();
37
+        Request request = new Request.Builder().url(uRL + resource).get().build();
38
+
39
+        Response response = null;
40
+
41
+        try {
42
+            response = client.newCall(request).execute();
43
+            responseBody = response.body().string();
44
+
45
+        } catch (IOException e) {
46
+            e.printStackTrace();
47
+        }
48
+        return responseBody;
49
+    }
50
+
51
+    public String unpackMessages(String responseBody) throws IOException {
52
+        list = objectMapper.readValue(responseBody, new TypeReference<List<Messages>>() {});
53
+
54
+        list.forEach(messages1 -> System.out.println(builder.append(messages1.toString() + "\n")));
55
+
56
+        return builder.toString();
57
+    }
58
+
59
+
60
+    public String packMessagesToAll(String fromid, String message) {
61
+        messages = new Messages(fromid, message);
62
+        try {
63
+            jsonObject = objectMapper.writeValueAsString(messages);
64
+        } catch (JsonProcessingException e) {
65
+            e.printStackTrace();
66
+        }
67
+        return jsonObject;
68
+    }
69
+
70
+    public String packMessagesToFriend(String fromid, String toid, String message) {
71
+        messages = new Messages(fromid, toid, message);
72
+        try {
73
+            jsonObject = objectMapper.writeValueAsString(messages);
74
+        } catch (JsonProcessingException e) {
75
+            e.printStackTrace();
76
+        }
77
+        return jsonObject;
78
+    }
79
+
80
+
81
+
82
+
83
+
84
+
85
+}

+ 43
- 0
Client/src/main/java/Messages.java Datei anzeigen

@@ -0,0 +1,43 @@
1
+import sun.net.idn.StringPrep;
2
+
3
+public class Messages extends Model{
4
+
5
+    private String sequence;
6
+    private String timestamp;
7
+    private String fromid;
8
+    private String toid;
9
+    private String message;
10
+
11
+    public Messages(String fromid, String toid, String message) {
12
+        this.fromid = fromid;
13
+        this.toid = toid;
14
+        this.message = message;
15
+
16
+    }
17
+
18
+    public Messages(String fromid, String message) {
19
+        this.fromid = fromid;
20
+        this.toid = timestamp;
21
+    }
22
+
23
+    public String getFromid() {
24
+        return fromid;
25
+    }
26
+
27
+    public void setFromid(String fromid) {
28
+        this.fromid = fromid;
29
+    }
30
+
31
+    public String getToid() {
32
+        return toid;
33
+    }
34
+
35
+    public void setToid(String toid) {
36
+        this.toid = toid;
37
+    }
38
+
39
+    @Override
40
+    public String toString() {
41
+        return fromid + ": " + toid + ": " + message + ": " + timestamp;
42
+    }
43
+}

+ 5
- 0
Client/src/main/java/Model.java Datei anzeigen

@@ -0,0 +1,5 @@
1
+
2
+
3
+public class Model {
4
+
5
+}

+ 31
- 2
Client/src/main/java/SimpleShell.java Datei anzeigen

@@ -65,6 +65,12 @@ public class SimpleShell {
65 65
                     continue;
66 66
                 }
67 67
 
68
+                if (list.contains("send")) {
69
+                    String results = webber.post_id(commands[1], commands[2]);
70
+                    SimpleShell.prettyPrint(results);
71
+                    continue;
72
+                }
73
+
68 74
                 // messages
69 75
                 if (list.contains("messages")) {
70 76
                     String results = webber.get_messages();
@@ -73,6 +79,28 @@ public class SimpleShell {
73 79
                 }
74 80
                 // you need to add a bunch more.
75 81
 
82
+                if (list.contains("messages github")) {
83
+                    String results = webber.getMyMessages(commands[2]);
84
+                    SimpleShell.prettyPrint(results);
85
+                    continue;
86
+                }
87
+
88
+                if (list.contains("send github")) {
89
+                    String messageToAll = null;
90
+                    for (int i = 3; i < commands.length - 1; i++) {
91
+                        messageToAll += commands[i] + " ";
92
+                    }
93
+
94
+                    String results = webber.postMessageToAll(commands[2], messageToAll );
95
+                    SimpleShell.prettyPrint(results);
96
+                    continue;
97
+                }
98
+
99
+                if (list.contains("send github to github")) {
100
+                    String results = webber.postMessageToFriend(commands[1], commands[3], commands[2]);
101
+                    SimpleShell.prettyPrint(results);
102
+                    continue;
103
+                }
76 104
                 //!! command returns the last command in history
77 105
                 if (list.get(list.size() - 1).equals("!!")) {
78 106
                     pb.command(history.get(history.size() - 2));
@@ -103,10 +131,11 @@ public class SimpleShell {
103 131
 
104 132
             }
105 133
 
106
-            //catch ioexception, output appropriate message, resume waiting for input
107
-            catch (IOException e) {
134
+            catch (ArrayIndexOutOfBoundsException | IOException e) {
108 135
                 System.out.println("Input Error, Please try again!");
109 136
             }
137
+            //catch ioexception, output appropriate message, resume waiting for input
138
+
110 139
             // So what, do you suppose, is the meaning of this comment?
111 140
             /** The steps are:
112 141
              * 1. parse the input to obtain the command and any parameters

+ 47
- 9
Client/src/main/java/YouAreEll.java Datei anzeigen

@@ -1,23 +1,61 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+import okhttp3.OkHttpClient;
3
+import okhttp3.Request;
4
+import okhttp3.RequestBody;
5
+import okhttp3.Response;
6
+
7
+import java.io.IOException;
8
+
1 9
 public class YouAreEll {
2 10
 
11
+    IdController idController;
12
+    MessageController messageController;
13
+
3 14
     YouAreEll() {
15
+        idController = new IdController();
16
+        messageController = new MessageController();
4 17
     }
5 18
 
6
-    public static void main(String[] args) {
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
+//        System.out.println(urlhandler.idController.getAllIDs("/ids"));
24
+        System.out.println(urlhandler.messageController.getMyMessages("/ids/The King of Lambdas/messages/"));
25
+//        System.out.println(urlhandler.messageController.postMessageToAll("/messages","The King of Lambdas",
26
+//                "rayskeez21", "The lambdagod strikes again!"));
27
+//        System.out.println(urlhandler.idController.postID("/ids", "Jason", "The King of Lambdas"));
28
+
29
+    }
30
+
31
+    public String get_ids() throws IOException {
32
+        return MakeURLCall(idController.getAllIDs("/ids"));
33
+    }
34
+
35
+    public String post_id(String name, String github) {
36
+        return MakeURLCall(idController.postID("/ids", name, github));
37
+    }
38
+
39
+    public String get_messages() throws IOException {
40
+        return MakeURLCall(messageController.getMessages("/messages"));
10 41
     }
11 42
 
12
-    public String get_ids() {
13
-        return MakeURLCall("/ids", "GET", "");
43
+    public String getMyMessages(String github) throws IOException {
44
+        return MakeURLCall(messageController.getMessages("/ids/" + github + "/messages/"));
14 45
     }
15 46
 
16
-    public String get_messages() {
17
-        return MakeURLCall("/messages", "GET", "");
47
+    public String getMessagesFromFriend(String github, String friendGithub) throws IOException {
48
+        return MakeURLCall(messageController.getMessages("/ids/" + github + "/from/" + friendGithub));
18 49
     }
19 50
 
20
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
21
-        return "nada";
51
+    public String postMessageToAll(String fromid, String message) {
52
+        return MakeURLCall(messageController.postMessageToAll("/ids/" + fromid + "/messages/", fromid, message));
22 53
     }
54
+
55
+    public String postMessageToFriend(String fromid, String toid, String message) {
56
+        return MakeURLCall(messageController.postMessageToFriend("/ids/" + toid + "/messages/", fromid, toid, message));
57
+    }
58
+
59
+    public String MakeURLCall( String method) { return method; }
23 60
 }
61
+

+ 7
- 1
YouAreEll.iml Datei anzeigen

@@ -1,6 +1,6 @@
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$">
@@ -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: org.apache.httpcomponents:httpclient:4.5.5" level="project" />
15
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.9" level="project" />
16
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
17
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
18
+    <orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.10.0" level="project" />
19
+    <orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
14 20
   </component>
15 21
 </module>

+ 22
- 0
pom.xml Datei anzeigen

@@ -12,11 +12,33 @@
12 12
         <module>Client</module>
13 13
     </modules>
14 14
 
15
+    <build>
16
+        <plugins>
17
+            <plugin>
18
+                <groupId>org.apache.maven.plugins</groupId>
19
+                <artifactId>maven-compiler-plugin</artifactId>
20
+                <configuration>
21
+                    <source>8</source>
22
+                    <target>8</target>
23
+                </configuration>
24
+            </plugin>
25
+        </plugins>
26
+    </build>
15 27
     <dependencies>
16 28
         <dependency>
17 29
             <groupId>com.fasterxml.jackson.core</groupId>
18 30
             <artifactId>jackson-databind</artifactId>
19 31
             <version>2.8.6</version>
20 32
         </dependency>
33
+        <dependency>
34
+            <groupId>org.apache.httpcomponents</groupId>
35
+            <artifactId>httpclient</artifactId>
36
+            <version>4.5.5</version>
37
+        </dependency>
38
+        <dependency>
39
+            <groupId>com.squareup.okhttp3</groupId>
40
+            <artifactId>okhttp</artifactId>
41
+            <version>3.10.0</version>
42
+        </dependency>
21 43
     </dependencies>
22 44
 </project>