April Rivera 6 vuotta sitten
vanhempi
commit
c67276a813

+ 9
- 2
Client/Client.iml Näytä tiedosto

@@ -5,12 +5,19 @@
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.5.2" level="project" />
14
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.4" level="project" />
15
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
16
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.9" level="project" />
17
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.1.1" level="project" />
18
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.4.4" level="project" />
19
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.5.2" level="project" />
20
+    <orderEntry type="library" name="Maven: org.json:json:20160212" level="project" />
14 21
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15 22
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16 23
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />

+ 45
- 0
Client/src/main/java/Entity/Id.java Näytä tiedosto

@@ -0,0 +1,45 @@
1
+package Entity;
2
+
3
+public class Id {
4
+private Integer userId;
5
+private String name;
6
+private String githubId;
7
+
8
+    public Id() {
9
+    }
10
+
11
+    public Id(Integer userId, String name, String githubName) {
12
+        this.userId = userId;
13
+        this.name = name;
14
+        this.githubId = githubName;
15
+    }
16
+
17
+    public Integer getUserId() {
18
+        return 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 getGithubName() {
30
+        return githubId;
31
+    }
32
+
33
+    public void setGithubName(String githubName) {
34
+        this.githubId = githubId;
35
+    }
36
+
37
+    @Override
38
+    public String toString() {
39
+        return "Entity.Id{" +
40
+                "userId='" + userId + '\'' +
41
+                ", name='" + name + '\'' +
42
+                ", githubName='" + githubId + '\'' +
43
+                '}';
44
+    }
45
+}

+ 71
- 0
Client/src/main/java/Entity/Message.java Näytä tiedosto

@@ -0,0 +1,71 @@
1
+package Entity;
2
+
3
+public class Message {
4
+    private String sequence;
5
+    private String timestamp;
6
+    private String fromId;
7
+    private String toId;
8
+    private String message;
9
+
10
+    public Message() {
11
+    }
12
+
13
+    public Message(String sequence, String timestamp, String fromId, String toId, String message) {
14
+        this.sequence = sequence;
15
+        this.timestamp = timestamp;
16
+        this.fromId = fromId;
17
+        this.toId = toId;
18
+        this.message = message;
19
+    }
20
+
21
+    public String getSequence() {
22
+        return sequence;
23
+    }
24
+
25
+    public void setSequence(String sequence) {
26
+        this.sequence = sequence;
27
+    }
28
+
29
+    public String getTimestamp() {
30
+        return timestamp;
31
+    }
32
+
33
+    public void setTimestamp(String timestamp) {
34
+        this.timestamp = timestamp;
35
+    }
36
+
37
+    public String getFromId() {
38
+        return fromId;
39
+    }
40
+
41
+    public void setFromId(String fromId) {
42
+        this.fromId = fromId;
43
+    }
44
+
45
+    public String getToId() {
46
+        return toId;
47
+    }
48
+
49
+    public void setToId(String toId) {
50
+        this.toId = toId;
51
+    }
52
+
53
+    public String getMessage() {
54
+        return message;
55
+    }
56
+
57
+    public void setMessage(String message) {
58
+        this.message = message;
59
+    }
60
+
61
+    @Override
62
+    public String toString() {
63
+        return "Entity.Message{" +
64
+                "sequence='" + sequence + '\'' +
65
+                ", timestamp='" + timestamp + '\'' +
66
+                ", fromId='" + fromId + '\'' +
67
+                ", toId='" + toId + '\'' +
68
+                ", message='" + message + '\'' +
69
+                '}';
70
+    }
71
+}

+ 13
- 2
Client/src/main/java/SimpleShell.java Näytä tiedosto

@@ -1,3 +1,6 @@
1
+import Entity.Id;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+
1 4
 import java.io.BufferedReader;
2 5
 import java.io.IOException;
3 6
 import java.io.InputStream;
@@ -9,9 +12,17 @@ public class SimpleShell {
9 12
 
10 13
 
11 14
     public static void prettyPrint(String output) {
12
-        // yep, make an effort to format things nicely, eh?
13
-        System.out.println(output);
15
+        ObjectMapper jacksonMapper = new ObjectMapper();
16
+        try {
17
+            Object jsonObject = jacksonMapper.readValue(output, Id.class);
18
+            String prettyJson = jacksonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
19
+            System.out.println(prettyJson);
20
+        } catch (IOException e) {
21
+            e.printStackTrace();
22
+        }
23
+
14 24
     }
25
+
15 26
     public static void main(String[] args) throws java.io.IOException {
16 27
 
17 28
         YouAreEll webber = new YouAreEll();

+ 61
- 0
Client/src/main/java/YouAreEll.java Näytä tiedosto

@@ -1,3 +1,13 @@
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.mashape.unirest.http.HttpResponse;
3
+import com.mashape.unirest.http.JsonNode;
4
+import com.mashape.unirest.http.ObjectMapper;
5
+import com.mashape.unirest.http.Unirest;
6
+import com.mashape.unirest.http.exceptions.UnirestException;
7
+import com.mashape.unirest.request.HttpRequestWithBody;
8
+
9
+import java.io.IOException;
10
+
1 11
 public class YouAreEll {
2 12
 
3 13
     YouAreEll() {
@@ -5,6 +15,28 @@ public class YouAreEll {
5 15
 
6 16
     public static void main(String[] args) {
7 17
         YouAreEll urlhandler = new YouAreEll();
18
+
19
+        Unirest.setObjectMapper(new ObjectMapper() {
20
+            private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
21
+                    = new com.fasterxml.jackson.databind.ObjectMapper();
22
+
23
+            public <T> T readValue(String value, Class<T> valueType) {
24
+                try {
25
+                    return jacksonObjectMapper.readValue(value, valueType);
26
+                } catch (IOException e) {
27
+                    throw new RuntimeException(e);
28
+                }
29
+            }
30
+
31
+            public String writeValue(Object value) {
32
+                try {
33
+                    return jacksonObjectMapper.writeValueAsString(value);
34
+                } catch (JsonProcessingException e) {
35
+                    throw new RuntimeException(e);
36
+                }
37
+            }
38
+        });
39
+
8 40
         System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9 41
         System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
10 42
     }
@@ -18,6 +50,35 @@ public class YouAreEll {
18 50
     }
19 51
 
20 52
     public String MakeURLCall(String mainurl, String method, String jpayload) {
53
+        String url = "http://zipcode.rocks:8085" + mainurl;
54
+
55
+        if(method.equals("GET")) {
56
+            HttpResponse<JsonNode> getRequest = null;
57
+            try {
58
+                getRequest = Unirest.get(url).asJson();
59
+            } catch (UnirestException e) {
60
+                e.printStackTrace();
61
+            }
62
+            return getRequest.getBody().toString();
63
+        }
64
+        else if(method.equals("POST")){
65
+            try {
66
+                HttpResponse<JsonNode> post = Unirest.post(url)
67
+                        .body(jpayload)
68
+                        .asJson();
69
+            } catch (UnirestException e) {
70
+                e.printStackTrace();
71
+            }
72
+        }
73
+        else if(method.equals("PUT")){
74
+            HttpRequestWithBody put = Unirest.put(url);
75
+            try{
76
+                put.body(jpayload).asJson();
77
+            }catch (UnirestException e){
78
+                e.printStackTrace();
79
+            }
80
+        }
81
+
21 82
         return "nada";
22 83
     }
23 84
 }

+ 5
- 5
README.md Näytä tiedosto

@@ -17,13 +17,13 @@ all the ids registered by sending a GET request to the same URL. Once you've rec
17 17
 messages to the world or to a specific Github_id.
18 18
 
19 19
 You 
20
-can send a message to the global timeline by POSTing a Message JSON object to the URL below.
20
+can send a message to the global timeline by POSTing a Entity.Message JSON object to the URL below.
21 21
 If you leave the "to id" field empty, the message is "to the world". If you fill out the the JSON template with
22 22
 a valid github_id in the "to id" field of the JSON payload, then that message is addressed to that friend. Yes, all 
23 23
 messages can be seen by users of the system. There are JSON templates below for both Ids and Messages.
24 24
 
25
-When you send a new Message or Id JSON object to the server, it records it, and fills in one or two fields. 
26
-A Message gets an assigned sequence number and a timestamp of when it was received by the server. An ID
25
+When you send a new Entity.Message or Entity.Id JSON object to the server, it records it, and fills in one or two fields. 
26
+A Entity.Message gets an assigned sequence number and a timestamp of when it was received by the server. An ID
27 27
 object gets a "user id" field assigned to it.
28 28
 Any sequence number, timestamp or userid you put into a JSON template
29 29
 gets overwritten by the server when you POST it. 
@@ -54,7 +54,7 @@ It's possible you may also need to understand some of what the Jackson package d
54 54
 
55 55
 * jackson json https://github.com/FasterXML/jackson
56 56
 
57
-And you may wish to create a couple classes `public class Message` and `public class Id` to make handling
57
+And you may wish to create a couple classes `public class Entity.Message` and `public class Entity.Id` to make handling
58 58
 the abstractions implied by the API easier.
59 59
 
60 60
 Jackson can help you parse the json into objects,and objects back into JSON strings. Be sure to research how you can
@@ -105,7 +105,7 @@ and send it as the body of a POST request to  `http://zipcode.rocks:8085/ids/`
105 105
  
106 106
 ## Messages
107 107
 
108
-#### Message comands in shell
108
+#### Entity.Message comands in shell
109 109
 
110 110
 in the shell, 
111 111
 * `messages` should return the last 20 messages, nicely formatted.

+ 9
- 0
YouAreEll.iml Näytä tiedosto

@@ -8,6 +8,15 @@
8 8
     </content>
9 9
     <orderEntry type="inheritedJdk" />
10 10
     <orderEntry type="sourceFolder" forTests="false" />
11
+    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
12
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.2" level="project" />
13
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.4" 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.9" level="project" />
16
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.1.1" level="project" />
17
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.4.4" level="project" />
18
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.5.2" level="project" />
19
+    <orderEntry type="library" name="Maven: org.json:json:20160212" level="project" />
11 20
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
12 21
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
13 22
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />

+ 11
- 1
pom.xml Näytä tiedosto

@@ -13,10 +13,20 @@
13 13
     </modules>
14 14
 
15 15
     <dependencies>
16
+
17
+        <dependency>
18
+            <groupId>com.mashape.unirest</groupId>
19
+            <artifactId>unirest-java</artifactId>
20
+            <version>1.4.9</version>
21
+        </dependency>
22
+
23
+        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
16 24
         <dependency>
17 25
             <groupId>com.fasterxml.jackson.core</groupId>
18 26
             <artifactId>jackson-databind</artifactId>
19 27
             <version>2.8.6</version>
20 28
         </dependency>
29
+
21 30
     </dependencies>
22
-</project>
31
+</project>
32
+