Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| package me.devyashis.DevLib; | |
| import java.io.IOException; | |
| import me.devyashis.DevLib.HttpServer.HttpServer; | |
| import me.devyashis.DevLib.HttpServer.HttpRequest; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.Map; | |
| import java.io.BufferedReader; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileReader; | |
| import java.io.PrintWriter; | |
| import java.io.UnsupportedEncodingException; | |
| import java.net.URLDecoder; | |
| import java.net.URLEncoder; | |
| public class ConfigParser extends HttpServer | |
| { | |
| private HashMap<String, String> _conf = new HashMap<String, String>(); | |
| private String _cfgloc = null; | |
| public ConfigParser(String cfgloc, int port) throws IOException | |
| { | |
| super(port); | |
| _cfgloc = cfgloc; | |
| readConfiguration(); | |
| } | |
| public String get(String key) | |
| { | |
| readConfiguration(); | |
| return _conf.get(key); | |
| } | |
| public void put(String key, String data) | |
| { | |
| readConfiguration(); | |
| _conf.put(key, data); | |
| writeConfiguration(); | |
| } | |
| public HttpRequest handleRequest(HttpRequest session) | |
| { | |
| readConfiguration(); | |
| //If we are at document root | |
| if(session.path.equals("/")) | |
| { | |
| String s = ""; | |
| s += "<html>"; | |
| s += "<head>"; | |
| s += "<title>Configuration Utility!</title>"; | |
| s += "</head>"; | |
| s += "<body>"; | |
| s += "<h1>Configuration: <span style=\"color: SpringGreen\">" + _cfgloc + "</span></h1>"; | |
| s += "<a href=\"/editconf\">Edit the configuration file!</a>"; | |
| s += "<table style=\"width: 100%; background-color: #404040; color: #C6C6C6;\">"; | |
| s += "<tr><th style=\"width: 20%;\">Key</th><th>Value</th><th style=\"width: 29px;\">→</th><th style=\"width: 25px;\">X</th></tr></table>"; | |
| Iterator it = _conf.entrySet().iterator(); | |
| while(it.hasNext()) | |
| { | |
| Map.Entry tuple = (Map.Entry) it.next(); | |
| s += "<form style=\"margin: 0px; border-bottom: 2px solid #404040;\" action=\"/setval\" method=\"POST\" id=\"entry_" + tuple.getKey() + "\">"; | |
| s += "<table style=\"width: 100%; background-color: #C6C6C6;\">"; | |
| s += "<tr>"; | |
| s += "<td style=\"width: 20%; border-right: 2px solid #404040;\">" + tuple.getKey() + "<input readonly name=\"name\" type=\"text\" style=\"display: none;\" value=\"" + tuple.getKey() + "\"></td>"; | |
| s += "<td style=\"border-right: 2px solid #404040;\"><input style=\"width: 100%;\" type=\"text\" name=\"value\" placeholder=\"value\" value=\"" + tuple.getValue() + "\">"; | |
| s += "<td style=\"width: 29px; border-right: 2px solid #404040;\"><input type=\"submit\" value=\"→\"></td>"; | |
| s += "<td style=\"width: 25px;\"><input type=\"submit\" value=\"X\""; | |
| s += "onmouseover=\"javascript:document.getElementById('entry_" + tuple.getKey() + "').action = '/delval';\""; | |
| s += "onmouseout=\"javascript:document.getElementById('entry_" + tuple.getKey() + "').action = '/setval';\""; | |
| s += "></td>"; | |
| s += "</tr>"; | |
| s += "</table>"; | |
| s += "</form>"; | |
| it.remove(); | |
| } | |
| s += "<form action=\"/setval\" method=\"POST\">"; | |
| s += "<table style=\"width: 100%; background-color: #C6C6C6;\">"; | |
| s += "<tr>"; | |
| s += "<td style=\"width: 20%;\"><input style=\"width: 100%;\" name=\"name\" placeholder=\"Key\" type=\"text\"></td>"; | |
| s += "<td><input style=\"width: 100%;\" type=\"text\" name=\"value\" placeholder=\"Value\">"; | |
| s += "<td style=\"width: 29px;\"><input type=\"submit\" value=\"→\"></td>"; | |
| s += "<td style=\"width: 25px;\">X</td>"; | |
| s += "</tr>"; | |
| s += "</table>"; | |
| s += "</form>"; | |
| s += "</body>"; | |
| s += "</html>"; | |
| session.response = s; | |
| } | |
| else if(session.path.equals("/setval")) | |
| { | |
| session.status_code = 301; | |
| session.setHeader("location", "/"); | |
| try | |
| { | |
| String x = session.queries.get("name"); | |
| String y = session.queries.get("value"); | |
| if(x != null && y != null) _conf.put(x, y); | |
| } | |
| catch(Exception e) | |
| { | |
| session.status_code = 500; | |
| session.response = "<html><body><h1>An error occured!</h1><p>" + e.getMessage() + "</p></body></html>"; | |
| } | |
| writeConfiguration(); | |
| } | |
| else if(session.path.equals("/delval")) | |
| { | |
| session.status_code = 301; | |
| session.setHeader("location", "/"); | |
| try | |
| { | |
| _conf.remove(session.queries.get("name")); | |
| writeConfiguration(); | |
| session.response = "Success!"; | |
| } | |
| catch(Exception e) | |
| { | |
| session.response = "Deletion Failed!"; | |
| } | |
| } | |
| else if(session.path.equals("/editconf")) | |
| { | |
| String s = ""; | |
| s += "<html>"; | |
| s += "<head>"; | |
| s += "<title>Edit Configuration File</title>"; | |
| s += "</head>"; | |
| s += "<body>"; | |
| s += "<h1>Edit Configuration File: <span style=\"color: SpringGreen;\">" + _cfgloc + "</span></h1>"; | |
| s += "<a href=\"/\">Configuration Interface</a>"; | |
| s += "<form action=\"/saveconf\" method=\"POST\">"; | |
| s += "<textarea name=\"filedata\" style=\"width: 100%; min-height: 300px;\">" + readFile(_cfgloc) + "</textarea>"; | |
| s += "<input type=\"submit\" value=\"Save Edits!\">"; | |
| s += "</form>"; | |
| s += "</body>"; | |
| s += "</html>"; | |
| session.response = s; | |
| } | |
| else if(session.path.equals("/saveconf")) | |
| { | |
| session.status_code = 301; | |
| session.setHeader("location", "/editconf"); | |
| writeFile(_cfgloc, session.queries.get("filedata")); | |
| } | |
| return session; | |
| } | |
| private String readFile(String location) | |
| { | |
| try | |
| { | |
| BufferedReader reader = new BufferedReader(new FileReader(_cfgloc)); | |
| String data = ""; | |
| int chbuf = 0; | |
| while((chbuf = reader.read()) != -1) | |
| { | |
| data += (char) chbuf; | |
| } | |
| return data; | |
| } | |
| catch(Exception e) | |
| { | |
| System.out.println("Something went wrong while reading the file!"); | |
| return ""; | |
| } | |
| } | |
| private void writeFile(String location, String data) | |
| { | |
| try | |
| { | |
| PrintWriter out = new PrintWriter(location, "UTF-8"); | |
| out.write(data); | |
| out.flush(); | |
| out.close(); | |
| } | |
| catch(Exception e) | |
| { | |
| System.out.println("An error ocurred while writing to the file! -> " + e.getMessage()); | |
| } | |
| } | |
| private void readConfiguration() | |
| { | |
| _conf.clear(); | |
| try | |
| { | |
| BufferedReader reader = new BufferedReader(new FileReader(_cfgloc)); | |
| while(true) | |
| { | |
| String x = reader.readLine(); | |
| if(x == null) | |
| { | |
| reader.close(); | |
| break; | |
| } | |
| String key = URLDecoder.decode(x.substring(0, x.indexOf('=')).trim(), "UTF-8"); | |
| String value = URLDecoder.decode(x.substring(x.indexOf('=') + 1, x.length()).trim(), "UTF-8"); | |
| _conf.put(key, value); | |
| } | |
| } | |
| catch(Exception e) | |
| { | |
| System.out.println("An error occured while reading the configuration file!"); | |
| } | |
| } | |
| @SuppressWarnings("rawtypes") | |
| private void writeConfiguration() | |
| { | |
| try | |
| { | |
| PrintWriter writer = new PrintWriter(_cfgloc, "UTF-8"); | |
| Iterator it = _conf.entrySet().iterator(); | |
| while(it.hasNext()) | |
| { | |
| Map.Entry tuple = (Map.Entry) it.next(); | |
| writer.println(URLEncoder.encode((String) tuple.getKey(), "UTF-8") + "=" + URLEncoder.encode((String) tuple.getValue(), "UTF-8")); | |
| it.remove(); | |
| } | |
| writer.flush(); | |
| writer.close(); | |
| } | |
| catch (FileNotFoundException e) | |
| { | |
| System.out.println("Unable to find an existing configuration file"); | |
| } | |
| catch (UnsupportedEncodingException e) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |