Kr Younger il y a 6 ans
Parent
révision
763ce14dbe

+ 3
- 0
.gitignore Voir le fichier

@@ -1,6 +1,9 @@
1 1
 # Compiled class file
2 2
 *.class
3 3
 
4
+# intellij
5
+.idea/*
6
+
4 7
 # Log file
5 8
 *.log
6 9
 

+ 17
- 0
.idea/compiler.xml Voir le fichier

@@ -0,0 +1,17 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="CompilerConfiguration">
4
+    <annotationProcessing>
5
+      <profile name="Maven default annotation processors profile" enabled="true">
6
+        <sourceOutputDir name="target/generated-sources/annotations" />
7
+        <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
8
+        <outputRelativeToContentRoot value="true" />
9
+        <module name="Client" />
10
+      </profile>
11
+    </annotationProcessing>
12
+    <bytecodeTargetLevel>
13
+      <module name="Client" target="1.5" />
14
+      <module name="YouAreEll" target="1.5" />
15
+    </bytecodeTargetLevel>
16
+  </component>
17
+</project>

+ 13
- 0
.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_8_0.xml Voir le fichier

@@ -0,0 +1,13 @@
1
+<component name="libraryTable">
2
+  <library name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.8.0/jackson-annotations-2.8.0.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.8.0/jackson-annotations-2.8.0-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.8.0/jackson-annotations-2.8.0-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 13
- 0
.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_8_6.xml Voir le fichier

@@ -0,0 +1,13 @@
1
+<component name="libraryTable">
2
+  <library name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.8.6/jackson-core-2.8.6.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.8.6/jackson-core-2.8.6-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.8.6/jackson-core-2.8.6-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 13
- 0
.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_8_6.xml Voir le fichier

@@ -0,0 +1,13 @@
1
+<component name="libraryTable">
2
+  <library name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 13
- 0
.idea/misc.xml Voir le fichier

@@ -0,0 +1,13 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="MavenProjectsManager">
4
+    <option name="originalFiles">
5
+      <list>
6
+        <option value="$PROJECT_DIR$/pom.xml" />
7
+      </list>
8
+    </option>
9
+  </component>
10
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_9" project-jdk-name="9.0" project-jdk-type="JavaSDK">
11
+    <output url="file://$PROJECT_DIR$/classes" />
12
+  </component>
13
+</project>

+ 9
- 0
.idea/modules.xml Voir le fichier

@@ -0,0 +1,9 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectModuleManager">
4
+    <modules>
5
+      <module fileurl="file://$PROJECT_DIR$/Client/Client.iml" filepath="$PROJECT_DIR$/Client/Client.iml" />
6
+      <module fileurl="file://$PROJECT_DIR$/YouAreEll.iml" filepath="$PROJECT_DIR$/YouAreEll.iml" />
7
+    </modules>
8
+  </component>
9
+</project>

+ 6
- 0
.idea/vcs.xml Voir le fichier

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+  </component>
6
+</project>

+ 15
- 0
Client/pom.xml Voir le fichier

@@ -0,0 +1,15 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+         xmlns="http://maven.apache.org/POM/4.0.0"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <parent>
6
+        <artifactId>YouAreEll</artifactId>
7
+        <groupId>com.zipcoder.ZipChat</groupId>
8
+        <version>1.0-SNAPSHOT</version>
9
+    </parent>
10
+    <modelVersion>4.0.0</modelVersion>
11
+
12
+    <artifactId>Client</artifactId>
13
+
14
+
15
+</project>

+ 119
- 0
Client/src/main/java/SimpleShell.java Voir le fichier

@@ -0,0 +1,119 @@
1
+import java.io.BufferedReader;
2
+import java.io.IOException;
3
+import java.io.InputStream;
4
+import java.io.InputStreamReader;
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
8
+public class SimpleShell {
9
+
10
+    public static void main(String[] args) throws java.io.IOException {
11
+
12
+        YouAreEll webber = new YouAreEll();
13
+        String commandLine;
14
+        BufferedReader console = new BufferedReader
15
+                (new InputStreamReader(System.in));
16
+
17
+        ProcessBuilder pb = new ProcessBuilder();
18
+        List<String> history = new ArrayList<String>();
19
+        int index = 0;
20
+        //we break out with <ctrl c>
21
+        while (true) {
22
+            //read what the user enters
23
+            System.out.println("cmd? ");
24
+            commandLine = console.readLine();
25
+
26
+            //input parsed into array of strings(command and arguments)
27
+            String[] commands = commandLine.split(" ");
28
+            List<String> list = new ArrayList<String>();
29
+
30
+            //if the user entered a return, just loop again
31
+            if (commandLine.equals(""))
32
+                continue;
33
+            if (commandLine.equals("exit")) {
34
+                System.out.println("bye!");
35
+                break;
36
+            }
37
+
38
+            //loop through to see if parsing worked
39
+            for (int i = 0; i < commands.length; i++) {
40
+                //System.out.println(commands[i]); //***check to see if parsing/split worked***
41
+                list.add(commands[i]);
42
+
43
+            }
44
+            System.out.print(list); //***check to see if list was added correctly***
45
+            history.addAll(list);
46
+            try {
47
+                //display history of shell with index
48
+                if (list.get(list.size() - 1).equals("history")) {
49
+                    for (String s : history)
50
+                        System.out.println((index++) + " " + s);
51
+                    continue;
52
+                }
53
+
54
+                // Specific Commands.
55
+
56
+                // ids
57
+                if (list.contains("ids")) {
58
+                    String results = webber.get_ids();
59
+                    System.out.println(results);
60
+                    continue;
61
+                }
62
+
63
+                // messages
64
+                if (list.contains("messages")) {
65
+                    String results = webber.get_messages();
66
+                    System.out.println(results);
67
+                    continue;
68
+                }
69
+                // you need to add a bunch more.
70
+
71
+                //!! command returns the last command in history
72
+                if (list.get(list.size() - 1).equals("!!")) {
73
+                    pb.command(history.get(history.size() - 2));
74
+
75
+                }//!<integer value i> command
76
+                else if (list.get(list.size() - 1).charAt(0) == '!') {
77
+                    int b = Character.getNumericValue(list.get(list.size() - 1).charAt(1));
78
+                    if (b <= history.size())//check if integer entered isn't bigger than history size
79
+                        pb.command(history.get(b));
80
+                } else {
81
+                    pb.command(list);
82
+                }
83
+
84
+                // wait, wait, what curiousness is this?
85
+                Process process = pb.start();
86
+
87
+                //obtain the input stream
88
+                InputStream is = process.getInputStream();
89
+                InputStreamReader isr = new InputStreamReader(is);
90
+                BufferedReader br = new BufferedReader(isr);
91
+
92
+                //read output of the process
93
+                String line;
94
+                while ((line = br.readLine()) != null)
95
+                    System.out.println(line);
96
+                br.close();
97
+
98
+
99
+            }
100
+
101
+            //catch ioexception, output appropriate message, resume waiting for input
102
+            catch (IOException e) {
103
+                System.out.println("Input Error, Please try again!");
104
+            }
105
+            // So what, do you suppose, is the meaning of this comment?
106
+            /** The steps are:
107
+             * 1. parse the input to obtain the command and any parameters
108
+             * 2. create a ProcessBuilder object
109
+             * 3. start the process
110
+             * 4. obtain the output stream
111
+             * 5. output the contents returned by the command
112
+             */
113
+
114
+        }
115
+
116
+
117
+    }
118
+
119
+}

