Class RestServer


  • public final class RestServer
    extends java.lang.Object
    Small RESTful server helper class.

    The Javalin framework is being used internally: https://javalin.io/ The primary purposes of this utility class is to provide

    • some convenience methods, default configuration (in particular relating to SSL and HTTP/2), and
    • to wrap the primary REST server implementation in view of back-end server upgrades or changing API.
    • to provide every GET route also with an long-polling and SSE listener/data-retrieval management.

    Server parameter can be controlled via the following system properties:

    • restServerHostName: host name or IP address the server should bind to
    • restServerPort: the HTTP port
    • restServerPort2: the HTTP/2 port (encrypted)
    • restKeyStore: the path to the file containing the key store for the encryption
    • restKeyStorePassword: the path to the file containing the key store for the encryption
    • restUserPasswordStore: the path to the file containing the user passwords and roles encryption
    some design choices: minimise exposing Javalin API outside this class, no usage of UI specific classes (ie. JavaFX)
    Author:
    rstein
    • Field Detail

      • TAG_REST_SERVER_HOST_NAME

        public static final java.lang.String TAG_REST_SERVER_HOST_NAME
        See Also:
        Constant Field Values
      • TAG_REST_SERVER_PORT

        public static final java.lang.String TAG_REST_SERVER_PORT
        See Also:
        Constant Field Values
      • TAG_REST_SERVER_PORT2

        public static final java.lang.String TAG_REST_SERVER_PORT2
        See Also:
        Constant Field Values
    • Method Detail

      • addLongPollingCookie

        public static void addLongPollingCookie​(io.javalin.http.Context ctx,
                                                java.lang.String key,
                                                long lastUpdateMillies)
      • applyRateLimit

        public static void applyRateLimit​(io.javalin.http.Context ctx,
                                          int numRequests,
                                          java.util.concurrent.TimeUnit timeUnit)
        guards this end point and returns HTTP error response if predefined rate limit is exceeded
        Parameters:
        ctx - end point context handler
        numRequests - number of calls
        timeUnit - time base reference
      • getDefaultRole

        public static java.util.Set<io.javalin.core.security.Role> getDefaultRole()
      • getEndpoints

        public static javafx.collections.ObservableList<io.javalin.core.event.HandlerMetaInfo> getEndpoints()
      • getEventClients

        public static java.util.Queue<io.javalin.http.sse.SseClient> getEventClients​(@NotNull
                                                                                     java.lang.String endpointName)
      • getHostName

        public static java.lang.String getHostName()
      • getHostPort

        public static int getHostPort()
      • getHostPort2

        public static int getHostPort2()
      • getInstance

        public static io.javalin.Javalin getInstance()
      • getRequestedProtocol

        public static java.lang.String getRequestedProtocol​(io.javalin.http.Context ctx,
                                                            java.lang.String... defaultProtocol)
      • getRequestedMimeProtocol

        public static de.gsi.dataset.remote.MimeType getRequestedMimeProtocol​(io.javalin.http.Context ctx,
                                                                              de.gsi.dataset.remote.MimeType... defaultProtocol)
      • getLocalURI

        public static java.net.URI getLocalURI()
      • getPublicURI

        public static java.net.URI getPublicURI()
      • getSessionCurrentRoles

        public static java.util.Set<io.javalin.core.security.Role> getSessionCurrentRoles​(io.javalin.http.Context ctx)
      • getSessionCurrentUser

        public static java.lang.String getSessionCurrentUser​(io.javalin.http.Context ctx)
      • getSessionLocale

        public static java.lang.String getSessionLocale​(io.javalin.http.Context ctx)
      • prefixPath

        public static java.lang.String prefixPath​(@NotNull
                                                  java.lang.String path)
      • setUserHandler

        public static void setUserHandler​(RestUserHandler newUserHandler)
        Sets a new user handler. N.B: This will issue a warning to remind system admins or security-minded people that the default implementation may have been replaced with a better/worse/different implementation (e.g. based on LDAP or another data base)
        Parameters:
        newUserHandler - the new implementation
      • startRestServer

        public static void startRestServer()
      • startRestServer

        public static void startRestServer​(int hostPort,
                                           int hostPort2)
      • startRestServer

        public static void startRestServer​(java.lang.String hostName,
                                           int hostPort,
                                           int hostPort2)
      • stopRestServer

        public static void stopRestServer()
      • suppressCaching

        public static void suppressCaching​(io.javalin.http.Context ctx)
        Suppresses caching for this end point
        Parameters:
        ctx - end point context handler
      • writeBytesToContext

        public static void writeBytesToContext​(@NotNull
                                               io.javalin.http.Context ctx,
                                               byte[] bytes,
                                               int nSize)
      • getDefaultProtocol

        public static de.gsi.dataset.remote.MimeType getDefaultProtocol()
      • setDefaultProtocol

        public static void setDefaultProtocol​(de.gsi.dataset.remote.MimeType defaultProtocol)