diff --git a/conf/GremlinServerConnection.json b/conf/GremlinServerConnection.json new file mode 100644 index 0000000..a3a9adf --- /dev/null +++ b/conf/GremlinServerConnection.json @@ -0,0 +1,6 @@ +{ + "host":"localhost", + "port":8182, + "username":"", + "password":"" +} \ No newline at end of file diff --git a/conf/SteamAPIKey.json b/conf/SteamAPIKey.json new file mode 100644 index 0000000..0284303 --- /dev/null +++ b/conf/SteamAPIKey.json @@ -0,0 +1,3 @@ +{ + "api":"put-your-key-here" +} diff --git a/pom.xml b/pom.xml index 5080960..f2d7d25 100644 --- a/pom.xml +++ b/pom.xml @@ -14,8 +14,8 @@ UTF-8 - 1.7 - 1.7 + 1.8 + 1.8 @@ -25,6 +25,34 @@ 4.11 test + + + + com.tinkerpop + gremlin-core + 3.0.0.M7 + + + + + org.apache.tinkerpop + gremlin-driver + 3.3.3 + + + + + org.apache.tinkerpop + tinkergraph-gremlin + 3.3.3 + + + + + org.json + json + 20180130 + diff --git a/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java new file mode 100644 index 0000000..653a174 --- /dev/null +++ b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java @@ -0,0 +1,37 @@ +package net.jrtechs.www.SteamAPI; + +import net.jrtechs.www.utils.ConfigLoader; + +/** + * Class which is used to pull information from the Steam api + * + * @author Jeffery Russell 5-26-18 + */ +public class APIConnection +{ + /** Base url to use for all queries to steam's api **/ + private final String baseURL = "http://api.steampowered.com"; + + /** Path to use when getting info on a player from api **/ + private final String playerInfoURL = "/ISteamUser/GetPlayerSummaries/v0002/"; + + private final String friendListURL = "/ISteamUser/GetFriendList/v0001/"; + + /** Path to conf file(from within the conf folder) **/ + private final String confPath = "SteamAPIKey.json"; + + /** API key for steam's api - loaded from json conf file **/ + private String apiKey; + + + /** + * Constructor for APIConnection which loads a config file + * and sets the api key to your Steam api key. + */ + public APIConnection() + { + ConfigLoader conf = new ConfigLoader(confPath); + + apiKey = "?key=" + conf.getValue("api"); + } +} diff --git a/src/main/java/net/jrtechs/www/graphDB/RemoteConnection.java b/src/main/java/net/jrtechs/www/graphDB/RemoteConnection.java new file mode 100644 index 0000000..c61dc40 --- /dev/null +++ b/src/main/java/net/jrtechs/www/graphDB/RemoteConnection.java @@ -0,0 +1,101 @@ +package net.jrtechs.www.graphDB; + +import org.apache.tinkerpop.gremlin.driver.Client; +import org.apache.tinkerpop.gremlin.driver.Cluster; +import org.apache.tinkerpop.gremlin.driver.ResultSet; + +import net.jrtechs.www.utils.ConfigLoader; + +/** + * Simple helper class which allows us to remotely connect to a + * remote graph + * + * @author Jeffery Russell 5-24-18 + */ +public class RemoteConnection +{ + + /** Stores/manages client connections **/ + private Cluster cluster; + + + /** + * Connection to the graph db + */ + private Client client; + + + /** + * Connects to a remote Graph database + * + * Check this link out to learn about Cluster.Builder() + * http://tinkerpop.apache.org/javadocs/3.3.3/core/org/apache/tinkerpop/ + * gremlin/driver/Cluster.Builder.html + * + */ + public RemoteConnection() + { + ConfigLoader conf = new ConfigLoader("GremlinServerConnection.json"); + + Cluster.Builder b = Cluster.build(); + b.addContactPoint(conf.getValue("host")); + b.port(conf.getInt("port")); + + b.credentials(conf.getValue("username"), conf.getValue("password")); + + this.cluster = b.create(); + + this.client = cluster.connect(); + } + + + /** + * Queries the graph and return the results which can be iterated over + * + * ex: + * ResultSet results = remote.queryGraph("g.V().values('name')"); + * + * results.stream().forEach(result -> + * { + * String s = result.getString(); + * System.out.println("name: " + s); + * }); + * + * @param q + * @return + */ + public ResultSet queryGraph(String q) + { + return this.client.submit(q); + } + + + /** + * Closes connection with remote database + */ + public void closeConnection() + { + this.cluster.close(); + } + + + /** + * testing method which will be removed soon + * @param args + */ + public static void main(String args[]) + { + RemoteConnection remote = new RemoteConnection(); + ResultSet results = remote.queryGraph("g.V().values('name')"); + + //results.stream().forEach(System.out::println); + + results.stream().forEach(result -> + { + String s = result.getString(); + System.out.println("name: " + s); + }); + + remote.closeConnection(); + } +} \ No newline at end of file diff --git a/src/main/java/net/jrtechs/www/utils/ConfigLoader.java b/src/main/java/net/jrtechs/www/utils/ConfigLoader.java new file mode 100644 index 0000000..b6444e4 --- /dev/null +++ b/src/main/java/net/jrtechs/www/utils/ConfigLoader.java @@ -0,0 +1,101 @@ +package net.jrtechs.www.utils; + +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; + + +/** + * {@link ConfigLoader} Is responsible for abstracting the process + * of loading a configuration file to make it easier to use for + * multiple purposes. + * All configuration files will be stored in JSON format which + * makes reading and distributing them easier. + * + * @author Jeffery Russell 5-22-18 + */ +public class ConfigLoader +{ + /** Json object which stores configuration contents **/ + JSONObject config; + + + /** + * Constructor which reads in a conf file and sets the + * json element of the class. + * + * @param fileName + */ + public ConfigLoader(String fileName) + { + fileName = "conf/" + fileName; + + String file = this.loadFile(fileName); + this.config = new JSONObject(file); + } + + + /** + * Loads JSON from a particular file + * + * @param file -- file name to open + * @return - String of file contents + */ + private String loadFile(String file) + { + String jsonString = ""; + + try + { + BufferedReader br = new BufferedReader( + new InputStreamReader(new FileInputStream(file))); + String line; + while((line = br.readLine()) != null) + { + if(line.length() > 0 && line.charAt(0) != '#') + { + jsonString += line; + } + } + } + catch(Exception e) + { + e.printStackTrace(); + } + return jsonString; + } + + + /** + * Returns a single property from a {@link ConfigLoader} + * based on its key. + * + * @param key + * @return + */ + public String getValue(String key) + { + if(this.config.has(key)) + return this.config.getString(key); + else + return null; + } + + + /** + * Returns the integer value associated with a key in + * the configuration file. + * + * @param key + * @return + */ + public int getInt(String key) + { + if(this.config.has(key)) + return this.config.getInt(key); + else + return 0; + } +} \ No newline at end of file diff --git a/src/main/java/net/jrtechs/www/utils/WebScraper.java b/src/main/java/net/jrtechs/www/utils/WebScraper.java new file mode 100644 index 0000000..1a8d90e --- /dev/null +++ b/src/main/java/net/jrtechs/www/utils/WebScraper.java @@ -0,0 +1,81 @@ +package net.jrtechs.www.utils; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * Class which retrieves contents of a website as html + * + * @author Jeffery Russell 5-25-18 + */ +public class WebScraper { + /** + * Grabs the contents of a website as a string + * + * @param link to open + * @return source code of website as a single string + */ + public static String getWebsite(String link) { + try { + URL url = new URL(link); + + BufferedReader br = new BufferedReader( + new InputStreamReader(url.openStream()) + ); + return WebScraper.getBufferedReaderData(br); + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + } + + + /** + * Gets contents of a post request sent to a web server + * + * @param link to open + * @param postData to send + * @return source code of website as a string + */ + public static String getPostResponse(String link, String postData) { + try { + URL url = new URL(link); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestMethod("POST"); + + OutputStream os = conn.getOutputStream(); + os.write(postData.getBytes()); + os.flush(); + + BufferedReader br = new BufferedReader( + new InputStreamReader(conn.getInputStream()) + ); + return WebScraper.getBufferedReaderData(br); + } catch (Exception ex) { + ex.printStackTrace(); + } + + return ""; + } + + + /** + * Helper method for getPostResponse() and getWebsite() + * Slams all contents of a buffered reader into a single string. + * + * @param br + * @return contents of buffered reader + * @throws IOException -- with br.readLine() + */ + private static String getBufferedReaderData(BufferedReader br) + throws IOException { + String html = ""; + String line; + while ((line = br.readLine()) != null) { + html += line; + } + return html; + } +} \ No newline at end of file