Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Client/Client.iml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.10" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.12" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.10.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.10.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.10.1" level="project" />
</component>
</module>
12 changes: 12 additions & 0 deletions Client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@

<artifactId>Client</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>

</dependencies>
</project>
43 changes: 39 additions & 4 deletions Client/src/main/java/controllers/IdController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import models.Id;
import org.codehaus.jackson.map.ObjectMapper;

public class IdController {
Id myId;

public ArrayList<Id> getIds() {
return null;
ArrayList<Id> idsList = new ArrayList<Id>();
String json = TransactionController.MakeURLCall("/ids", "GET", "");
try {
ObjectMapper mapper = new ObjectMapper();
Id[] ids = mapper.readValue(json, Id[].class);
//parameter is json which takes in all url calls.
for(int i = 0; i < ids.length; i++){
idsList.add(ids[i]);
}
return idsList;
}catch (IOException i){
i.printStackTrace();
return null;
}
}

public Id postId(Id id) {
return null;
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(id);
String response = TransactionController.MakeURLCall("/ids", "POST", json);
return mapper.readValue(response, Id.class);
}catch (IOException i){
i.printStackTrace();
return null;
}
}

public Id putId(Id id) {
return null;
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(id);
String response = TransactionController.MakeURLCall("/ids", "PUT", json);
return mapper.readValue(response, Id.class);
}catch (IOException i){
i.printStackTrace();
return null;
}
}
public Id getIdByGit(String githubid){
return getIds().stream().filter(id -> id.getGithub().equals(githubid)).collect(Collectors.toList()).get(0);
}

}
51 changes: 40 additions & 11 deletions Client/src/main/java/controllers/MessageController.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
package controllers;

import java.util.ArrayList;
import java.util.HashSet;

import models.Id;
import models.Message;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.stream.Collectors;

public class MessageController {

private HashSet<Message> messagesSeen;
// why a HashSet??

public ArrayList<Message> getMessages() {
return null;
ArrayList<Message> messagesList = new ArrayList<Message>();
String json = TransactionController.MakeURLCall("/messages", "GET", "");
try {
ObjectMapper mapper = new ObjectMapper();
Message[] messages = mapper.readValue(json, Message[].class);
for (int i = 0; i < messages.length; i++) {
messagesList.add(messages[i]);
}
return messagesList;
} catch (IOException i) {
i.printStackTrace();
return null;
}
}
public ArrayList<Message> getMessagesForId(Id Id) {
return null;

public ArrayList<Message> getMessagesForId(Id id) {
return (ArrayList<Message>) getMessages().stream().filter(m -> m.getfromid().equals(id.getGithub())).collect(Collectors.toList());
}

public Message getMessageForSequence(String seq) {
return null;
return getMessages().stream().filter(m -> m.getSequence().equals(seq)).collect(Collectors.toList()).get(0);
}
public ArrayList<Message> getMessagesFromFriend(Id myId, Id friendId) {
return null;
return (ArrayList<Message>) getMessages().stream()
.filter(m ->
(m.getfromid().equals(myId.getGithub())) && m.gettoid().equals(friendId.getGithub()))
.collect(Collectors.toList());
}

public Message postMessage(Id myId, Id toId, Message msg) {
return null;
public Message postMessage(String myId, String toId, String msg) {
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(new Message(msg, myId, toId));
String mainUrl = String.format("/ids/%s/messages", myId);
String response = TransactionController.MakeURLCall(mainUrl, "POST", json);
return mapper.readValue(response, Message.class);
}catch (IOException i){
i.printStackTrace();
return null;
}
}

}
71 changes: 70 additions & 1 deletion Client/src/main/java/controllers/TransactionController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
package controllers;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class TransactionController {
private String rootURL = "http://zipcode.rocks:8085";
private static final String rootURL = "http://zipcode.rocks:8085";

public TransactionController() {}

public static String MakeURLCall(String mainurl, String method, String jpayload){
String response = "";
try{
HttpURLConnection conn = initConnection(mainurl, method, jpayload);
response = readServerResponse(conn);
conn.disconnect();
//closes connection to server
} catch (Exception e) {
e.printStackTrace();
}
return response;
}

public static HttpURLConnection initConnection(String mainurl, String method, String jpayload){
try{
URL url = new URL(rootURL + mainurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);
conn.setRequestMethod(method);
if(method.equals("POST") || method.equals("PUT")){
conn = writeJsonToConn(conn, jpayload);
}

return conn;

} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static String readServerResponse(HttpURLConnection conn){
//takes server output and puts it into a string
StringBuilder result = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//conn.getInputStream is the input but InputStreamReader reads that input
String output;
while ((output = br.readLine()) != null) {
result.append(output);
}
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}

public static HttpURLConnection writeJsonToConn(HttpURLConnection conn, String jpayload){
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
try{
OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream());
os.write(jpayload);
os.flush();;
os.close();
//conn is an open connection to the server running at the time in question
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
47 changes: 45 additions & 2 deletions Client/src/main/java/models/Id.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,50 @@
* POJO for an Id object
*/
public class Id {

public Id (String name, String githubId) {}
String name;
String github;
String userid;

private Id(){}
//looks like this isnt used but required for object mapper

public Id (String name, String githubId) {
this.name = name;
this.github = githubId;
this.userid = null;
}

public Id (String name, String githubId, String userid) {
this.name = name;
this.github = githubId;
this.userid = userid;
}

public Id (Id id){
this(id.getName(), id.getGithub(), id.getUserid());
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGithub() {
return github;
}

public void setGithub(String github) {
this.github = github;
}

public String getUserid() {
return userid;
}

public void setUserid(String userid) {
this.userid = userid;
}
}
67 changes: 66 additions & 1 deletion Client/src/main/java/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,72 @@
* POJO for an Message object
*/
public class Message {
private String message;
private String fromid;
private String toid;
private String sequence;
private String timestamp;

public Message (String message, String fromId, String toId) {}
public Message(String message, String fromid, String toid, String sequence, String timestamp){
this.message = message;
this.fromid = fromid;
this.toid = toid;
this.sequence = sequence;
this.timestamp = timestamp;
}

private Message(){}
//looks like this isnt used but required for object mapper

public Message(String message, String fromid, String toid){
this(message, fromid, toid, "-", null);
}

public Message(Message message){
this(message.getMessage(), message.getfromid(), message.gettoid(), message.getSequence(), message.getTimestamp());
}

public String getSequence() {
return sequence;
}

public void setSequence(String sequence) {
this.sequence = sequence;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
// public Message (String message, String fromId, String toId) {
//
// }

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getfromid() {
return fromid;
}

public void setfromid(String fromid) {
this.fromid = fromid;
}

public String gettoid() {
return toid;
}

public void settoid(String toid) {
this.toid = toid;
}

}
Loading