From 2b6134e50801f9fc66a27a2d565340f5773f251f Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sun, 04 Apr 2021 11:24:49 +0000
Subject: [PATCH] ResourceBundle eingebaut

---
 src/de/uhilger/mediaz/App.java                   |   39 ++++++++----
 src/mediaz_de_DE.properties                      |   25 ++++++++
 src/de/uhilger/mediaz/Server.java                |   23 ++++---
 src/de/uhilger/mediaz/api/StopServerHandler.java |   16 ++++-
 src/de/uhilger/mediaz/api/FileHandler.java       |   38 +++++++-----
 5 files changed, 98 insertions(+), 43 deletions(-)

diff --git a/src/de/uhilger/mediaz/App.java b/src/de/uhilger/mediaz/App.java
index c20cba3..63d8cbb 100644
--- a/src/de/uhilger/mediaz/App.java
+++ b/src/de/uhilger/mediaz/App.java
@@ -20,6 +20,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.ResourceBundle;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -31,12 +32,20 @@
 
   private static final Logger logger = Logger.getLogger(App.class.getName());
 
-  public static final String IP_PORT = "port";
-  public static final String IP_WWW_DATA = "www-data";
-  public static final String IP_CTX = "ctx";
-  public static final String IP_CONF = "conf";
-
   private static HashMap initParams;
+  
+  /* ResourceBundle dieser App */
+  private static ResourceBundle rb;
+  
+  /* Name des ResourceBundles dieser App */
+  private static final String RB_NAME = "mediaz";
+  
+  /* ResourceBundle-Kennungen */
+  public static final String RB_PARAM_FEHLT = "msgParamFehlt";
+  public static final String RB_AP_PORT = "appParamPort";
+  public static final String RB_AP_CONF = "appParamConf";
+  public static final String RB_AP_WWW_DATA = "appParamWWWData"; 
+  public static final String RB_AP_CTX = "appParamCtx"; 
 
   /**
    * <p>Start-Methode dieser Anwendung</p>
@@ -44,7 +53,7 @@
    * @param args Kommandozeilenparameter
    */
   public static void main(String[] args) {
-    
+    rb = ResourceBundle.getBundle(RB_NAME);
     logger.info(new File(".").getAbsolutePath());
     
     initParams = new HashMap();
@@ -53,29 +62,29 @@
       initParams.put(argParts[0], argParts[1]);
     }
 
-    String portStr = getInitParameter(IP_PORT);
+    String portStr = getInitParameter(getRs(RB_AP_PORT));
     if (portStr != null) {
       Server server = new Server(Integer.parseInt(portStr));
       try {
-        String ctxName = getInitParameter(IP_CTX);
+        String ctxName = getInitParameter(getRs(RB_AP_CTX));
         if (ctxName != null) {
           server.setContextName(ctxName);
           server.start();
         } else {
-          logger.severe("Der Parameter " + IP_CTX + " muss angegeben werden.");
+          logger.log(Level.INFO, getRs(RB_PARAM_FEHLT), getRs(RB_AP_CTX));
         }
-        String conf = getInitParameter(IP_CONF);
+        String conf = getInitParameter(getRs(RB_AP_CONF));
         if(conf != null) {
           File confDir = new File(conf);
           confDir.mkdirs();
         } else {
-          
+          logger.log(Level.INFO, App.getRs(RB_PARAM_FEHLT), getRs(RB_AP_CONF));
         }
       } catch (IOException ex) {
-        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
+        logger.log(Level.SEVERE, null, ex);
       }
     } else {
-      logger.severe("Der Parameter " + IP_PORT + " muss angegeben werden.");
+      logger.log(Level.INFO, App.getRs(RB_PARAM_FEHLT), getRs(RB_AP_PORT));
     }
   }
 
@@ -101,5 +110,9 @@
     }
     return param;
   }
+  
+  public static String getRs(String key) {
+    return rb.getString(key);
+  }
 
 }
diff --git a/src/de/uhilger/mediaz/Server.java b/src/de/uhilger/mediaz/Server.java
index ebaf91d..2ec1044 100644
--- a/src/de/uhilger/mediaz/Server.java
+++ b/src/de/uhilger/mediaz/Server.java
@@ -18,12 +18,14 @@
 package de.uhilger.mediaz;
 
 import com.sun.net.httpserver.HttpServer;
