Skip to content

Instantly share code, notes, and snippets.

@mtodd
Forked from anonymous/App.java
Created July 31, 2008 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtodd/3519 to your computer and use it in GitHub Desktop.
Save mtodd/3519 to your computer and use it in GitHub Desktop.
undefined
import java.util.HashMap;
public class App {
private static int installWindowWidth = 500, installWindowHeight = 350;
private static int adminWindowWidth = 400, adminWindowHeight = 275;
private static String server = "168.28.247.195", port = "3306", database = "software";
private static String user = "root", password = "";
private static String axeServer = "https://168.28.245.221:6467";
private static String webServer = "http://168.28.247.195:3000";
private static HashMap authHeaders = new HashMap();
private static String serving = "";
private static String authToken = "";
public static int getInstallWidth(){
return installWindowWidth;
}
public static int getInstallHeight(){
return installWindowHeight;
}
public static int getAdminWidth(){
return adminWindowWidth;
}
public static int getAdminHeight(){
return adminWindowHeight;
}
public static String getWebServer(){
return webServer;
}
public static String getDBServerAddress(){
return server;
}
public static String getDBPort(){
return port;
}
public static String getDBName(){
return database;
}
public static String getDBUser(){
return user;
}
public static String getDBPassword(){
return password;
}
public static String getAxeServer(){
return axeServer;
}
public static String getAuthToken() {
return authToken;
}
public static void setAuthHeader() {
authHeaders.put("Authorization", "Basic YXV0aGVudGljYXRvcjpPY2ggZGVuIHNvbSBpbnRlIGhlbGFuIHRhck8gaGFuIGVqIGhlbGxlciBoYWx2YW4gZmFySw==");
}
public static void setAuthToken(String token) {
authToken = token;
}
public static HashMap addTokenAuth(HashMap body) {
body.put("token", authToken);
return body;
}
public static HashMap authenticationHeaders() {
return authHeaders;
}
public static String whoAreWeServing() {
return serving;
}
public static void setWhoAreWeServing(String clientOrUser) {
serving = clientOrUser;
}
}
import java.util.HashMap;
public class Authentication {
public static boolean Login(String user, String pass) {
boolean isAuth = false;
if(!pass.equals("")) {
Machete halcyonClient = new Machete(App.getAxeServer());
HashMap hash = new HashMap();
hash.put("password", pass);
try {
halcyonClient.POST("/user/auth/" + user, hash,
App.authenticationHeaders());
isAuth = true;
}
catch(Exception e) {
// If POST Fails, auth fails. No exception handling required.
}
}
return isAuth;
}
public static boolean isAdmin() {
Machete halcyonClient = new Machete(App.getAxeServer());
HashMap hash = new HashMap();
try {
hash = halcyonClient.GET("/user/" + User.getUserName() +
"/massen/permit/role", App.authenticationHeaders());
String response = (String)hash.get("body");
if(response.equals("admin")) { return true; }
else { return false; }
}
catch(Exception e) {
// No exception handling required. If the GET fails then auth for admin fails.
return false;
}
}
}
import java.sql.*;
import java.text.*;
import java.util.HashMap;
public class Gopher {
private static String server = App.getDBServerAddress();
private static String port = App.getDBPort();
private static String database = App.getDBName();
private static String user = App.getDBUser();
private static String password = App.getDBPassword();
private static Connection conn;
private static Statement Connect() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, user, password);
return conn.createStatement();
}
public static String GetSemesterCode() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
String dateStr = dateFormat.format(date);
char[] chardate = dateStr.toCharArray();
String syear = dateStr.substring(0, 4);
String smonth = String.valueOf(chardate[5]) + String.valueOf(chardate[6]);
int imonth = Integer.parseInt(smonth);
if (imonth < 5)
return syear + "02";
if (imonth >= 5 && imonth < 8)
return syear + "05";
if (imonth > 12 && imonth <= 8)
return syear + "08";
return null;
}
//Finds current machine in database by its mac address, returns the computer ID or null if it does not exist
public static String FindComputerByMac(String lan) throws Exception {
try {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from computers where lan='" + lan + "';");
results.next();
return results.getString("id");
} catch (SQLException e){
return "none";
}
}
//Adds new machine if it does not exist in the database. Only does some real basic shit here--this aint no swiss knife
public static void AddNewComputer(String client_id, String lan) throws Exception {
// Machete client = new Machete(App.getWebServer());
Machete client = new Machete("http://localhost:3000");
HashMap body = new HashMap();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
String dateStr = dateFormat.format(date);
// prepare client params
body.put("client_id", client_id);
body.put("lan", lan);
body.put("stolen", "N");
body.put("status_reported", dateStr);
// submit
try{
HashMap response = client.POST("/computers/create_from_client.json", App.addTokenAuth(body), new HashMap());
} catch(Exception e){
// nothing
}
}
//Adds a new operating system into the database
public static void AddNewOS(String name) throws Exception {
// Machete client = new Machete(App.getWebServer());
Machete client = new Machete("http://localhost:3000");
HashMap body = new HashMap();
// prepare client params
body.put("name", name);
// submit
try{
HashMap response = client.POST("/operating_system/create_from_client.json", App.addTokenAuth(body), new HashMap());
} catch(Exception e){
// nothing
}
}
//Takes in mac address, returns true if machine is stolen
public static boolean CheckStolen(String lan) throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from computers where lan=\"" + lan + "\";");
while (results.next()) {
if (results.getString("stolen").equals("Y")) {
return true;
}
}
return false;
}
//Update computer operating system
public static void UpdateOS(String computer_id, String os_id) throws Exception {
// Machete client = new Machete(App.getWebServer());
Machete client = new Machete("http://localhost:3000");
HashMap body = new HashMap();
// prepare client params
body.put("os_id", os_id);
body.put("id", computer_id);
// submit
try{
HashMap response = client.POST("/computers/update_from_client.json", App.addTokenAuth(body), new HashMap());
} catch(Exception e){
// nothing
}
}
//Updates database with new installed software
public static void UpdateInstalledSoftware(String software_id, String computer_id, String user_id, String client_id, String ip, String semester_code) throws Exception {
// Machete client = new Machete(App.getWebServer());
Machete client = new Machete("http://localhost:3000");
HashMap body = new HashMap();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
String dateStr = dateFormat.format(date);
// prepare client params
body.put("software_id", software_id);
body.put("computer_id", computer_id);
body.put("user_id", user_id);
body.put("install_date", dateStr);
body.put("client_id", client_id);
body.put("ip_address", ip);
body.put("status_code", semester_code);
// submit
try{
HashMap response = client.POST("/installations/create_from_client.json", App.addTokenAuth(body), new HashMap());
} catch(Exception e){
// nothing
}
}
//Finds software ID by name
public static String FindSoftwareID(String name) throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from software_details where name='" + name + "';");
results.next();
return results.getString("id");
}
//Takes in user or client network id specified by query and returns ID
public static String FindClientsorUsersID(String name, String query) throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from " + query + " where network_id='" + name + "';");
results.next();
try { return results.getString("id"); }
catch (Exception fg) { return "none"; }
}
//Takes in a computer ID and updates both the LAN and WLAN addresses -- not used
public static void UpdateMacAddresses(String[] addresses, String computer_id) throws Exception {
// Machete client = new Machete(App.getWebServer());
Machete client = new Machete("http://localhost:3000");
HashMap body = new HashMap();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
String dateStr = dateFormat.format(date);
// specify which computer to update
body.put("id", computer_id);
body.put("lan", addresses[0]);
if(addresses.length() > 1) {
body.put("wlan", addresses[1]);
}
// submit
try{
HashMap response = client.POST("/computers/update_from_client.json", App.addTokenAuth(body), new HashMap());
} catch(Exception e){
// nothing
}
}
public static HashMap RetrieveSoftwareNames() throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select name from software_details");
HashMap names = new HashMap();
Integer counter = 0;
while(results.next()) {
names.put(counter, results.getString("name"));
counter++;
}
return names;
}
//Finds client in database by network_id, returns the client info or null if it does not exist
public static String FindClientByNetworkID() throws Exception {
try {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from clients where network_id='" + Client.getUserName() + "';");
results.next();
return results.getString("name");
}
catch (SQLException e) {
return null;
}
}
//Takes in OS name, returns ID
public static String FindOSID(String name) throws Exception {
try {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from opsys where name='" + name + "';");
results.next();
return results.getString("id");
}
catch (SQLException e) {
return "none";
}
}
//Redo this sometime... gets requirements and returns array
public static String[] FindSoftwareRequirements(String name) throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from software_details where name='" + name + "';");
String[] reqs = new String[4];
while(results.next()) {
reqs[0] = results.getString("req_cpu"); //cpu
reqs[1] = results.getString("req_hdd"); //hdd
reqs[2] = results.getString("req_memory"); //mem
reqs[3] = results.getString("req_windows"); //windows
}
return reqs;
}
//Takes in software name, checks if windows is required
public static Boolean RequiresWindows(String name) throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from software_details where name='" + name +"';");
results.next();
return results.getBoolean("req_windows");
}
//Checks if student has installed software, returns latest installation
public static String CheckLastSemesterInstalled(String client_id, String software_id) throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from installations where client_id=" + client_id + " and software_id=" + software_id + ";");
results.last();
try { return results.getString("semester_code"); }
catch (Exception a) { return "none"; }
}
//Take in software name, finds location
public static String FindSoftwareLocation(String name) throws Exception {
Statement stmt = Connect();
ResultSet results = stmt.executeQuery("select * from software_details where name='" + name + "';");
results.next();
return results.getString("location");
}
//Adds new client into the database
public static void AddNewClient(String network_id, String name) throws Exception {
// Statement stmt = Connect();
// stmt.executeUpdate("insert into users (network_id, name) values (" + network_id + ",'" + name + ");");
// Machete client = new Machete(App.getWebServer());
Machete client = new Machete("http://localhost:3000");
HashMap body = new HashMap();
// prepare client params
body.put("network_id", network_id);
body.put("name", name);
// submit
try{
HashMap response = client.POST("/client/create_from_client.json", App.addTokenAuth(body), new HashMap());
} catch(Exception e){
// nothing
}
}
//Manual disconnect database connection
public static void Disconnect() throws Exception {
try { if (!conn.isClosed()) conn.close(); } catch (Exception e) { }
}
}
import java.util.HashMap;
import org.json.JSONObject;
public class Software {
public static JSONObject Installed(String network_id) {
// new machete client
Machete verificationClient = new Machete("http://localhost:3000");
try{
// connect to webserver to reuse ruby code and controllers to find availble software
HashMap headers = verificationClient.GET("/client/software_installed_for/" +
network_id + ".json", new HashMap());
// Will sometimes return string, so catch the cast exception
JSONObject jObj = JSONObject.class.newInstance();
try {
jObj = (JSONObject)headers.get("body");
if(jObj.has("condition")) {
String value = (String)jObj.get("condition");
if(value.equals("not a student")) {
jObj = new JSONObject().put("not a student", headers.get("body"));
}
else if(value.equals("not eligible")) {
jObj = new JSONObject().put("not eligible", headers.get("body"));
}
}
} catch (Exception e) {
jObj = new JSONObject().put("not eligible", headers.get("body"));
}
return jObj;
} catch(Exception e){
return new JSONObject();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment