Browse Source

Set up repository with helper files which will be used in the project.

pull/8/head
jrtechs 6 years ago
parent
commit
11af5e4c6d
7 changed files with 359 additions and 2 deletions
  1. +6
    -0
      conf/GremlinServerConnection.json
  2. +3
    -0
      conf/SteamAPIKey.json
  3. +30
    -2
      pom.xml
  4. +37
    -0
      src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java
  5. +101
    -0
      src/main/java/net/jrtechs/www/graphDB/RemoteConnection.java
  6. +101
    -0
      src/main/java/net/jrtechs/www/utils/ConfigLoader.java
  7. +81
    -0
      src/main/java/net/jrtechs/www/utils/WebScraper.java

+ 6
- 0
conf/GremlinServerConnection.json View File

@ -0,0 +1,6 @@
{
"host":"localhost",
"port":8182,
"username":"",
"password":""
}

+ 3
- 0
conf/SteamAPIKey.json View File

@ -0,0 +1,3 @@
{
"api":"put-your-key-here"
}

+ 30
- 2
pom.xml View File

@ -14,8 +14,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
@ -25,6 +25,34 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.tinkerpop/gremlin-core -->
<dependency>
<groupId>com.tinkerpop</groupId>
<artifactId>gremlin-core</artifactId>
<version>3.0.0.M7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tinkerpop/gremlin-driver -->
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkergraph-gremlin</artifactId>
<version>3.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
<build>

+ 37
- 0
src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java View File

@ -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");
}
}

+ 101
- 0
src/main/java/net/jrtechs/www/graphDB/RemoteConnection.java View File

@ -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();
}
}

+ 101
- 0
src/main/java/net/jrtechs/www/utils/ConfigLoader.java View File

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

+ 81
- 0
src/main/java/net/jrtechs/www/utils/WebScraper.java View File

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

Loading…
Cancel
Save