diff options
author | Nathan Lasseter <nathan.je.lasseter@googlemail.com> | 2011-02-20 16:48:04 +0000 |
---|---|---|
committer | Nathan Lasseter <nathan.je.lasseter@googlemail.com> | 2011-02-20 16:48:04 +0000 |
commit | f89ef9ed36e8185f53c7d5f22f91935e2c4ccaa0 (patch) | |
tree | 88a3bced4fcef9ac3a1260f8761cf241176c59da | |
parent | 1873252f1ec11794092dcaedaed477afc0572da7 (diff) |
Uses new config. PLEASE DONT PUSH PASSWORDS IN THE CONFIG\!
-rw-r--r-- | res/conf.xml | 14 | ||||
-rw-r--r-- | src/uk/org/ury/config/Auth.java | 15 | ||||
-rw-r--r-- | src/uk/org/ury/config/ConfigReader.java | 94 | ||||
-rw-r--r-- | src/uk/org/ury/config/Database.java | 2 | ||||
-rw-r--r-- | src/uk/org/ury/database/DatabaseDriver.java | 62 | ||||
-rw-r--r-- | src/uk/org/ury/database/DatabaseLogin.java | 150 | ||||
-rw-r--r-- | src/uk/org/ury/database/credentials/.gitignore | 1 | ||||
-rw-r--r-- | src/uk/org/ury/library/viewer/LibraryViewer.java | 10 |
8 files changed, 120 insertions, 228 deletions
diff --git a/res/conf.xml b/res/conf.xml index 3665cb1..4795e94 100644 --- a/res/conf.xml +++ b/res/conf.xml @@ -1,11 +1,12 @@ <?xml version="1.0" encoding="UTF-8" ?> <!-- - At the moment the database and auth tags do nothing. - Only the first of each tag is read. Proceeding tags are ignored. + Only the first database, and the first auths of each type are used. + Proceeding tags are ignored. + Do not have empty element nodes as this will break things. --> -<config version="1.0" > +<config version="1.2" > <database> <host>4574.co.uk</host> @@ -15,8 +16,13 @@ <auth> <user>xauth</user> - <pass></pass> + <pass>pass</pass> <type>read_only</type> </auth> + <auth> + <user>xwrite</user> + <pass>password</pass> + <type>read_write</type> + </auth> </config>
\ No newline at end of file diff --git a/src/uk/org/ury/config/Auth.java b/src/uk/org/ury/config/Auth.java index d68fc55..81be6e2 100644 --- a/src/uk/org/ury/config/Auth.java +++ b/src/uk/org/ury/config/Auth.java @@ -1,17 +1,14 @@ package uk.org.ury.config; -import uk.org.ury.database.UserClass; - /** * A login authorisation configuration * - * @author Nathan + * @author Nathan Lasseter */ public class Auth { private String user; private String pass; - private UserClass type; /** * Get the username of the login @@ -25,23 +22,15 @@ public class Auth { * @return String password */ public String getPass() { return pass; } - /** - * Get the access level of the login - * - * @return UserClass type - */ - public UserClass getType() { return type; } /** * Create a login auth object * @param user The username for the login * @param pass The password for the login - * @param type The access type for the login */ - public Auth(String user, String pass, UserClass type) { + public Auth(String user, String pass) { this.user = user; this.pass = pass; - this.type = type; } }
\ No newline at end of file diff --git a/src/uk/org/ury/config/ConfigReader.java b/src/uk/org/ury/config/ConfigReader.java index 6166ec4..04deb49 100644 --- a/src/uk/org/ury/config/ConfigReader.java +++ b/src/uk/org/ury/config/ConfigReader.java @@ -1,24 +1,29 @@ package uk.org.ury.config; import java.io.File; -import org.w3c.dom.*; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; -import uk.org.ury.database.UserClass; +import uk.org.ury.database.exceptions.MissingCredentialsException; /** * Reads in an XML config file and creates config objects * - * @author Nathan + * @author Nathan Lasseter */ public class ConfigReader { - private Database database; - private Auth auth; + private Database database = null; + private Auth roAuth = null; + private Auth rwAuth = null; /** * Get the database configuration @@ -27,43 +32,67 @@ public class ConfigReader { */ public Database getDatabase() { return database; } /** - * Get the login auth configuration + * Get the read only login auth configuration + * + * @return Auth roAauth + */ + public Auth getRoAuth() { return roAuth; } + /** + * Get the read write login auth configuration * - * @return Auth auth + * @return Auth rwAauth */ - public Auth getAuth() { return auth; } + public Auth getRwAuth() { return rwAuth; } /** * Read in the config file and create the Database and Auth configuration objects. + * Specify a config file. + * @throws MissingCredentialsException */ - public ConfigReader() { - new ConfigReader("res/conf.xml"); - } - - public ConfigReader(String configFile) { + public ConfigReader(String configFile) throws MissingCredentialsException { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File(configFile)); doc.getDocumentElement().normalize(); - - String user = doc.getElementsByTagName("user").item(0).getTextContent(); - String pass = doc.getElementsByTagName("pass").item(0).getTextContent(); - UserClass type; - if(doc.getElementsByTagName("type").item(0).getTextContent().toLowerCase().equals("read_only")) { - type = UserClass.READ_ONLY; - } else if(doc.getElementsByTagName("type").item(0).getTextContent().toLowerCase().equals("read_write")) { - type = UserClass.READ_WRITE; - } else { - throw new IllegalArgumentException("Unused user class."); + + NodeList nList = doc.getElementsByTagName("auth"); + for(int i = 0; i < nList.getLength(); i++) { + Node nNode = nList.item(i); + if(nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + String user = getTagValue("user", eElement); + String pass = getTagValue("pass", eElement); + String sType = getTagValue("type", eElement); + if(sType.equalsIgnoreCase("read_only")) { + if(roAuth != null) continue; + roAuth = new Auth(user, pass); + } else if(sType.equalsIgnoreCase("read_write")) { + if(rwAuth != null) continue; + rwAuth = new Auth(user, pass); + } else { + throw new IllegalArgumentException("Unused user class."); + } + } } - auth = new Auth(user, pass, type); - String host = doc.getElementsByTagName("host").item(0).getTextContent(); - int port = Integer.parseInt(doc.getElementsByTagName("port").item(0).getTextContent().trim()); - String db = doc.getElementsByTagName("db").item(0).getTextContent(); - database = new Database(host, port, db); + nList = doc.getElementsByTagName("database"); + for(int i = 0; i < nList.getLength(); i++) { + if(database != null) break; + Node nNode = nList.item(i); + if(nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + String host = getTagValue("host", eElement); + String port = getTagValue("port", eElement); + String db = getTagValue("db", eElement); + database = new Database(host, Integer.parseInt(port.trim()), db); + } + } + } + + catch(NullPointerException n) { + throw new MissingCredentialsException("An element node is empty."); } catch (SAXParseException err) { @@ -78,4 +107,11 @@ public class ConfigReader { t.printStackTrace (); } } + + private static String getTagValue(String sTag, Element eElement){ + NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes(); + Node nValue = (Node) nlList.item(0); + + return nValue.getNodeValue(); + } } diff --git a/src/uk/org/ury/config/Database.java b/src/uk/org/ury/config/Database.java index a82f775..19fe9bc 100644 --- a/src/uk/org/ury/config/Database.java +++ b/src/uk/org/ury/config/Database.java @@ -3,7 +3,7 @@ package uk.org.ury.config; /** * A Database Server configuration * - * @author Nathan + * @author Nathan Lasseter */ public class Database { diff --git a/src/uk/org/ury/database/DatabaseDriver.java b/src/uk/org/ury/database/DatabaseDriver.java index 732714c..ca7a928 100644 --- a/src/uk/org/ury/database/DatabaseDriver.java +++ b/src/uk/org/ury/database/DatabaseDriver.java @@ -7,6 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import uk.org.ury.config.ConfigReader; import uk.org.ury.database.exceptions.ConnectionFailureException; import uk.org.ury.database.exceptions.MissingCredentialsException; @@ -17,13 +18,14 @@ import uk.org.ury.database.exceptions.MissingCredentialsException; * of SQL queries. * * @author Matt Windsor + * @author Nathan Lasseter * */ public class DatabaseDriver { /* The JDBC path used to connect to the URY database. */ - private String DATABASE_PATH = "jdbc:postgresql://4574.co.uk/membership"; + private String DATABASE_PATH = "jdbc:postgresql://"; /* The database connection. */ private Connection conn; @@ -32,6 +34,7 @@ public class DatabaseDriver /** * Construct a new DatabaseDriver with the given user class. * + * @param config The config with login details. * @param userclass The user class to log in to the database with. * * @throws IllegalArgumentException if the user class is @@ -46,27 +49,14 @@ public class DatabaseDriver */ public - DatabaseDriver (UserClass userclass) throws MissingCredentialsException, ConnectionFailureException + DatabaseDriver (ConfigReader config, UserClass type) throws MissingCredentialsException, ConnectionFailureException { - DatabaseLogin login = null; - switch (userclass) - { - case READ_ONLY: - login = DatabaseLogin.getLoginFromFile ("read_only.txt"); - break; - case READ_WRITE: - login = DatabaseLogin.getLoginFromFile ("read_write.txt"); - default: - throw new IllegalArgumentException ("Unused user class."); - } - - try { System.out.println ("Trying to acquire connection..."); - connect (login); + connect(config, type); System.out.println ("...connection succeeded."); } @@ -81,27 +71,43 @@ public class DatabaseDriver /** * Connect to the URY database. * - * @param login The login tuple to use for the connection. + * @param config The config to use for the connection. + * @param type The access level of the connection * * @throws SQLException if the database connection failed. */ private void - connect (DatabaseLogin login) throws SQLException + connect (ConfigReader config, UserClass type) throws SQLException { - if (login == null) - throw new IllegalArgumentException ("Supplied null login."); + if (config == null) + throw new IllegalArgumentException ("Supplied null config."); - if (login.getUsername () == null) - throw new IllegalArgumentException ("Login has no associated username."); - - if (login.getPassword () == null) - throw new IllegalArgumentException ("Login has no associated password."); + if (config.getDatabase().getHost() == null) + throw new IllegalArgumentException ("config has no associated host."); + + if (config.getDatabase().getDb() == null) + throw new IllegalArgumentException ("config has no associated database."); + DATABASE_PATH = DATABASE_PATH + config.getDatabase().getHost() + "/" + config.getDatabase().getDb(); - conn = DriverManager.getConnection (DATABASE_PATH, - login.getUsername (), - login.getPassword ()); + if(type == UserClass.READ_ONLY) { + if(config.getRoAuth().getUser() == null) + throw new IllegalArgumentException ("config has no associated username."); + if(config.getRoAuth().getPass() == null) + throw new IllegalArgumentException ("config has no associated password."); + conn = DriverManager.getConnection (DATABASE_PATH, + config.getRoAuth().getUser(), + config.getRoAuth().getPass()); + } else if(type == UserClass.READ_WRITE) { + if(config.getRwAuth().getUser() == null) + throw new IllegalArgumentException ("config has no associated username."); + if(config.getRwAuth().getPass() == null) + throw new IllegalArgumentException ("config has no associated password."); + conn = DriverManager.getConnection (DATABASE_PATH, + config.getRwAuth().getUser(), + config.getRwAuth().getPass()); + } } diff --git a/src/uk/org/ury/database/DatabaseLogin.java b/src/uk/org/ury/database/DatabaseLogin.java deleted file mode 100644 index e2f22bf..0000000 --- a/src/uk/org/ury/database/DatabaseLogin.java +++ /dev/null @@ -1,150 +0,0 @@ -/** - * - */ -package uk.org.ury.database; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.net.URL; - -import uk.org.ury.database.exceptions.MissingCredentialsException; - - -/** - * A login username/password pair. - * - * @author Matt Windsor - */ - -public class DatabaseLogin -{ - private String username; - private String password; - - - /** - * Create a new DatabaseLogin. - * - * This constructor is intentionally left private to prevent - * coders from hard-coding passwords. Please read database - * credentials from a source external to the program code. - * - * @param username The database username. - * @param password The database password. - * - * @see getLoginFromFile - */ - - private - DatabaseLogin (String username, String password) - { - this.username = username; - this.password = password; - } - - - /** - * Retrieve login credentials from a plaintext file. - * - * The credentials should be listed in the following format: - * - * username <newline> - * password - * - * @param file The filename of the file - * - * @return a new DatabaseLogin containing the information - * retrieved from the file. - * - * @throws IllegalArgumentException if the filename is null. - * - * @throws MissingCredentialsException if the file does not exist - * or is of the wrong format. - */ - - public static DatabaseLogin - getLoginFromFile (String file) throws MissingCredentialsException - { - // Find the credentials file. - - if (file == null) - throw new IllegalArgumentException ("Supplied null credentials filename."); - - URL fileURL = DatabaseLogin.class.getResource ("credentials/" + file); - - if (fileURL == null) - throw new MissingCredentialsException ("Credentials file " - + file - + " not found."); - - - String username = null; - String password = null; - BufferedReader reader = null; - - - try - { - reader = new BufferedReader (new FileReader (fileURL.getFile ())); - } - catch (FileNotFoundException e) - { - throw new MissingCredentialsException ("Credentials file " - + file - + " not found."); - } - - try - { - username = reader.readLine (); - password = reader.readLine (); - } - catch (IOException e) - { - throw new MissingCredentialsException ("Credentials file " - + file - + "is invalid."); - } - finally - { - try - { - reader.close (); - } - catch (IOException e) - { - // TODO Auto-generated catch block - e.printStackTrace (); - } - } - - - DatabaseLogin login = new DatabaseLogin (username, password); - - return login; - } - - - /** - * @return the username. - */ - - public String - getUsername () - { - return username; - } - - - /** - * @return the password. - */ - - public String - getPassword () - { - return password; - } -} diff --git a/src/uk/org/ury/database/credentials/.gitignore b/src/uk/org/ury/database/credentials/.gitignore deleted file mode 100644 index 2211df6..0000000 --- a/src/uk/org/ury/database/credentials/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.txt diff --git a/src/uk/org/ury/library/viewer/LibraryViewer.java b/src/uk/org/ury/library/viewer/LibraryViewer.java index 970235a..a631e89 100644 --- a/src/uk/org/ury/library/viewer/LibraryViewer.java +++ b/src/uk/org/ury/library/viewer/LibraryViewer.java @@ -31,6 +31,7 @@ public class LibraryViewer extends AbstractFrontendModule private List<LibraryItem> libraryList; private LibraryViewerPanel panel; private FrontendFrame frame; + private static ConfigReader config; /** @@ -43,7 +44,12 @@ public class LibraryViewer extends AbstractFrontendModule public static void main (String[] args) { - new ConfigReader(); + try { + config = new ConfigReader("res/conf.xml"); + } + catch(MissingCredentialsException e) { + System.out.println(e); + } LibraryViewer lv = new LibraryViewer (); lv.runFrontendInFrame (); } @@ -141,7 +147,7 @@ public class LibraryViewer extends AbstractFrontendModule try { - dd = new DatabaseDriver (UserClass.READ_ONLY); + dd = new DatabaseDriver(config, UserClass.READ_ONLY); } catch (MissingCredentialsException e) { |