Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working #3

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
12 changes: 7 additions & 5 deletions Client/Client.iml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?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: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.json:json:20190722" 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>
13 changes: 13 additions & 0 deletions Client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>Client</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>


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

import java.util.ArrayList;
import java.util.stream.Collectors;

import models.Id;
import utils.JsonUtils;

public class IdController {
Id myId;

public void setMyId(Id myId) {
this.myId = myId;
}

public Id getMyId() {
return this.myId;
}

public ArrayList<Id> getIds() {
return null;
ArrayList<Id> ids = new ArrayList<Id>();
String json = TransactionController.MakeURLCall("/ids", "GET", "");
for (String id : JsonUtils.jsonSplitter(json)) {
ids.add(JsonUtils.stringToId(id));
}
return ids;
}

public Id postId(Id id) {
return null;
String[] labels = {"name", "github"};
String[] values = {id.getName(), id.getGithubId()};
String json = JsonUtils.jsonBuilder(labels, values);

String resp = TransactionController.MakeURLCall("/ids", "POST", json);
return JsonUtils.stringToId(resp);
}

public Id putId(Id id) {
return null;
String[] labels = {"userid", "name", "github"};
String[] values = {id.getUserId(), id.getName(), id.getGithubId()};
String json = JsonUtils.jsonBuilder(labels, values);

String resp = TransactionController.MakeURLCall("/ids", "PUT", json);
return JsonUtils.stringToId(resp);
}

public Id getIdByGit(String githubId) {
return getIds().stream().filter(id -> id.getGithubId().equals(githubId)).collect(Collectors.toList()).get(0);
}

}
40 changes: 32 additions & 8 deletions Client/src/main/java/controllers/MessageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,54 @@

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

import models.Id;
import models.Message;
import utils.JsonUtils;

public class MessageController {

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

public ArrayList<Message> getMessages() {
return null;
ArrayList<Message> messages = new ArrayList<Message>();
String json = TransactionController.MakeURLCall("/messages", "GET", "");
for (String message : JsonUtils.jsonSplitter(json)) {
messages.add(JsonUtils.stringToMessage(message));
}
return messages;
}
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.getGithubId()))
.collect(Collectors.<Message>toList());
}
public Message getMessageForSequence(String seq) {
return null;
return getMessages().stream()
.filter(m -> m.getSequence().equals(seq))
.collect(Collectors.<Message>toList()).get(0);
}
public ArrayList<Message> getMessagesFromFriend(Id myId, Id friendId) {
return null;
return (ArrayList<Message>) getMessages().stream()
.filter(m -> friendCondition(m, myId, friendId))
.collect(Collectors.<Message>toList());
}

public Message postMessage(String fromid, String toid, String message) {
String[] labels = {"sequence", "timestamp", "fromid", "toid", "message"};
String[] values = {"-", null, fromid, toid, message};
String json = JsonUtils.jsonBuilder(labels, values);

String mainurl = String.format("/ids/%s/messages", fromid);

String resp = TransactionController.MakeURLCall(mainurl,"POST", json);
return JsonUtils.stringToMessage(resp);
}

public Message postMessage(Id myId, Id toId, Message msg) {
return null;
public static Boolean friendCondition(Message m, Id myId, Id friendId) {
return ((m.getFromId().equals(myId.getGithubId())) && (m.getToId().equals(friendId.getGithubId()))); //||
//((m.getFromId().equals(friendId.getGithubId())) && (m.getToId().equals(myId.getGithubId())));
}

}
75 changes: 73 additions & 2 deletions Client/src/main/java/controllers/TransactionController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
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();
}
catch (Exception e) {
e.printStackTrace();
}
return response;
}

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();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public static String readServerResponse(HttpURLConnection conn) {
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

String output;
while ((output = br.readLine()) != null) {
result.append(output);
}
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}

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;

public TransactionController() {}
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
51 changes: 49 additions & 2 deletions Client/src/main/java/models/Id.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
package models;

/*
import com.fasterxml.jackson.databind.ObjectMapper;
import utils.JsonUtils;

/*
* POJO for an Id object
*/
public class Id {
String name;
String githubid;
String userid;

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

public Id (Id id) {
this(id.getName(), id.getGithubId(), id.getUserId());
}

public Id (String json) {
this(JsonUtils.stringToId(json));
}

public String getName() {
return this.name;
}

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

public String getGithubId() {
return this.githubid;
}

public void setGithubId(String githubId) {
this.githubid = githubId;
}

public String getUserId() {
return this.userid;
}

public void setUserId(String userId) {
this.userid = userId;
}

}
69 changes: 67 additions & 2 deletions Client/src/main/java/models/Message.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,75 @@
package models;

/*
import utils.JsonUtils;

/*
* POJO for an Message object
*/
public class Message {
String message;
String fromId;
String toId;
String sequence;
String timestamp;

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

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 Message (String json) {
this(JsonUtils.stringToMessage(json));
}


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

public String getSequence() {
return sequence;
}

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

public Message (String message, String fromId, String toId) {}
public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
Loading