From 07967435cd21e0132d0eabf604c1f8cbea716e4f Mon Sep 17 00:00:00 2001
From: Timm Fitschen <timm.fitschen@ds.mpg.de>
Date: Wed, 10 Apr 2019 01:44:49 +0200
Subject: [PATCH] MAINT: refactored property name

* SERVER_PORT_HTTPS_EXTERNAL -> REDIRECT_HTTP_TO_HTTPS_PORT
* added loggers to some classes and removed system.out.print statements
* added some doc strings.
* updated .gitlab-ci.yml to use java 10 now
---
 .gitlab-ci.yml                                | 10 +++---
 src/main/java/caosdb/server/CaosDBServer.java | 35 ++++++++++---------
 .../caosdb/server/HttpToHttpsRedirector.java  | 18 +++++-----
 .../java/caosdb/server/ServerProperties.java  |  2 +-
 4 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index e078dfd4..553f92aa 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -33,7 +33,7 @@ stages:
 
 # delete local maven repo to be sure all packages are still maintained by the maven repos
 setup:mvn:
-    tags: [ java8 ]
+    tags: [ java10 ]
     stage: setup
     script:
         - make easy-units
@@ -46,7 +46,7 @@ setup:mvn:
 
 # generate parsing classes with antlr
 code-generation:antlr:
-    tags: [ java8 ]
+    tags: [ java10 ]
     stage: code-generation
     artifacts:
         paths:
@@ -60,7 +60,7 @@ code-generation:antlr:
 
 # compile the server
 build:server:
-    tags: [ java8 ]
+    tags: [ java10 ]
     stage: build
     dependencies:
         - code-generation:antlr
@@ -76,7 +76,7 @@ build:server:
 
 # junit tests for the server
 test:server:
-    tags: [ java8 ]
+    tags: [ java10 ]
     stage: test
     dependencies:
         - build:server
@@ -88,7 +88,7 @@ test:server:
 # Clean
 ###########
 clean:mvn:
-    tags: [ java8 ]
+    tags: [ java10 ]
     stage: clean
     when: always
     dependencies: []
diff --git a/src/main/java/caosdb/server/CaosDBServer.java b/src/main/java/caosdb/server/CaosDBServer.java
index 082b2d2b..2e726217 100644
--- a/src/main/java/caosdb/server/CaosDBServer.java
+++ b/src/main/java/caosdb/server/CaosDBServer.java
@@ -239,8 +239,7 @@ public class CaosDBServer extends Application {
       initServerProperties();
       initTimeZone();
     } catch (IOException | InterruptedException e1) {
-      e1.printStackTrace();
-      System.err.println("Could not configure the server.");
+      logger.error("Could not configure the server.", e1);
       System.exit(1);
     }
 
@@ -289,7 +288,7 @@ public class CaosDBServer extends Application {
         // ChecksumUpdater
         ChecksumUpdater.start();
       } else {
-        System.err.println("NO BACKEND");
+        logger.info("NO BACKEND");
       }
 
       // GUI
@@ -313,7 +312,7 @@ public class CaosDBServer extends Application {
         // add Benchmark
         StatsPanel.addStat("TransactionBenchmark", TransactionBenchmark.getInstance());
       } else {
-        System.err.println("NO GUI");
+        logger.info("NO GUI");
         System.setOut(new NullPrintStream());
       }
 
