136 lines
4.4 KiB
Java
136 lines
4.4 KiB
Java
package de.gurkengewuerz.postfix_rest_send;
|
|
|
|
import org.json.JSONObject;
|
|
import org.json.JSONTokener;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.nio.file.AccessDeniedException;
|
|
import java.nio.file.Files;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
* Created by gurkengewuerz.de on 02.07.2017.
|
|
*/
|
|
public class Config extends JSONObject {
|
|
private File file;
|
|
private JSONObject database;
|
|
private boolean firstRun = false;
|
|
|
|
public Config(File file) throws IOException {
|
|
this.file = file;
|
|
|
|
if (!file.exists()) {
|
|
file.createNewFile();
|
|
firstRun = true;
|
|
}
|
|
|
|
if (!file.isFile()) {
|
|
throw new FileNotFoundException(file.getAbsolutePath() + " not found");
|
|
}
|
|
|
|
if (!file.canRead() || !file.canWrite()) {
|
|
throw new AccessDeniedException(file.getAbsolutePath() + " is not accessable");
|
|
}
|
|
|
|
this.put("debug", true);
|
|
this.put("disable_bruteforcefilter", false);
|
|
this.put("token_expire", 730); // houres/1M
|
|
this.put("http_port", 8081);
|
|
this.put("postfixadmin_encryption", "md5crypt");
|
|
this.put("postfixadmin_path", "/var/www/html/postfixadmin/");
|
|
|
|
JSONObject database = new JSONObject();
|
|
database.put("port", 3306);
|
|
database.put("host", "127.0.0.1");
|
|
database.put("db", "mail");
|
|
database.put("username", "postfixadmin");
|
|
database.put("password", "abc123");
|
|
this.put("database", database);
|
|
}
|
|
|
|
public void save() {
|
|
try {
|
|
FileWriter fw = new FileWriter(file.getAbsolutePath());
|
|
fw.write(this.toString(4));
|
|
fw.close();
|
|
loadRestData();
|
|
} catch (IOException e) {
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
}
|
|
|
|
public void load() {
|
|
try {
|
|
String content = new String(Files.readAllBytes(file.toPath()), "UTF-8");
|
|
if (content.isEmpty()) {
|
|
save();
|
|
return;
|
|
}
|
|
JSONTokener jt = new JSONTokener(content);
|
|
if (jt.nextClean() != 123) {
|
|
throw jt.syntaxError("A JSONObject text must begin with '{'");
|
|
} else {
|
|
while (jt.more()) {
|
|
char c = jt.nextClean();
|
|
switch (c) {
|
|
case '\u0000':
|
|
throw jt.syntaxError("A JSONObject text must end with '}'");
|
|
case '}':
|
|
return;
|
|
default:
|
|
jt.back();
|
|
String key = jt.nextValue().toString();
|
|
c = jt.nextClean();
|
|
if (c != 58) {
|
|
throw jt.syntaxError("Expected a ':' after a key");
|
|
}
|
|
|
|
this.remove(key);
|
|
this.putOnce(key, jt.nextValue());
|
|
switch (jt.nextClean()) {
|
|
case ',':
|
|
case ';':
|
|
if (jt.nextClean() == 125) {
|
|
return;
|
|
}
|
|
|
|
jt.back();
|
|
break;
|
|
case '}':
|
|
save();
|
|
return;
|
|
default:
|
|
throw jt.syntaxError("Expected a ',' or '}'");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
}
|
|
|
|
public boolean debug() {
|
|
return getBoolean("debug");
|
|
}
|
|
|
|
private void loadRestData() {
|
|
database = getJSONObject("database");
|
|
}
|
|
|
|
public JSONObject getDatabaseConfig() {
|
|
return database;
|
|
}
|
|
|
|
public String getDBURL() {
|
|
return "jdbc:mysql://" + getDatabaseConfig().getString("host") + ":" + getDatabaseConfig().getInt("port") + "/" + getDatabaseConfig().getString("db");
|
|
}
|
|
|
|
public boolean isFirstRun() {
|
|
return firstRun;
|
|
}
|
|
} |