-import de.uhilger.mediaz.handler.FileHandler;
-import de.uhilger.mediaz.handler.StopServerHandler;
+import de.uhilger.mediaz.api.FileHandler;
+import de.uhilger.mediaz.api.StopServerHandler;
 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;
 
 /**
  * Die Klasse Server stellt Methoden zur Ausf&uuml;hrung eines 
@@ -36,8 +38,10 @@
   
   private static final Logger logger = Logger.getLogger(Server.class.getName());
   
-  public static final String STR_SLASH = "/";
-  public static final String CMD_SERVER_STOP = "/server/stop";
+  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_SLASH = "slash";
   
   private int port;
   
@@ -66,8 +70,9 @@
    * @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
    */
   public void setContextName(String ctxName) {
-    if(!ctxName.startsWith(STR_SLASH)) {
-      this.ctx = STR_SLASH + ctxName;
+    String slash = App.getRs(RB_SLASH);
+    if(!ctxName.startsWith(slash)) {
+      this.ctx = slash + ctxName;
     } else {
       this.ctx = ctxName;
     }
@@ -81,11 +86,11 @@
    * in diesem Objekt 
    */
   public void start() throws IOException {
-    logger.info("Server starting on port " + port);
+    logger.log(Level.INFO, App.getRs(RB_SERVER_START_MSG), Integer.toString(port));
 
     HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
-    server.createContext(ctx + STR_SLASH, new FileHandler(App.getInitParameter(App.IP_WWW_DATA)));
-    server.createContext(ctx + CMD_SERVER_STOP, new StopServerHandler());
+    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.setExecutor(Executors.newFixedThreadPool(20));
     server.start();
   }
diff --git a/src/de/uhilger/mediaz/handler/FileHandler.java b/src/de/uhilger/mediaz/api/FileHandler.java
similarity index 92%
rename from src/de/uhilger/mediaz/handler/FileHandler.java
rename to src/de/uhilger/mediaz/api/FileHandler.java
index 4835e05..ab20120 100644
--- a/src/de/uhilger/mediaz/handler/FileHandler.java
+++ b/src/de/uhilger/mediaz/api/FileHandler.java
@@ -15,12 +15,14 @@
   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
-package de.uhilger.mediaz.handler;
+package de.uhilger.mediaz.api;
 
 import com.sun.net.httpserver.Headers;
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
+import de.uhilger.mediaz.App;
 import de.uhilger.mediaz.Server;
+import static de.uhilger.mediaz.Server.RB_SLASH;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -68,15 +70,17 @@
   public static final String HTTP_GET = "GET";
   
   /* String Konstanten */
-  public static final String STR_BYTES = "bytes";
   public static final String STR_BLANK = " ";
-  public static final String STR_DASH = "-";
   public static final String STR_COMMA = ",";
   public static final String STR_DOT = ".";
-  public static final String STR_NOT_FOUND = " not found.";
-  public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
-  public static final String RANGE_PATTERN = "[^\\d-,]";
-  public static final String WELCOME_FILE = "index.html";
+
+  /* ResourceBundle-Kennungen */
+  public static final String RB_BYTES = "bytes";
+  public static final String RB_DASH = "dash";
+  public static final String RB_NOT_FOUND = "notFound";
+  public static final String RB_LM_PATTERN = "lmPattern";
+  public static final String RB_RANGE_PATTERN = "rangePattern";
+  public static final String RB_WELCOME_FILE = "welcomeFile";
 
   /* Ablageort fuer Webinhalte */
   private final String fileBase;
@@ -111,8 +115,8 @@
       if (headers.containsKey(RANGE_HEADER)) {
         serveFileParts(e, new File(fileBase, fName));
       } else {
-        if (fName.length() < 1 || fName.endsWith(Server.STR_SLASH)) {
-          fName += WELCOME_FILE;
+        if (fName.length() < 1 || fName.endsWith(App.getRs(RB_SLASH))) {
+          fName += App.getRs(RB_WELCOME_FILE);
         }
         serveFile(e, new File(fileBase, fName));
       }
@@ -254,7 +258,7 @@
       Der regulaere Ausdruck "[^\\d-,]" bezeichnet alle Zeichen, die keine 
       Ziffern 0-9, Bindestrich oder Komma sind.
      */
-    rangeHeader = rangeHeader.replaceAll(RANGE_PATTERN, "");
+    rangeHeader = rangeHeader.replaceAll(App.getRs(RB_RANGE_PATTERN), "");
 
     /*
       Die Ranges ermitteln. 
@@ -277,7 +281,7 @@
     String[] rangeArray = rangeHeader.split(STR_COMMA);
     for (String rangeStr : rangeArray) {
       Range range = new Range();
-      String[] values = rangeStr.split(STR_DASH);
+      String[] values = rangeStr.split(App.getRs(RB_DASH));
       if (values.length < 2) {
         // Fall 3
         range.setStart(Long.parseLong(values[0]));
@@ -308,12 +312,12 @@
    */
   protected String contentRangeHdr(Range range, File file) {
     StringBuilder sb = new StringBuilder();
-    sb.append(STR_BYTES);
+    sb.append(App.getRs(RB_BYTES));
     sb.append(STR_BLANK);
     sb.append(range.getStart());
-    sb.append(STR_DASH);
+    sb.append(App.getRs(RB_DASH));
     sb.append(range.getEnd());
-    sb.append(Server.STR_SLASH);
+    sb.append(App.getRs(RB_SLASH));
     sb.append(file.length());
     return sb.toString();
   }
@@ -330,12 +334,12 @@
    */
   protected void setHeaders(HttpExchange e, File file) throws IOException {
     Headers resHeaders = e.getResponseHeaders();
-    resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES);
+    resHeaders.add(ACCEPT_RANGES_HEADER, App.getRs(RB_BYTES));
     String mimeType = Files.probeContentType(file.toPath());
     if (mimeType != null) {
       resHeaders.add(CONTENT_TYPE, mimeType);
     }
-    SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN);
+    SimpleDateFormat sdf = new SimpleDateFormat(App.getRs(RB_LM_PATTERN));
     Date date = new Date(file.lastModified());
     resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date));
   }
