thulasi пре 6 година
родитељ
комит
af9445e008

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

@@ -1,16 +1,26 @@
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: com.mashape.unirest:unirest-java:1.4.9" level="project" />
13
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
14
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
15
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
16
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
17
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.2" level="project" />
18
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.3.2" level="project" />
19
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.3.6" level="project" />
20
+    <orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
21
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
22
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
23
+    <orderEntry type="library" scope="TEST" name="Maven: commons-io:commons-io:2.4" level="project" />
14 24
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15 25
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16 26
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />

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

@@ -11,5 +11,44 @@
11 11
 
12 12
     <artifactId>Client</artifactId>
13 13
 
14
+    <dependencies>
15
+        <dependency>
16
+        <groupId>com.mashape.unirest</groupId>
17
+        <artifactId>unirest-java</artifactId>
18
+        <version>1.4.9</version>
19
+        </dependency>
14 20
 
21
+        <dependency>
22
+            <groupId>org.apache.httpcomponents</groupId>
23
+            <artifactId>httpclient</artifactId>
24
+            <version>4.3.6</version>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>org.apache.httpcomponents</groupId>
28
+            <artifactId>httpasyncclient</artifactId>
29
+            <version>4.0.2</version>
30
+        </dependency>
31
+        <dependency>
32
+            <groupId>org.apache.httpcomponents</groupId>
33
+            <artifactId>httpmime</artifactId>
34
+            <version>4.3.6</version>
35
+        </dependency>
36
+        <dependency>
37
+            <groupId>org.json</groupId>
38
+            <artifactId>json</artifactId>
39
+            <version>20140107</version>
40
+        </dependency>
41
+        <dependency>
42
+            <groupId>junit</groupId>
43
+            <artifactId>junit</artifactId>
44
+            <version>4.11</version>
45
+            <scope>test</scope>
46
+        </dependency>
47
+        <dependency>
48
+            <groupId>commons-io</groupId>
49
+            <artifactId>commons-io</artifactId>
50
+            <version>2.4</version>
51
+            <scope>test</scope>
52
+        </dependency>
53
+    </dependencies>
15 54
 </project>

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

@@ -0,0 +1,31 @@
1
+import com.mashape.unirest.http.JsonNode;
2
+
3
+import java.util.Scanner;
4
+
5
+public class Console {
6
+    Scanner in;
7
+
8
+    public Console(){
9
+        in = new Scanner(System.in);
10
+    }
11
+
12
+    public String readString(String message){
13
+        System.out.println(message);
14
+        return in.nextLine();
15
+    }
16
+
17
+    public void printAll(JsonNode body){
18
+        System.out.println(body);
19
+    }
20
+
21
+
22
+    public String printMenu() {
23
+        System.out.println("Enter 1 - create new user\n" +
24
+                "2 - get all users\n" +
25
+                "3 - send a new message\n" +
26
+                "4 - see all messages\n" +
27
+                "5 - update user information\n" +
28
+                "6 - exit");
29
+        return in.next();
30
+    }
31
+}

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

@@ -0,0 +1,46 @@
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+
4
+public class Id {
5
+    private String userid;
6
+    private String name;
7
+    private String github;
8
+
9
+    public Id(String name, String github) {
10
+        this.name = name;
11
+        this.github = github;
12
+    }
13
+
14
+    public String getUserid() {
15
+        return userid;
16
+    }
17
+
18
+    public void setUserid(String userid) {
19
+        this.userid = userid;
20
+    }
21
+
22
+    public String getName() {
23
+        return name;
24
+    }
25
+
26
+    public void setName(String name) {
27
+        this.name = name;
28
+    }
29
+
30
+    public String getGithub() {
31
+        return github;
32
+    }
33
+
34
+    public void setGithub(String github) {
35
+        this.github = github;
36
+    }
37
+
38
+    @Override
39
+    public String toString() {
40
+        try {
41
+            return new ObjectMapper().writeValueAsString(this);
42
+        } catch (JsonProcessingException e) {
43
+            throw new Error(e);
44
+        }
45
+    }
46
+}

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

@@ -0,0 +1,26 @@
1
+import com.mashape.unirest.http.HttpResponse;
2
+import com.mashape.unirest.http.JsonNode;
3
+import com.mashape.unirest.http.Unirest;
4
+import com.mashape.unirest.http.exceptions.UnirestException;
5
+
6
+public class IdController {
7
+    private Console console;
8
+
9
+    public IdController(){
10
+        console = new Console();
11
+    }
12
+
13
+
14
+    public Id createId(){
15
+        Id id = new Id(readName(), readGithub());
16
+        return id;
17
+    }
18
+
19
+    private String readName(){
20
+        return console.readString("Enter name");
21
+    }
22
+
23
+    private String readGithub(){
24
+        return console.readString("Enter Github");
25
+    }
26
+}

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

@@ -0,0 +1,65 @@
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+
4
+public class Message {
5
+    String sequence;
6
+    String timestamp;
7
+    String fromid;
8
+    String toid;
9
+    String message;
10
+
11
+    public Message(String fromid, String toid, String message) {
12
+        this.fromid = fromid;
13
+        this.toid = toid;
14
+        this.message = message;
15
+    }
16
+
17
+    public String getSequence() {
18
+        return sequence;
19
+    }
20
+
21
+    public void setSequence(String sequence) {
22
+        this.sequence = sequence;
23
+    }
24
+
25
+    public String getTimestamp() {
26
+        return timestamp;
27
+    }
28
+
29
+    public void setTimestamp(String timestamp) {
30
+        this.timestamp = timestamp;
31
+    }
32
+
33
+    public String getFromid() {
34
+        return fromid;
35
+    }
36
+
37
+    public void setFromid(String fromid) {
38
+        this.fromid = fromid;
39
+    }
40
+
41
+    public String getToid() {
42
+        return toid;
43
+    }
44
+
45
+    public void setToid(String toid) {
46
+        this.toid = toid;
47
+    }
48
+
49
+    public String getMessage() {
50
+        return message;
51
+    }
52
+
53
+    public void setMessage(String message) {
54
+        this.message = message;
55
+    }
56
+
57
+    @Override
58
+    public String toString() {
59
+        try {
60
+            return new ObjectMapper().writeValueAsString(this);
61
+        } catch (JsonProcessingException e) {
62
+            throw new Error(e);
63
+        }
64
+    }
65
+}

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

@@ -0,0 +1,28 @@
1
+import com.mashape.unirest.http.HttpResponse;
2
+import com.mashape.unirest.http.JsonNode;
3
+import com.mashape.unirest.http.Unirest;
4
+import com.mashape.unirest.http.exceptions.UnirestException;
5
+
6
+public class MessageController {
7
+    private Console console;
8
+
9
+    public MessageController(){
10
+        console = new Console();
11
+    }
12
+
13
+    public Message createMessage(){
14
+        return new Message(readFromId(), readToId(), readMessage());
15
+    }
16
+
17
+    private String readMessage() {
18
+        return console.readString("Enter your message: ");
19
+    }
20
+
21
+    private String readToId() {
22
+        return console.readString("Enter ToId: ");
23
+    }
24
+
25
+    private String readFromId() {
26
+        return console.readString("Enter FromId: ");
27
+    }
28
+}

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

@@ -1,22 +1,112 @@
1
+import com.mashape.unirest.http.HttpResponse;
2
+import com.mashape.unirest.http.JsonNode;
3
+import com.mashape.unirest.http.Unirest;
4
+import com.mashape.unirest.http.exceptions.UnirestException;
5
+
1 6
 public class YouAreEll {
7
+    static Console console;
8
+    static IdController idController;
9
+    static MessageController messageController;
10
+    static String url;
2 11
 
3 12
     YouAreEll() {
13
+        console = new Console();
14
+        idController = new IdController();
15
+        messageController = new MessageController();
16
+        url = "http://zipcode.rocks:8085";
4 17
     }
5 18
 
6 19
     public static void main(String[] args) {
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("http://zipcode.rocks:8085/ids", "GET", ""));
22
+        System.out.println(urlhandler.MakeURLCall("http://zipcode.rocks:8085/messages", "GET", ""));*/
23
+
24
+        String choice = console.printMenu();
25
+        while(choice != null) {
26
+            switch (choice) {
27
+                case "1":
28
+                    postIds();
29
+                    break;
30
+                case "2":
31
+                    get_ids();
32
+                    break;
33
+                case "3":
34
+                    postMessages();
35
+                    break;
36
+                case "4":
37
+                    get_messages();
38
+                    break;
39
+                case "5":
40
+                    break;
41
+                case "6":
42
+                    System.exit(0);
43
+            }
44
+            choice = console.printMenu();
45
+        }
46
+    }
47
+
48
+    public static String get_ids() {
49
+        try {
50
+            HttpResponse<JsonNode> jsonResponse = Unirest.get(url + "/ids")
51
+                    .header("accept", "application/json")
52
+                    .asJson();
53
+            console.printAll(jsonResponse.getBody());
54
+            return jsonResponse.getBody().toString();
55
+
56
+        } catch (UnirestException e) {
57
+            return "Error";
58
+        }
59
+    }
60
+
61
+    public static void postIds() {
62
+        try {
63
+            Unirest.post(url + "/ids")
64
+                    .header("accept", "application/json")
65
+                    .body(idController.createId().toString())
66
+                    .asJson();
67
+        } catch (UnirestException e) {
68
+            e.printStackTrace();
69
+        }
10 70
     }
11 71
 
12
-    public String get_ids() {
13
-        return MakeURLCall("/ids", "GET", "");
72
+   /* public void putId(Id id){
73
+        try {
74
+            Unirest.put(url + "/ids")
75
+                    .header("accept", "application/json")
76
+                    .body(id.toString())
77
+                    .asJson();
78
+        } catch (UnirestException e) {
79
+            e.printStackTrace();
80
+        }
81
+
82
+    }*/
83
+
84
+    public static String get_messages() {
85
+        try {
86
+            HttpResponse<JsonNode> jsonResponse = Unirest.get(url + "/messages")
87
+                    .header("accept", "application/json")
88
+                    .asJson();
89
+
90
+            console.printAll(jsonResponse.getBody());
91
+            return jsonResponse.getBody().toString();
92
+
93
+        } catch (UnirestException e) {
94
+            return "Error";
95
+        }
14 96
     }
15 97
 
16
-    public String get_messages() {
17
-        return MakeURLCall("/messages", "GET", "");
98
+    public static void postMessages() {
99
+        try {
100
+            Unirest.post(url + "/ids")
101
+                    .header("accept", "application/json")
102
+                    .body(messageController.createMessage().toString())
103
+                    .asJson();
104
+        } catch (UnirestException e) {
105
+            e.printStackTrace();
106
+        }
18 107
     }
19 108
 
109
+
20 110
     public String MakeURLCall(String mainurl, String method, String jpayload) {
21 111
         return "nada";
22 112
     }