Eric Barnaba пре 6 година
родитељ
комит
14acdee670

+ 1
- 1
Client/Client.iml Прегледај датотеку

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

+ 13
- 1
Client/pom.xml Прегледај датотеку

@@ -10,8 +10,20 @@
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>1.8</source>
20
+                    <target>1.8</target>
21
+                </configuration>
22
+            </plugin>
23
+        </plugins>
24
+    </build>
13 25
 
14
-<dependencies>
26
+    <dependencies>
15 27
     <dependency>
16 28
         <groupId>org.apache.httpcomponents</groupId>
17 29
         <artifactId>httpclient</artifactId>

+ 3
- 1
Client/src/main/java/Id.java Прегледај датотеку

@@ -3,6 +3,8 @@ public class Id {
3 3
     private String name;
4 4
     private String github;
5 5
 
6
+    public Id(){}
7
+
6 8
     public Id(String userid, String name, String github){
7 9
         this.userid=userid;
8 10
         this.name=name;
@@ -29,7 +31,7 @@ public class Id {
29 31
 
30 32
     @Override
31 33
     public String toString(){
32
-        return "User ID: " + this.userid + "Name: " + this.name +  "GitHub: " + this.github;
34
+        return String.format("Name: %-30s Github: %-30s ",this.name, this.github);
33 35
     }
34 36
 
35 37
 

+ 14
- 0
Client/src/main/java/Mapper.java Прегледај датотеку

@@ -0,0 +1,14 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+
3
+public class Mapper {
4
+    public static ObjectMapper mapper;
5
+    private static Mapper ourInstance = new Mapper();
6
+
7
+    public static Mapper getInstance() {
8
+        return ourInstance;
9
+    }
10
+
11
+    private Mapper() {
12
+        mapper = new ObjectMapper();
13
+    }
14
+}

+ 8
- 0
Client/src/main/java/Message.java Прегледај датотеку

@@ -5,6 +5,9 @@ public class Message {
5 5
     String toid;
6 6
     String message;
7 7
 
8
+    public Message (){}
9
+
10
+
8 11
     public Message(String fromid, String message){
9 12
         this.sequence = "-";
10 13
         this.timestamp="2018-03-21T01:00:00.0Z";
@@ -49,5 +52,10 @@ public class Message {
49 52
         return message;
50 53
     }
51 54
 
55
+    @Override
56
+    public String toString(){
57
+        return String.format("TimeStamp: %-30s From: %-30s To: %-30s Message: %s ",this.timestamp, this.fromid, this.toid, this.message);
58
+    }
59
+
52 60
 
53 61
 }

+ 6
- 0
Client/src/main/java/ObjectType.java Прегледај датотеку

@@ -0,0 +1,6 @@
1
+public enum ObjectType {
2
+
3
+    MESSAGE,
4
+    ID;
5
+
6
+}

+ 82
- 13
Client/src/main/java/SimpleShell.java Прегледај датотеку

@@ -1,16 +1,54 @@
1
+import com.fasterxml.jackson.core.type.TypeReference;
2
+import com.fasterxml.jackson.databind.JavaType;
3
+
1 4
 import java.io.BufferedReader;
2 5
 import java.io.IOException;
3 6
 import java.io.InputStream;
4 7
 import java.io.InputStreamReader;
5 8
 import java.util.ArrayList;
9
+import java.util.Collections;
6 10
 import java.util.List;
11
+import java.util.regex.Matcher;
12
+import java.util.regex.Pattern;
7 13
 
8 14
 public class SimpleShell {
9 15
 
10 16
 
11
-    public static void prettyPrint(String output) {
17
+    public static void prettyPrint(String output, ObjectType type) {
12 18
         // yep, make an effort to format things nicely, eh?
13
-        System.out.println(output);
19
+
20
+        if(type.equals(ObjectType.MESSAGE)){
21
+            try {
22
+                ArrayList<Message> messages = Mapper.mapper.readValue(output, new TypeReference<ArrayList<Message>>() {});
23
+                System.out.println();
24
+                messages.forEach(System.out::println);
25
+            }
26
+            catch(IOException ioe){
27
+                System.out.println(ioe.getMessage());
28
+            }
29
+        }
30
+        else if (type.equals(ObjectType.ID)){
31
+            try {
32
+                ArrayList<Id> ids = Mapper.mapper.readValue(output, new TypeReference<ArrayList<Id>>() {});
33
+                System.out.println();
34
+                ids.forEach(System.out::println);
35
+            }
36
+            catch(IOException ioe){
37
+                System.out.println(ioe.getMessage());
38
+            }
39
+        }
40
+        else System.out.println("Something has gone wrong");
41
+
42
+//        try {
43
+//            ArrayList<JSONObject> messages = Mapper.mapper.readValue(output, new TypeReference<ArrayList<JSONObject>>() {});
44
+//            for(JSONObject j : messages){
45
+//                System.out.println(j);
46
+//            }
47
+//        }
48
+//        catch(IOException ioe){
49
+//            System.out.println(ioe.getMessage());
50
+//        }
51
+
14 52
     }
15 53
     public static void main(String[] args) throws java.io.IOException {
16 54
 
@@ -41,11 +79,7 @@ public class SimpleShell {
41 79
             }
42 80
 
43 81
             //loop through to see if parsing worked
44
-            for (int i = 0; i < commands.length; i++) {
45
-                //System.out.println(commands[i]); //***check to see if parsing/split worked***
46
-                list.add(commands[i]);
47
-
48
-            }
82
+            Collections.addAll(list, commands);
49 83
             System.out.print(list); //***check to see if list was added correctly***
50 84
             history.addAll(list);
51 85
             try {
@@ -59,16 +93,51 @@ public class SimpleShell {
59 93
                 // Specific Commands.
60 94
 
61 95
                 // ids
62
-                if (list.contains("ids")) {
63
-                    String results = webber.get_ids();
64
-                    SimpleShell.prettyPrint(results);
96
+                if (list.get(0).equals("ids")) {
97
+                    if (list.size()==3){
98
+                        String userName = list.get(1);
99
+                        String gitHub = list.get(2);
100
+                        String payload = Mapper.mapper.writeValueAsString(new Id(userName,gitHub));
101
+                        webber.MakeURLCall("/ids","POST", payload);
102
+                    }
103
+                    else if (list.size()==1) {
104
+                        String results = webber.get_ids();
105
+                        SimpleShell.prettyPrint(results, ObjectType.ID);
106
+                    }
107
+                    else System.out.println("Invalid Command");
65 108
                     continue;
66 109
                 }
67 110
 
68 111
                 // messages
69
-                if (list.contains("messages")) {
70
-                    String results = webber.get_messages();
71
-                    SimpleShell.prettyPrint(results);
112
+                if (list.get(0).equals("messages")) {
113
+                    if(list.size()==2){
114
+                        String id = list.get(1);
115
+                        SimpleShell.prettyPrint(webber.MakeURLCall("/ids/" + id + "/messages", "GET", ""),ObjectType.MESSAGE);
116
+                    }
117
+                    else if(list.size()==1) {
118
+                        String results = webber.get_messages();
119
+                        SimpleShell.prettyPrint(results, ObjectType.MESSAGE);
120
+                    }
121
+                    else System.out.println("Invalid Command");
122
+                    continue;
123
+                }
124
+
125
+                if(list.get(0).equals("send")){
126
+                    Pattern messageNoTo = Pattern.compile("'(.*)'");
127
+                    Pattern messageTo = Pattern.compile("'(.*)' (to) (.*)");
128
+                    Matcher withTo = messageTo.matcher(commandLine);
129
+                    Matcher noTo = messageNoTo.matcher(commandLine);
130
+                    String from = list.get(1);
131
+                    if(withTo.find()){
132
+                        String to = withTo.group(3);
133
+                        String payload = Mapper.mapper.writeValueAsString(new Message(from, to, withTo.group(1)));
134
+                        webber.MakeURLCall("/ids/" + to + "/messages", "POST", payload);
135
+                    }
136
+                    else if(noTo.find()) {
137
+                        String payload = Mapper.mapper.writeValueAsString(new Message(from, noTo.group(1)));
138
+                        webber.MakeURLCall("/ids/" + from + "/messages", "POST", payload);
139
+                    }
140
+                    else System.out.println("Invalid message format");
72 141
                     continue;
73 142
                 }
74 143
                 // you need to add a bunch more.

+ 5
- 6
Client/src/main/java/YouAreEll.java Прегледај датотеку

@@ -13,14 +13,13 @@ public class YouAreEll {
13 13
 
14 14
 
15 15
     YouAreEll() {
16
-
17 16
     }
18 17
 
19 18
     public static void main(String[] args) {
20
-        ObjectMapper mapper = new ObjectMapper();
19
+
21 20
         YouAreEll urlhandler = new YouAreEll();
22
-//        Id ericB = new Id("EricB", "EricBarnaba");
23
-//        Message testMessage = new Message("EricBarnaba", "Now that I've made contact with the world it's time for dinner!");
21
+//        Id stinkyPete = new Id("Stinky Pete", "StinkyPete");
22
+//        Message testMessage = new Message("EricBarnaba","JoeHendricks415", "Chicken on a BISCUIT");
24 23
 //        try {
25 24
 //            String json = mapper.writeValueAsString(testMessage);
26 25
 //            urlhandler.MakeURLCall("/ids/EricBarnaba/messages", "POST", json );
@@ -30,8 +29,8 @@ public class YouAreEll {
30 29
 //            System.out.println(jpe.getMessage());
31 30
 //        }
32 31
 
33
-        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
34
-        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
32
+        System.out.println(urlhandler.get_ids());
33
+        System.out.println(urlhandler.get_messages());
35 34
     }
36 35
 
37 36
     public String get_ids() {