@@ -322,12 +321,12 @@ public class CaosDBServer extends Application {
           Integer.parseInt(getServerProperty(ServerProperties.KEY_SERVER_PORT_HTTPS));
       final int port_http =
           Integer.parseInt(getServerProperty(ServerProperties.KEY_SERVER_PORT_HTTP));
-      int port_https_ext;
+      int port_redirect_https;
       try {
-        port_https_ext =
-          Integer.parseInt(getServerProperty(ServerProperties.KEY_SERVER_PORT_HTTPS_EXTERNAL));
+        port_redirect_https =
+            Integer.parseInt(getServerProperty(ServerProperties.KEY_REDIRECT_HTTP_TO_HTTPS_PORT));
       } catch (NumberFormatException e) {
-        port_https_ext = port_https;
+        port_redirect_https = port_https;
       }
       final int initialConnections =
           Integer.parseInt(getServerProperty(ServerProperties.KEY_INITIAL_CONNECTIONS));
@@ -339,12 +338,12 @@ public class CaosDBServer extends Application {
       if (INSECURE) {
         runHTTPServer(port_http, initialConnections, maxTotalConnections);
       } else {
-          runHTTPSServer(port_https, port_http, port_https_ext,
-                         initialConnections, maxTotalConnections);
+        runHTTPSServer(
+            port_https, port_http, port_redirect_https, initialConnections, maxTotalConnections);
       }
       initShutDownHook();
     } catch (final Exception e) {
-      e.printStackTrace();
+      logger.error("Server start failed.", e);
       init.release();
       System.exit(1);
     }
@@ -408,16 +407,20 @@ public class CaosDBServer extends Application {
   }
 
   /**
-   * Starts a https server running on the specified port.
+   * Starts a https server running on the specified `port_https`, listening also for http
+   * connections on `port_http` and redirect any http connections to `port_redirect_https`.
    *
    * @author Timm Fitschen
-   * @param port The port on which this server should run.
+   * @param port_https Listen on this port for https connections.
+   * @param port_http Listen on this port for http connections and send http-to-https redirect with
+   *     different port.
+   * @parem port_redirect_https Redirect any http connections to this port.
    * @throws Exception if problems occur starting up this server.
    */
   private static void runHTTPSServer(
       final int port_https,
       final int port_http,
-      final int port_https_ext,
+      final int port_redirect_https,
       final int initialConnections,
       final int maxTotalConnections)
       throws Exception {
@@ -439,11 +442,11 @@ public class CaosDBServer extends Application {
 
     // redirector http to https
     if (port_http != 0) {
-      System.out.println("Redirecting to " + port_https_ext);
+      logger.info("Redirecting to " + port_redirect_https);
       component
           .getServers()
           .add(Protocol.HTTP, port_http)
-          .setNext(new HttpToHttpsRedirector(port_https_ext));
+          .setNext(new HttpToHttpsRedirector(port_redirect_https));
     }
 
     // set initial and maximal connections
diff --git a/src/main/java/caosdb/server/HttpToHttpsRedirector.java b/src/main/java/caosdb/server/HttpToHttpsRedirector.java
index 4f5c44b1..6e250028 100644
--- a/src/main/java/caosdb/server/HttpToHttpsRedirector.java
+++ b/src/main/java/caosdb/server/HttpToHttpsRedirector.java
@@ -26,18 +26,19 @@ import org.restlet.Request;
 import org.restlet.Response;
 import org.restlet.Restlet;
 import org.restlet.data.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-/**
- * Sends a permanent redirect response.
- */
+/** Sends a permanent redirect response. */
 public class HttpToHttpsRedirector extends Restlet {
 
+  public Logger logger = LoggerFactory.getLogger(getClass());
   private final int port;
 
- /**
-  * @author Timm Fitschen
-  * @param httpsPort The port to which the redirect should point.
-  */
+  /**
+   * @author Timm Fitschen
+   * @param httpsPort The port to which the redirect should point.
+   */
   public HttpToHttpsRedirector(final int httpsPort) {
     this.port = httpsPort;
   }
@@ -48,7 +49,6 @@ public class HttpToHttpsRedirector extends Restlet {
     reference.setScheme("https");
     reference.setHostPort(this.port);
     response.redirectPermanent(reference);
-    System.err.println(
-        "Redirected " + request.getOriginalRef().toString() + " to " + reference.toString());
+    logger.info("Redirected {} to {}", request.getOriginalRef(), reference);
   }
 }
diff --git a/src/main/java/caosdb/server/ServerProperties.java b/src/main/java/caosdb/server/ServerProperties.java
index 9673eff2..10ede003 100644
--- a/src/main/java/caosdb/server/ServerProperties.java
+++ b/src/main/java/caosdb/server/ServerProperties.java
@@ -61,7 +61,7 @@ public class ServerProperties extends Properties {
 
   public static final String KEY_SERVER_PORT_HTTPS = "SERVER_PORT_HTTPS";
   public static final String KEY_SERVER_PORT_HTTP = "SERVER_PORT_HTTP";
-  public static final String KEY_SERVER_PORT_HTTPS_EXTERNAL = "SERVER_PORT_HTTPS_EXTERNAL";
+  public static final String KEY_REDIRECT_HTTP_TO_HTTPS_PORT = "REDIRECT_HTTP_TO_HTTPS_PORT";
 
   public static final String KEY_HTTPS_ENABLED_PROTOCOLS = "HTTPS_ENABLED_PROTOCOLS";
   public static final String KEY_HTTPS_DISABLED_PROTOCOLS = "HTTPS_DISABLED_PROTOCOLS";
-- 
GitLab