src/de/uhilger/mediaz/Server.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/mediaz/api/AblageTestHandler.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/mediaz/api/StoreTestHandler.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/mediaz/conf/Store.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/mediaz/entity/Ablage.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/mediaz/entity/ConfigurationElement.java | ●●●●● patch | view | raw | blame | history | |
src/mediaz_de_DE.properties | ●●●●● patch | view | raw | blame | history |
src/de/uhilger/mediaz/Server.java
@@ -18,12 +18,13 @@ package de.uhilger.mediaz; import com.sun.net.httpserver.HttpServer; import de.uhilger.mediaz.api.AblageTestHandler; import de.uhilger.mediaz.api.FileHandler; import de.uhilger.mediaz.api.StopServerHandler; import de.uhilger.mediaz.api.StoreTestHandler; import java.io.IOException; import java.util.logging.Logger; import java.net.InetSocketAddress; import java.util.ResourceBundle; import java.util.concurrent.Executors; import java.util.logging.Level; @@ -41,6 +42,8 @@ public static final String RB_SERVER_START_MSG = "msgServerStart"; public static final String RB_WEBROOT = "webroot"; public static final String RB_STOP_SERVER = "stopServer"; public static final String RB_ABLAGE_TEST = "testAblage"; public static final String RB_STORE_TEST = "testStore"; public static final String RB_SLASH = "slash"; private int port; @@ -91,6 +94,8 @@ HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(App.getInitParameter(App.getRs(App.RB_AP_WWW_DATA)))); server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler()); server.createContext(ctx + App.getRs(RB_ABLAGE_TEST), new AblageTestHandler()); server.createContext(ctx + App.getRs(RB_STORE_TEST), new StoreTestHandler()); server.setExecutor(Executors.newFixedThreadPool(20)); server.start(); } src/de/uhilger/mediaz/api/AblageTestHandler.java
New file @@ -0,0 +1,48 @@ /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package de.uhilger.mediaz.api; import com.google.gson.Gson; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import de.uhilger.mediaz.entity.Ablage; import java.io.File; import java.io.IOException; import java.io.OutputStream; /** * * @author ulrich */ public class AblageTestHandler implements HttpHandler { @Override public void handle(HttpExchange e) throws IOException { Ablage ablage = new Ablage(); ablage.setName("Katalog"); ablage.setOrt("/home/ulrich/Videos"); Gson gson = new Gson(); File mediaOrdner = new File(ablage.getOrt()); File[] files = mediaOrdner.listFiles(); String json = gson.toJson(files); StringBuilder sb = new StringBuilder(); sb.append(json); json = gson.toJson(ablage); sb.append(json); byte[] b = sb.toString().getBytes(); //String response = getResponseString(map, cmd, antwort); e.sendResponseHeaders(200, b.length); OutputStream os = e.getResponseBody(); os.write(b); os.close(); } } src/de/uhilger/mediaz/api/StoreTestHandler.java
New file @@ -0,0 +1,51 @@ /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package de.uhilger.mediaz.api; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import de.uhilger.mediaz.conf.Store; import de.uhilger.mediaz.entity.Ablage; import de.uhilger.mediaz.entity.ConfigurationElement; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author ulrich */ public class StoreTestHandler implements HttpHandler { private static final Logger logger = Logger.getLogger(StoreTestHandler.class.getName()); @Override public void handle(HttpExchange e) throws IOException { Ablage ablage = new Ablage(); ablage.setName("Katalog"); ablage.setOrt("/home/ulrich/Videos"); Store store = new Store(); File file = store.writeToFile(ablage); try { ConfigurationElement elem = store.readFromFile(file); logger.log(Level.INFO, "Typ: {0}, Name: {1}", new Object[]{elem.getClass().getSimpleName(), elem.getName()}); } catch (ClassNotFoundException ex) { logger.log(Level.SEVERE, null, ex); } String response = "OK"; e.sendResponseHeaders(200, response.length()); OutputStream os = e.getResponseBody(); os.write(response.getBytes()); os.close(); } } src/de/uhilger/mediaz/conf/Store.java
New file @@ -0,0 +1,99 @@ /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package de.uhilger.mediaz.conf; import com.google.gson.Gson; import de.uhilger.mediaz.App; import de.uhilger.mediaz.Server; import de.uhilger.mediaz.entity.Ablage; import de.uhilger.mediaz.entity.ConfigurationElement; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.logging.Logger; /** * * @author ulrich */ public class Store { private static final Logger logger = Logger.getLogger(Store.class.getName()); private static final String typeAblage = "Ablage"; /** * Ein Objekt als JSON in eine Datei schreiben * * Es wird in den Ordner geschrieben, der von conf angegeben ist * * Wenn es z.B. ein Ablage-Objekt ist, wird das Objekt in die Datei * [conf]/Ablage/[name der Ablage].json geschrieben * * Der Name der Ablage muss eindeutig sein * * * @param o */ public File writeToFile(ConfigurationElement o) throws IOException { Gson gson = new Gson(); String className = o.getClass().getSimpleName(); logger.finer(className); File dir = new File(App.getInitParameter(App.getRs(App.RB_AP_CONF)), className); dir.mkdirs(); File file = new File(dir, o.getName()); if(file.exists()) { file.delete(); } FileWriter fw = new FileWriter(file); fw.write(gson.toJson(o)); fw.flush(); fw.close(); return file; } public ConfigurationElement readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { String type = typeFromName(file); StringBuilder sb = new StringBuilder(); FileReader in = new FileReader(file); BufferedReader r = new BufferedReader(in); String line = r.readLine(); while(line != null) { sb.append(line); line = r.readLine(); } r.close(); in.close(); String json = sb.toString(); Gson gson = new Gson(); switch(type) { case typeAblage: return gson.fromJson(json, Ablage.class); default: Ablage ablage = new Ablage(); ablage.setName("Test"); return ablage; } } private String typeFromName(File file) { //String path = file.getPath(); //logger.info(path); String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); //for(String part : parts) { //logger.info(part); //} //logger.info("" + parts.length); logger.info(parts[parts.length-2]); return parts[parts.length-2]; } } src/de/uhilger/mediaz/entity/Ablage.java
New file @@ -0,0 +1,34 @@ /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package de.uhilger.mediaz.entity; /** * * @author ulrich */ public class Ablage implements ConfigurationElement { private String name; private String ort; @Override public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOrt() { return ort; } public void setOrt(String ort) { this.ort = ort; } } src/de/uhilger/mediaz/entity/ConfigurationElement.java
New file @@ -0,0 +1,14 @@ /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package de.uhilger.mediaz.entity; /** * * @author ulrich */ public interface ConfigurationElement { public String getName(); } src/mediaz_de_DE.properties
@@ -8,6 +8,8 @@ # API-Endpunkte webroot=/ stopServer=/server/stop testAblage=/test/ablage testStore=/test/store slash=/ dash=-