Browse Source

Merge 21d0ecac4bdee23d0bb0d3023448991063a5d37a into bd1fca498ef63809a3a7a9e8867a8c20219bafbb

EricBarnaba 6 years ago
parent
commit
8152b25b67
No account linked to committer's email

+ 9
- 3
Client/Client.iml View File

1
 <?xml version="1.0" encoding="UTF-8"?>
1
 <?xml version="1.0" encoding="UTF-8"?>
2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
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
     <output url="file://$MODULE_DIR$/target/classes" />
4
     <output url="file://$MODULE_DIR$/target/classes" />
5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
     <content url="file://$MODULE_DIR$">
6
     <content url="file://$MODULE_DIR$">
7
       <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
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
       <excludeFolder url="file://$MODULE_DIR$/target" />
8
       <excludeFolder url="file://$MODULE_DIR$/target" />
11
     </content>
9
     </content>
12
     <orderEntry type="inheritedJdk" />
10
     <orderEntry type="inheritedJdk" />
13
     <orderEntry type="sourceFolder" forTests="false" />
11
     <orderEntry type="sourceFolder" forTests="false" />
12
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.5" level="project" />
13
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.9" level="project" />
14
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
15
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
16
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient-cache:4.5.5" level="project" />
17
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.5.5" level="project" />
18
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:fluent-hc:4.5.5" level="project" />
19
+    <orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
14
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
20
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
21
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
22
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />

+ 45
- 0
Client/pom.xml View File

10
     <modelVersion>4.0.0</modelVersion>
10
     <modelVersion>4.0.0</modelVersion>
11
 
11
 
12
     <artifactId>Client</artifactId>
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>
25
+
26
+    <dependencies>
27
+    <dependency>
28
+        <groupId>org.apache.httpcomponents</groupId>
29
+        <artifactId>httpclient</artifactId>
30
+        <version>4.5.5</version>
31
+    </dependency>
32
+    <dependency>
33
+        <groupId>org.apache.httpcomponents</groupId>
34
+        <artifactId>httpclient-cache</artifactId>
35
+        <version>4.5.5</version>
36
+    </dependency>
37
+    <dependency>
38
+        <groupId>org.apache.httpcomponents</groupId>
39
+        <artifactId>httpmime</artifactId>
40
+        <version>4.5.5</version>
41
+    </dependency>
42
+    <dependency>
43
+        <groupId>org.apache.httpcomponents</groupId>
44
+        <artifactId>fluent-hc</artifactId>
45
+        <version>4.5.5</version>
46
+    </dependency>
47
+    <dependency>
48
+        <groupId>org.json</groupId>
49
+        <artifactId>json</artifactId>
50
+        <version>20140107</version>
51
+    </dependency>
52
+    <dependency>
53
+        <groupId>com.fasterxml.jackson.core</groupId>
54
+        <artifactId>jackson-databind</artifactId>
55
+        <version>2.8.6</version>
56
+    </dependency>
57
+</dependencies>
13
 
58
 
14
 
59
 
15
 </project>
60
 </project>

+ 40
- 0
Client/src/main/java/Id.java View File

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

+ 11
- 0
Client/src/main/java/IdCommand.java View File

1
+public enum IdCommand {
2
+
3
+    GET_ALL,
4
+    POST_ID;
5
+
6
+
7
+
8
+
9
+
10
+
11
+}

+ 47
- 0
Client/src/main/java/IdController.java View File

1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+
3
+public class IdController implements Runnable {
4
+
5
+    private IdCommand command;
6
+    private String[] commands;
7
+
8
+
9
+    public IdController(IdCommand command, String commandLine){
10
+        this.command = command;
11
+        this.commands = commandLine.split(" ");
12
+    }
13
+
14
+    public IdController(IdCommand command){
15
+        this.command = command;
16
+    }
17
+
18
+
19
+    @Override
20
+    public void run() {
21
+        if(this.command.equals(IdCommand.GET_ALL)){
22
+            getAll();
23
+        }
24
+        else if (this.command.equals(IdCommand.POST_ID)){
25
+            postId();
26
+        }
27
+    }
28
+
29
+    private void getAll(){
30
+        SimpleShell.prettyPrint(YouAreEll.get_ids(), ObjectType.ID);
31
+    }
32
+
33
+    private void postId(){
34
+        String userName = commands[1];
35
+        String gitHub = commands[2];
36
+        try {
37
+            String payload = Mapper.mapper.writeValueAsString(new Id(userName, gitHub));
38
+            YouAreEll.MakeURLCall("/ids", "POST", payload);
39
+        }
40
+        catch(JsonProcessingException jpe){
41
+            System.out.println(jpe.getMessage());
42
+        }
43
+    }
44
+
45
+
46
+
47
+}

+ 14
- 0
Client/src/main/java/Mapper.java View File

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

+ 61
- 0
Client/src/main/java/Message.java View File

1
+public class Message {
2
+    String sequence;
3
+    String timestamp;
4
+    String fromid;
5
+    String toid;
6
+    String message;
7
+
8
+    public Message (){}
9
+
10
+
11
+    public Message(String fromid, String message){
12
+        this.sequence = "-";
13
+        this.timestamp="2018-03-21T01:00:00.0Z";
14
+        this.fromid = fromid;
15
+        this.toid = "";
16
+        this.message = message;
17
+    }
18
+
19
+    public Message(String fromid, String toid, String message){
20
+        this.sequence = "-";
21
+        this.timestamp="2018-03-21T01:00:00.0Z";
22
+        this.fromid = fromid;
23
+        this.toid = toid;
24
+        this.message = message;
25
+    }
26
+
27
+    public Message(String sequence, String timestamp, String fromid, String toid, String message){
28
+        this.sequence = sequence;
29
+        this.timestamp=timestamp;
30
+        this.fromid = fromid;
31
+        this.toid = toid;
32
+        this.message = message;
33
+    }
34
+
35
+    public String getSequence() {
36
+        return sequence;
37
+    }
38
+
39
+    public String getTimestamp() {
40
+        return timestamp;
41
+    }
42
+
43
+    public String getFromid() {
44
+        return fromid;
45
+    }
46
+
47
+    public String getToid() {
48
+        return toid;
49
+    }
50
+
51
+    public String getMessage() {
52
+        return message;
53
+    }
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
+
60
+
61
+}

+ 9
- 0
Client/src/main/java/MessageCommand.java View File

1
+public enum MessageCommand {
2
+
3
+    GET_ALL,
4
+    GET_TO_SPECIFIC,
5
+    SEND,
6
+    WAIT;
7
+
8
+
9
+}

+ 76
- 0
Client/src/main/java/MessageController.java View File

1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+
3
+import java.util.regex.Matcher;
4
+import java.util.regex.Pattern;
5
+
6
+public class MessageController implements Runnable {
7
+
8
+    private MessageCommand command;
9
+    private String commandLine;
10
+    private String[] commands;
11
+
12
+    public MessageController(MessageCommand command, String commandLine){
13
+        this.command = command;
14
+        this.commandLine = commandLine;
15
+        this.commands = commandLine.split(" ");
16
+    }
17
+
18
+    @Override
19
+    public void run() {
20
+        if(command.equals(MessageCommand.GET_ALL)){
21
+            getAll();
22
+        }
23
+        else if (command.equals(MessageCommand.GET_TO_SPECIFIC)){
24
+            getToSpecific();
25
+        }
26
+        else if (command.equals(MessageCommand.SEND)){
27
+            sendMessage();
28
+        }
29
+        else if (command.equals(MessageCommand.WAIT)){
30
+            waitTest();
31
+        }
32
+
33
+    }
34
+
35
+    private void getAll(){
36
+        SimpleShell.prettyPrint(YouAreEll.get_messages(),ObjectType.MESSAGE);
37
+    }
38
+
39
+    private void getToSpecific(){
40
+        String id = commands[1];
41
+        SimpleShell.prettyPrint(YouAreEll.MakeURLCall("/ids/" + id + "/messages", "GET", ""), ObjectType.MESSAGE);
42
+    }
43
+
44
+    private void sendMessage(){
45
+        Pattern messageNoTo = Pattern.compile("'(.*)'");
46
+        Pattern messageTo = Pattern.compile("'(.*)' (to) (.*)");
47
+        Matcher withTo = messageTo.matcher(commandLine);
48
+        Matcher noTo = messageNoTo.matcher(commandLine);
49
+        String from = commands[1];
50
+        try {
51
+            if (withTo.find()) {
52
+                String to = withTo.group(3);
53
+                String payload = Mapper.mapper.writeValueAsString(new Message(from, to, withTo.group(1)));
54
+                YouAreEll.MakeURLCall("/ids/" + to + "/messages", "POST", payload);
55
+            } else if (noTo.find()) {
56
+                String payload = Mapper.mapper.writeValueAsString(new Message(from, noTo.group(1)));
57
+                YouAreEll.MakeURLCall("/ids/" + from + "/messages", "POST", payload);
58
+            } else System.out.println("Invalid message format");
59
+        }
60
+        catch(JsonProcessingException jpe){
61
+            System.out.println(jpe.getMessage());
62
+        }
63
+    }
64
+
65
+    private void waitTest(){
66
+        try {
67
+            Thread.sleep(20000);
68
+            System.out.println("Waking Up");
69
+        }
70
+        catch(InterruptedException ie){
71
+            System.out.println(ie.getMessage());
72
+        }
73
+    }
74
+
75
+
76
+}

+ 6
- 0
Client/src/main/java/ObjectType.java View File

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

+ 52
- 15
Client/src/main/java/SimpleShell.java View File

1
+import com.fasterxml.jackson.core.type.TypeReference;
2
+
1
 import java.io.BufferedReader;
3
 import java.io.BufferedReader;
2
 import java.io.IOException;
4
 import java.io.IOException;
3
 import java.io.InputStream;
5
 import java.io.InputStream;
4
 import java.io.InputStreamReader;
6
 import java.io.InputStreamReader;
5
 import java.util.ArrayList;
7
 import java.util.ArrayList;
8
+import java.util.Collections;
6
 import java.util.List;
9
 import java.util.List;
10
+import java.util.regex.Matcher;
11
+import java.util.regex.Pattern;
7
 
12
 
8
 public class SimpleShell {
13
 public class SimpleShell {
9
 
14
 
10
 
15
 
11
-    public static void prettyPrint(String output) {
16
+    public static void prettyPrint(String output, ObjectType type) {
12
         // yep, make an effort to format things nicely, eh?
17
         // yep, make an effort to format things nicely, eh?
13
-        System.out.println(output);
18
+
19
+        if (type.equals(ObjectType.MESSAGE)) {
20
+            try {
21
+                ArrayList<Message> messages = Mapper.mapper.readValue(output, new TypeReference<ArrayList<Message>>() {
22
+                });
23
+                System.out.println();
24
+                messages.forEach(System.out::println);
25
+            } catch (IOException ioe) {
26
+                System.out.println(ioe.getMessage());
27
+            }
28
+        } else if (type.equals(ObjectType.ID)) {
29
+            try {
30
+                ArrayList<Id> ids = Mapper.mapper.readValue(output, new TypeReference<ArrayList<Id>>() {
31
+                });
32
+                System.out.println();
33
+                ids.forEach(System.out::println);
34
+            } catch (IOException ioe) {
35
+                System.out.println(ioe.getMessage());
36
+            }
37
+        } else System.out.println("Something has gone wrong");
38
+
14
     }
39
     }
40
+
15
     public static void main(String[] args) throws java.io.IOException {
41
     public static void main(String[] args) throws java.io.IOException {
16
 
42
 
17
-        YouAreEll webber = new YouAreEll();
43
+        //YouAreEll webber = new YouAreEll();
18
         String commandLine;
44
         String commandLine;
19
         BufferedReader console = new BufferedReader
45
         BufferedReader console = new BufferedReader
20
                 (new InputStreamReader(System.in));
46
                 (new InputStreamReader(System.in));
41
             }
67
             }
42
 
68
 
43
             //loop through to see if parsing worked
69
             //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
-            }
70
+            Collections.addAll(list, commands);
49
             System.out.print(list); //***check to see if list was added correctly***
71
             System.out.print(list); //***check to see if list was added correctly***
50
             history.addAll(list);
72
             history.addAll(list);
51
             try {
73
             try {
59
                 // Specific Commands.
81
                 // Specific Commands.
60
 
82
 
61
                 // ids
83
                 // ids
62
-                if (list.contains("ids")) {
63
-                    String results = webber.get_ids();
64
-                    SimpleShell.prettyPrint(results);
84
+                if (list.get(0).equals("ids")) {
85
+                    if (list.size() == 3) {
86
+                        new Thread(new IdController(IdCommand.POST_ID,commandLine)).start();
87
+                    } else if (list.size() == 1) {
88
+                        new Thread(new IdController(IdCommand.GET_ALL)).start();
89
+                    } else System.out.println("Invalid Command");
65
                     continue;
90
                     continue;
66
                 }
91
                 }
67
 
92
 
68
                 // messages
93
                 // messages
69
-                if (list.contains("messages")) {
70
-                    String results = webber.get_messages();
71
-                    SimpleShell.prettyPrint(results);
94
+                if (list.get(0).equals("messages")) {
95
+                    if (list.size() == 2) {
96
+                        new Thread(new MessageController(MessageCommand.GET_TO_SPECIFIC, commandLine)).start();
97
+                    } else if (list.size() == 1) {
98
+                        new Thread(new MessageController(MessageCommand.GET_ALL, commandLine)).start();
99
+                    } else System.out.println("Invalid Command");
100
+                    continue;
101
+                }
102
+
103
+                if (list.get(0).equals("send")) {
104
+                    new Thread(new MessageController(MessageCommand.SEND, commandLine)).start();
72
                     continue;
105
                     continue;
73
                 }
106
                 }
107
+
108
+                if (list.get(0).equals("wait")) {
109
+                    new Thread(new MessageController(MessageCommand.WAIT, commandLine)).start();
110
+                }
74
                 // you need to add a bunch more.
111
                 // you need to add a bunch more.
75
 
112
 
76
                 //!! command returns the last command in history
113
                 //!! command returns the last command in history
86
                     pb.command(list);
123
                     pb.command(list);
87
                 }
124
                 }
88
 
125
 
89
-                // wait, wait, what curiousness is this?
90
                 Process process = pb.start();
126
                 Process process = pb.start();
91
 
127
 
92
                 //obtain the input stream
128
                 //obtain the input stream
96
 
132
 
97
                 //read output of the process
133
                 //read output of the process
98
                 String line;
134
                 String line;
135
+
99
                 while ((line = br.readLine()) != null)
136
                 while ((line = br.readLine()) != null)
100
                     System.out.println(line);
137
                     System.out.println(line);
101
                 br.close();
138
                 br.close();

+ 57
- 8
Client/src/main/java/YouAreEll.java View File

1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import org.apache.http.client.fluent.Request;
3
+import org.apache.http.entity.ContentType;
4
+import java.io.IOException;
5
+
1
 public class YouAreEll {
6
 public class YouAreEll {
2
 
7
 
8
+
9
+
3
     YouAreEll() {
10
     YouAreEll() {
4
     }
11
     }
5
 
12
 
6
-    public static void main(String[] args) {
7
-        YouAreEll urlhandler = new YouAreEll();
8
-        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9
-        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
10
-    }
13
+//    public static void main(String[] args) {
14
+//
15
+//        YouAreEll urlhandler = new YouAreEll();
16
+//        Id stinkyPete = new Id("Stinky Pete", "StinkyPete");
17
+//        try {
18
+//            String json = Mapper.mapper.writeValueAsString(stinkyPete);
19
+//            urlhandler.MakeURLCall("/ids", "PUT", json );
20
+//        }
21
+//        catch(JsonProcessingException jpe){
22
+//            System.out.println(jpe.getMessage());
23
+//        }
24
+//
25
+////        System.out.println(urlhandler.get_ids());
26
+////        System.out.println(urlhandler.get_messages());
27
+//    }
11
 
28
 
12
-    public String get_ids() {
29
+    public static String get_ids() {
13
         return MakeURLCall("/ids", "GET", "");
30
         return MakeURLCall("/ids", "GET", "");
14
     }
31
     }
15
 
32
 
16
-    public String get_messages() {
33
+    public static String get_messages() {
17
         return MakeURLCall("/messages", "GET", "");
34
         return MakeURLCall("/messages", "GET", "");
18
     }
35
     }
19
 
36
 
20
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
37
+    public static String MakeURLCall(String mainurl, String method, String jpayload) {
38
+        String url = "http://zipcode.rocks:8085" + mainurl;
39
+
40
+        if(method.equals("GET")){
41
+            try{
42
+                return Request.Get(url).execute().returnContent().asString();
43
+            }
44
+            catch(IOException ioe){
45
+                System.out.println(ioe.getMessage());
46
+            }
47
+
48
+        }
49
+        else if (method.equals("POST")){
50
+            try {
51
+                return Request.Post(url)
52
+                        .bodyString(jpayload, ContentType.APPLICATION_JSON)
53
+                        .execute().returnContent().asString();
54
+            }
55
+            catch(IOException ioe){
56
+                System.out.println(ioe.getMessage());
57
+            }
58
+        }
59
+
60
+        else if (method.equals("PUT")){
61
+            try {
62
+                return Request.Put(url)
63
+                        .useExpectContinue().bodyString(jpayload, ContentType.APPLICATION_JSON)
64
+                        .execute().returnContent().asString();
65
+            }
66
+            catch(IOException ioe){
67
+                System.out.println(ioe.getMessage());
68
+            }
69
+        }
21
         return "nada";
70
         return "nada";
22
     }
71
     }
23
 }
72
 }