@@ -350,7 +354,7 @@
    */
   protected void sendNotFound(HttpExchange e, String fname) throws IOException {
     OutputStream os = e.getResponseBody();
-    String response = fname + STR_NOT_FOUND;
+    String response = fname + STR_BLANK + App.getRs(RB_NOT_FOUND);
     byte[] bytes = response.getBytes(StandardCharsets.UTF_8);
     e.sendResponseHeaders(SC_NOT_FOUND, bytes.length);
     os.write(bytes);
diff --git a/src/de/uhilger/mediaz/handler/StopServerHandler.java b/src/de/uhilger/mediaz/api/StopServerHandler.java
similarity index 79%
rename from src/de/uhilger/mediaz/handler/StopServerHandler.java
rename to src/de/uhilger/mediaz/api/StopServerHandler.java
index d910470..78826d7 100644
--- a/src/de/uhilger/mediaz/handler/StopServerHandler.java
+++ b/src/de/uhilger/mediaz/api/StopServerHandler.java
@@ -15,7 +15,7 @@
   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
-package de.uhilger.mediaz.handler;
+package de.uhilger.mediaz.api;
 
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
@@ -32,6 +32,14 @@
  * @author Ulrich Hilger
  */
 public class StopServerHandler implements HttpHandler {
+  
+  /* Der Logger fuer diesen StopServerHandler */
+  private static final Logger logger = Logger.getLogger(StopServerHandler.class.getName());
+  
+  /* ResourceBundle-Kennungen */
+  public static final String RB_STOPPING_SERVER = "stoppingServer";
+  public static final String RB_SERVER_STOPPED = "serverStopped";
+  public static final String RB_MEDIAZ_END = "mediazEnd";
 
   /**
    * Den Server geordnet herunterfahren und 
@@ -44,13 +52,13 @@
   @Override
   public void handle(HttpExchange e) throws IOException {
     Logger.getLogger(StopServerHandler.class.getName()).info(e.getRequestURI().toString());
-    String response = "Server stopped";
+    String response = App.getRs(RB_SERVER_STOPPED);
     e.sendResponseHeaders(200, response.length());
     OutputStream os = e.getResponseBody();
     os.write(response.getBytes());
     os.flush();
     os.close();
-    Logger.getLogger(StopServerHandler.class.getName()).info("stopping server.");
+    logger.info(App.getRs(RB_STOPPING_SERVER));
     e.getHttpContext().getServer().stop(1);
     Timer timer = new Timer();
     timer.schedule(new AppStopper(), 2000);
@@ -64,7 +72,7 @@
 
     @Override
     public void run() {
-      Logger.getLogger(StopServerHandler.class.getName()).info("Mediazentrale beendet.");
+      logger.info(App.getRs(RB_MEDIAZ_END));
       App.stop();
     }
   }
diff --git a/src/mediaz_de_DE.properties b/src/mediaz_de_DE.properties
new file mode 100644
index 0000000..11f491f
--- /dev/null
+++ b/src/mediaz_de_DE.properties
@@ -0,0 +1,25 @@
+
+# App-Parameter
+appParamPort=port
+appParamConf=conf
+appParamWWWData=www-data
+appParamCtx=ctx
+
+# API-Endpunkte
+webroot=/
+stopServer=/server/stop
+
+slash=/
+dash=-
+bytes=bytes
+notFound=not found.
+lmPattern=EEE, dd MMM yyyy HH:mm:ss zzz
+rangePattern=[^\\d-,]
+welcomeFile=index.html
+stoppingServer=stopping server.
+serverStopped=Server stopped.
+mediazEnd=Mediazentrale beendet.
+
+# Mitteilungen
+msgServerStart=Server starting on port {0}
+msgParamFehlt=Der Parameter {0} muss angegeben werden.

--
Gitblit v1.9.3