+ 23
- 0
Client/src/main/java/YouAreEll.java Voir le fichier

@@ -0,0 +1,23 @@
1
+public class YouAreEll {
2
+
3
+    YouAreEll() {
4
+    }
5
+
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
+    }
11
+
12
+    public String get_ids() {
13
+        return MakeURLCall("/ids", "GET", "");
14
+    }
15
+
16
+    public String get_messages() {
17
+        return MakeURLCall("/messages", "GET", "");
18
+    }
19
+
20
+    public String MakeURLCall(String mainurl, String method, String jpayload) {
21
+        return "nada";
22
+    }
23
+}

+ 162
- 2
README.md Voir le fichier

@@ -1,2 +1,162 @@
1
-# YouAreEll
2
-Java URL client shell
1
+# Under-A-Rock
2
+
3
+## Client HTTP/REST API for UnderARock
4
+
5
+
6
+### The Point
7
+
8
+You are to write a command interpreter using the provided SimpleShell class. You're going to create a way for 
9
+commands to be typed into your shell, read the typed commands and arguments, send them off to the Under-A-Rock 
10
+server using a REST API over the HTTP protocol, read the JSON data returned from the URL call, and print it out 
11
+nicely formatted for your user. 
12
+
13
+Under-A-Rock acts a little (very little) like a twitter server or chat server. 
14
+
15
+You register your name and githubid by creating an ID JSON payload (see below) and POSTing it to the server. You can GET 
16
+all the ids registered by sending a GET request to the same URL. Once you've received all the ids, you can send
17
+messages to the world or to a specific Github_id.
18
+
19
+You 
20
+can send a message to the global timeline by POSTing a Message JSON object to the URL below.
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
+a valid github_id in the "to id" field of the JSON payload, then that message is addressed to that friend. Yes, all 
23
+messages can be seen by users of the system. There are JSON templates below for both Ids and Messages.
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
27
+object gets a "user id" field assigned to it.
28
+Any sequence number, timestamp or userid you put into a JSON template
29
+gets overwritten by the server when you POST it. 
30
+
31
+You're going to create a series of REST API handlers that will each perform a 
32
+specific command. Each one of the command methods will then call a even lower-level method that makes a certain kind
33
+of HTTP request (GET, POST, PUT) to specific filled-in URL.
34
+
35
+The Under-A-Rock Server can be reached at `http://zipcode.rocks:8085` Everyone uses the same server. 
36
+
37
+There are two segments to the API and two kinds of commands in the shell, the ID segment and the Messages segment.
38
+
39
+
40
+You can explore several ways of doing the HTTP URL calls to the server, using the one of these:
41
+* Core Java version 
42
+URL https://docs.oracle.com/javase/8/docs/api/java/net/URL.html and 
43
+HttpURLConnection https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html, 
44
+* Apache HTTP Client Library http://hc.apache.org/httpcomponents-client-ga/index.html
45
+* Unirest for Java http://unirest.io/java.html
46
+* Square's OKHttp https://github.com/square/okhttp
47
+
48
+Be prepared to defend your choice if which HTTP client library you chose, with reasons why you chose it.
49
+You should also create some unit tests for your REST API handlers.
50
+
51
+It's possible you may also need to understand some of what the Jackson package does for you. 
52
+* jackson json 
53
+
54
+## IDs
55
+
56
+#### ID commands in shell
57
+In the shell, 
58
+`ids` should return a formatted list of the IDs available to you.
59
+
60
+`ids your_name your_github_id` command should post your Name and your GithubId to the server.
61
+If you do this twice with two different Names, but the name GithubId, the name on the server gets changed.
62
+
63
+the IDs API is:
64
+
65
+#### URL: /ids/
66
+* `GET` : Get all github ids registered
67
+* `POST` : add your github id / name to be registered
68
+* `PUT` : change the name linked to your github id
69
+
70
+json payload for /ids/ - this is a sample
71
+```json
72
+{
73
+    "userid": "-", // gets filled w id
74
+    "name": "Kris",
75
+    "githubid": "xt0fer"
76
+}
77
+```
78
+ 
79
+ #### Example: 
80
+ 
81
+ if I type 
82
+ ```aidl
83
+cmd?
84
+ ids Kris xt0fer
85
+```
86
+  into the shell, your command processor creates a JSON object which looks like:
87
+ ```json
88
+ {
89
+     "userid": "-", // gets filled w id
90
+     "name": "Kris",
91
+     "githubid": "xt0fer"
92
+ }
93
+ ```
94
+and send it as the body of a POST request to  `http://zipcode.rocks:8085/ids/`
95
+
96
+ 
97
+## Messages
98
+
99
+#### Message comands in shell
100
+
101
+in the shell, 
102
+* `messages` should return the last 20 messages, nicely formatted.
103
+* `messages your_github_id` should return the last 20 messages sent to you.
104
+* `send your_github_id 'Hello World' ` should post a new message in the timeline
105
+* `send your_github_id 'my string message' to some_friend_githubid` should post a message to your friend from you on the timeline.
106
+
107
+the Messages API is:
108
+
109
+#### URL: /messages/
110
+* `GET` : Get last 20 msgs - returns an JSON array of message objects
111
+
112
+#### URL: /ids/:mygithubid/messages/
113
+* `GET` : Get last 20 msgs for myid  - returns an JSON array of message objects
114
+* `POST` : Create a new message in timeline - need to POST a new message object, and will get back one with a message sequence number and timestamp of the server inserted.
115
+
116
+#### URL: /ids/:mygithubid/messages/:sequence
117
+* `GET` : Get msg with a sequence  - returns a JSON message object for a sequence number
118
+
119
+#### URL: /ids/:mygithubid/from/:friendgithubid
120
+* `GET` : Get last 20 msgs for myid from friendid
121
+
122
+json payload for /messages/ these are samples, one to a specific friend, one to the timeline.
123
+```json
124
+[
125
+  {
126
+    "sequence": "-",
127
+    "timestamp": "_",
128
+    "fromid": "xt0fer",
129
+    "toid": "kristofer",
130
+    "message": "Hello, Kristofer!"
131
+  },
132
+
133
+  {
134
+    "sequence": "-",
135
+    "timestamp": "_",
136
+    "fromid": "xt0fer",
137
+    "toid": "",
138
+    "message": "Hello, World!"
139
+  }
140
+]
141
+```
142
+
143
+#### Example: 
144
+ 
145
+ if I type 
146
+ ```aidl
147
+cmd?
148
+send xt0fer 'Hello old buddy!' to torvalds
149
+```
150
+  into the shell, your command processor creates a JSON object which looks like:
151
+ ```json
152
+  {
153
+     "sequence": "-",
154
+     "timestamp": "_",
155
+     "fromid": "xt0fer",
156
+     "toid": "torvalds",
157
+     "message": "Hello old buddy!"
158
+   }
159
+ ```
160
+and send it as the body of a POST request to  `http://zipcode.rocks:8085/ids/xt0fer/messages/`
161
+
162
+

+ 15
- 0
YouAreEll.iml Voir le fichier

@@ -0,0 +1,15 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4
+    <output url="file://$MODULE_DIR$/target/classes" />
5
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
+    <content url="file://$MODULE_DIR$">
7
+      <excludeFolder url="file://$MODULE_DIR$/target" />
8
+    </content>
9
+    <orderEntry type="inheritedJdk" />
10
+    <orderEntry type="sourceFolder" forTests="false" />
11
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
12
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
13
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
14
+  </component>
15
+</module>

+ 22
- 0
pom.xml Voir le fichier

@@ -0,0 +1,22 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+         xmlns="http://maven.apache.org/POM/4.0.0"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>com.zipcoder.ZipChat</groupId>
8
+    <artifactId>YouAreEll</artifactId>
9
+    <packaging>pom</packaging>
10
+    <version>1.0-SNAPSHOT</version>
11
+    <modules>
12
+        <module>Client</module>
13
+    </modules>
14
+
15
+    <dependencies>
16
+        <dependency>
17
+            <groupId>com.fasterxml.jackson.core</groupId>
18
+            <artifactId>jackson-databind</artifactId>
19
+            <version>2.8.6</version>
20
+        </dependency>
21
+    </dependencies>
22
+</project>