Skip to content
Snippets Groups Projects
Commit 4077a8fa authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Add new settings handling; add new plugin routes; update client

parent a35ead27
No related branches found
No related tags found
No related merge requests found
Showing
with 190 additions and 39 deletions
# Database
host=localhost
port=3306
username=root
password=password
database=PlayWall
\ No newline at end of file
package de.tobias.playpad.server
import java.nio.file.{Files, Paths}
import java.util.Properties
import com.google.gson.Gson
import com.j256.ormlite.dao.{Dao, DaoManager}
import com.j256.ormlite.jdbc.JdbcConnectionSource
import com.j256.ormlite.table.TableUtils
import de.tobias.playpad.server.plugin.Plugin
import spark.Spark
import de.tobias.playpad.server.server.plugin.{PluginGet, PluginList}
import de.tobias.playpad.server.settings.SettingsHandler
import de.tobias.playpad.server.transformer.JsonTransformer
import spark.Spark._
import spark.route.RouteOverview
......@@ -18,40 +18,35 @@ import spark.route.RouteOverview
object PlayPadServer extends App {
// Load Config
val properties = new Properties()
private val path = Paths.get("settings.properties")
if (Files.notExists(path)) {
val loader = Thread.currentThread.getContextClassLoader
val input = loader.getResourceAsStream("settings/settings.properties")
Files.copy(input, path)
private val settingsLoader = SettingsHandler.loader
private val settingsPath = Paths.get("settings.properties")
if (Files.notExists(settingsPath)) {
SettingsHandler.saver.default(settingsPath)
}
properties.load(Files.newBufferedReader(path))
// Setup Database
private val host = properties.getProperty("host")
private val port = properties.getProperty("port")
private val username = properties.getProperty("username")
private val password = properties.getProperty("password")
private val database = properties.getProperty("database")
private val settings = settingsLoader.load(settingsPath)
private val databaseUrl = "jdbc:mysql://" + host + ":" + port + "/" + database
private val databaseUrl = "jdbc:mysql://" + settings.db_host + ":" + settings.db_port + "/" + settings.db_database
var connectionSource = new JdbcConnectionSource(databaseUrl)
connectionSource.setUsername(username)
connectionSource.setPassword(password)
connectionSource.setUsername(settings.db_username)
connectionSource.setPassword(settings.db_password)
val dao: Dao[Plugin, String] = DaoManager.createDao(connectionSource, classOf[Plugin])
val dao: Dao[Plugin, Int] = DaoManager.createDao(connectionSource, classOf[Plugin])
TableUtils.createTableIfNotExists(connectionSource, classOf[Plugin])
// Setup Http Server
Spark.port(8090)
secure("deploy/keystore.jks", "password", null, null);
get("/", (req, res) => "Hallo World")
port(8090)
private val externalPath = Paths.get(settings.download_folder).toAbsolutePath.toString
externalStaticFileLocation(externalPath)
get("/plugin/list", (req, res) => {
val plugins = dao.queryForAll()
val gson = new Gson()
gson.toJson(plugins)
})
secure("deploy/keystore.jks", settings.keystorePassword, null, null)
get("/plugins/:id", new PluginGet(dao), new JsonTransformer)
get("/plugins", new PluginList(dao), new JsonTransformer)
RouteOverview.enableRouteOverview()
SettingsHandler.saver.save(settings, settingsPath)
}
package de.tobias.playpad.server.plugin
import com.j256.ormlite.dao.ForeignCollection
import com.j256.ormlite.field.{DatabaseField, ForeignCollectionField}
import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.table.DatabaseTable
import com.sun.xml.internal.xsom.impl.ForeignAttributesImpl
/**
* Created by tobias on 31.01.17.
......@@ -11,13 +9,17 @@ import com.sun.xml.internal.xsom.impl.ForeignAttributesImpl
@DatabaseTable(tableName = "Plugin") class Plugin() {
@DatabaseField(generatedId = true) private val id: Int = 0
@DatabaseField var name: String = _
@DatabaseField var displayName: String = _
@DatabaseField var description: String = _
@DatabaseField var path: String = _
@DatabaseField var version: String = _
@DatabaseField var build: Int = _
def this(name: String, description: String, version: String, build: Int) {
def this(name: String, displayName: String, path: String, description: String, version: String, build: Int) {
this()
this.name = name
this.displayName = displayName
this.path = path
this.description = description
this.version = version
this.build = build
......
package de.tobias.playpad.server.server.plugin
import com.google.gson.Gson
import com.j256.ormlite.dao.Dao
import de.tobias.playpad.server.plugin.Plugin
import spark.{Request, Response, Route, Spark}
/**
* Created by tobias on 05.02.17.
*/
class PluginGet(dao: Dao[Plugin, Int]) extends Route {
val gson = new Gson()
override def handle(request: Request, response: Response): AnyRef = {
val plugin = dao.queryForId(request.params(":id").toInt)
if (plugin == null) {
Spark.halt(400, "Bad request")
}
plugin
}
}
package de.tobias.playpad.server.server.plugin
import com.google.gson.Gson
import com.j256.ormlite.dao.Dao
import de.tobias.playpad.server.plugin.Plugin
import spark.{Request, Response, Route}
/**
* Created by tobias on 05.02.17.
*/
class PluginList(val dao: Dao[Plugin, Int]) extends Route {
val gson = new Gson()
override def handle(request: Request, response: Response): AnyRef = {
val plugins = dao.queryForAll()
plugins
}
}
package de.tobias.playpad.server.settings
import java.io.IOException
import java.lang.reflect.Modifier
import java.nio.file.{Files, Path}
import java.util.Properties
/**
* Created by tobias on 05.02.17.
*/
class PropertiesSettingsHandler extends SettingsLoader with SettingsSaver {
@throws[IOException]
override def load(path: Path): Settings = {
val properties = new Properties()
properties.load(Files.newBufferedReader(path))
val settings = new Settings()
classOf[Settings].getDeclaredFields.filter(f => !Modifier.isTransient(f.getModifiers))
.filter(f => properties.containsKey(f.getName))
.foreach(f => {
f.setAccessible(true)
if (f.getType == Integer.TYPE) {
f.setInt(settings, properties.getProperty(f.getName).toInt)
} else {
f.set(settings, properties.getProperty(f.getName))
}
})
settings
}
@throws[IOException]
override def save(settings: Settings, path: Path): Unit = {
val properties = new Properties()
println("Save")
classOf[Settings].getDeclaredFields.filter(f => !Modifier.isTransient(f.getModifiers))
.foreach(f => {
f.setAccessible(true)
properties.setProperty(f.getName, f.get(settings).toString)
})
properties.store(Files.newOutputStream(path), "Settings")
}
}
package de.tobias.playpad.server.settings
/**
* Created by tobias on 05.02.17.
*/
class Settings {
var db_host: String = "localhost"
var db_port: Int = 3306
var db_username: String = "root"
var db_password: String = "password"
var db_database: String = "PlayWall"
var download_folder: String = "./"
var keystorePassword = "password"
}
package de.tobias.playpad.server.settings
/**
* Created by tobias on 05.02.17.
*/
object SettingsHandler {
def loader = new PropertiesSettingsHandler()
def saver = new PropertiesSettingsHandler()
}
package de.tobias.playpad.server.settings
import java.io.IOException
import java.nio.file.Path
/**
* Created by tobias on 05.02.17.
*/
trait SettingsLoader {
@throws[IOException]
def load(path: Path): Settings
}
package de.tobias.playpad.server.settings
import java.io.IOException
import java.nio.file.Path
/**
* Created by tobias on 05.02.17.
*/
trait SettingsSaver {
@throws[IOException]
def save(settings: Settings, path: Path)
def default(path: Path) = save(new Settings(), path)
}
package de.tobias.playpad.server.transformer
import com.google.gson.Gson
import spark.ResponseTransformer
/**
* Created by tobias on 05.02.17.
*/
class JsonTransformer extends ResponseTransformer {
val gson = new Gson()
override def render(o: scala.Any): String = {
gson.toJson(o)
}
}
......@@ -33,7 +33,7 @@ public class ClientTest {
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> true);
URL url = new URL("https://localhost:6789");
URL url = new URL("https://localhost:8090/plugins");
URLConnection conn = url.openConnection();
InputStream stream = conn.getInputStream();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment