Class WebServer

  • All Implemented Interfaces:
    Service

    public class WebServer
    extends java.lang.Object
    implements Service
    The web server is a simple standalone HTTP server that implements the H2 Console application. It is not optimized for performance.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private class  WebServer.TranslateThread
      The translate thread reads and writes the file translation.properties once a second.
    • Constructor Summary

      Constructors 
      Constructor Description
      WebServer()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String addSession​(java.sql.Connection conn)
      Create a session with a given connection.
      (package private) boolean checkAdminPassword​(java.lang.String password)
      Check the admin password.
      (package private) WebSession createNewSession​(java.lang.String hostAddr)
      Create a new web session id and object.
      private static java.lang.String generateSessionId()  
      (package private) byte[] getAdminPassword()  
      (package private) boolean getAllowChunked()  
      boolean getAllowOthers()
      Check if remote connections are allowed.
      java.util.ArrayList<java.lang.String> getCommandHistoryList()  
      (package private) java.sql.Connection getConnection​(java.lang.String driver, java.lang.String databaseUrl, java.lang.String user, java.lang.String password, java.lang.String userKey, NetworkConnectionInfo networkConnectionInfo)
      Open a database connection.
      (package private) java.lang.String getExternalNames()  
      (package private) byte[] getFile​(java.lang.String file)
      Read the given file from the file system or from the resources.
      java.lang.String getHost()  
      (package private) java.lang.String getKey()
      Returns the key for privileged connections.
      java.lang.String getName()
      Get the human readable name of the service.
      int getPort()
      Gets the port this service is listening on.
      (package private) WebSession getSession​(java.lang.String sessionId)
      Get the web session object for the given session id.
      (package private) java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> getSessions()  
      (package private) ConnectionInfo getSetting​(java.lang.String name)
      Get the connection information for this setting.
      (package private) java.lang.String[] getSettingNames()
      Get the list of connection information setting names.
      (package private) java.util.ArrayList<ConnectionInfo> getSettings()
      Get the list of connection info objects.
      (package private) boolean getSSL()  
      (package private) java.lang.String getStartDateTime()  
      java.lang.String getType()
      Get the human readable short name of the service.
      java.lang.String getURL()
      Get the URL of this service in a human readable form
      void init​(java.lang.String... args)
      Initialize the service from command line options.
      boolean isCommandHistoryAllowed()  
      boolean isDaemon()
      Check if a daemon thread should be used.
      boolean isRunning​(boolean traceError)
      Check if the service is running.
      boolean isStopped()  
      void listen()
      Listen for incoming connections.
      private java.util.Properties loadProperties()  
      (package private) void readTranslations​(WebSession session, java.lang.String language)
      Read the translation for this language and save them in the 'text' property of this session.
      (package private) void remove​(WebThread t)
      Remove this web thread from the set of running threads.
      (package private) void removeSetting​(java.lang.String name)
      Remove a connection information setting from the list
      void saveCommandHistoryList​(java.util.ArrayList<java.lang.String> commandHistory)
      Save the command history to the properties file.
      (package private) void saveProperties​(java.util.Properties prop)
      Save the settings to the properties file.
      (package private) void setAdminPassword​(java.lang.String password)  
      (package private) void setAllowChunked​(boolean allowChunked)  
      (package private) void setAllowOthers​(boolean b)  
      void setAllowSecureCreation​(boolean allowSecureCreation)  
      void setCommandHistoryAllowed​(boolean allowed)  
      (package private) void setExternalNames​(java.lang.String externalNames)  
      void setKey​(java.lang.String key)
      Sets the key for privileged connections.
      (package private) void setPort​(int port)  
      void setShutdownHandler​(ShutdownHandler shutdownHandler)  
      (package private) void setSSL​(boolean b)  
      (package private) void shutdown()
      Shut down the web server.
      void start()
      Start the service.
      (package private) java.lang.String startTranslate​(java.util.Map<java.lang.Object,​java.lang.Object> translation)
      Start the translation thread that reads the file once a second.
      void stop()
      Stop the service.
      (package private) boolean supportsLanguage​(java.lang.String language)
      Check if this language is supported / translated.
      (package private) void trace​(java.lang.String s)
      Write trace information if trace is enabled.
      (package private) void traceError​(java.lang.Throwable e)
      Write the stack trace if trace is enabled.
      (package private) void updateSetting​(ConnectionInfo info)
      Update a connection information setting.
      private void updateURL()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LANGUAGES

        static final java.lang.String[][] LANGUAGES
      • DEFAULT_LANGUAGE

        private static final java.lang.String DEFAULT_LANGUAGE
        See Also:
        Constant Field Values
      • GENERIC

        private static final java.lang.String[] GENERIC
      • ticker

        private static int ticker
      • SESSION_TIMEOUT

        private static final long SESSION_TIMEOUT
        The session timeout (the default is 30 minutes).
      • port

        private int port
      • allowOthers

        private boolean allowOthers
      • externalNames

        private java.lang.String externalNames
      • isDaemon

        private boolean isDaemon
      • running

        private final java.util.Set<WebThread> running
      • ssl

        private boolean ssl
      • adminPassword

        private byte[] adminPassword
      • connInfoMap

        private final java.util.HashMap<java.lang.String,​ConnectionInfo> connInfoMap
      • lastTimeoutCheck

        private long lastTimeoutCheck
      • sessions

        private final java.util.HashMap<java.lang.String,​WebSession> sessions
      • languages

        private final java.util.HashSet<java.lang.String> languages
      • startDateTime

        private java.lang.String startDateTime
      • serverSocket

        private java.net.ServerSocket serverSocket
      • host

        private java.lang.String host
      • url

        private java.lang.String url
      • listenerThread

        private java.lang.Thread listenerThread
      • ifExists

        private boolean ifExists
      • key

        private java.lang.String key
      • allowSecureCreation

        private boolean allowSecureCreation
      • trace

        private boolean trace
      • allowChunked

        private boolean allowChunked
      • serverPropertiesDir

        private java.lang.String serverPropertiesDir
      • commandHistoryString

        private java.lang.String commandHistoryString
    • Constructor Detail

      • WebServer

        public WebServer()
    • Method Detail

      • getFile

        byte[] getFile​(java.lang.String file)
                throws java.io.IOException
        Read the given file from the file system or from the resources.
        Parameters:
        file - the file name
        Returns:
        the data
        Throws:
        java.io.IOException - on failure
      • remove

        void remove​(WebThread t)
        Remove this web thread from the set of running threads.
        Parameters:
        t - the thread to remove
      • generateSessionId

        private static java.lang.String generateSessionId()
      • getSession

        WebSession getSession​(java.lang.String sessionId)
        Get the web session object for the given session id.
        Parameters:
        sessionId - the session id
        Returns:
        the web session or null
      • createNewSession

        WebSession createNewSession​(java.lang.String hostAddr)
        Create a new web session id and object.
        Parameters:
        hostAddr - the host address
        Returns:
        the web session object
      • getStartDateTime

        java.lang.String getStartDateTime()
      • getKey

        java.lang.String getKey()
        Returns the key for privileged connections.
        Returns:
        key key, or null
      • setKey

        public void setKey​(java.lang.String key)
        Sets the key for privileged connections.
        Parameters:
        key - key, or null
      • setAllowSecureCreation

        public void setAllowSecureCreation​(boolean allowSecureCreation)
        Parameters:
        allowSecureCreation - whether creation of databases using the key should be allowed
      • init

        public void init​(java.lang.String... args)
        Description copied from interface: Service
        Initialize the service from command line options.
        Specified by:
        init in interface Service
        Parameters:
        args - the command line options
      • getURL

        public java.lang.String getURL()
        Description copied from interface: Service
        Get the URL of this service in a human readable form
        Specified by:
        getURL in interface Service
        Returns:
        the url
      • getHost

        public java.lang.String getHost()
        Returns:
        host name
      • updateURL

        private void updateURL()
      • start

        public void start()
        Description copied from interface: Service
        Start the service. This usually means create the server socket. This method must not block.
        Specified by:
        start in interface Service
      • listen

        public void listen()
        Description copied from interface: Service
        Listen for incoming connections. This method blocks.
        Specified by:
        listen in interface Service
      • isRunning

        public boolean isRunning​(boolean traceError)
        Description copied from interface: Service
        Check if the service is running.
        Specified by:
        isRunning in interface Service
        Parameters:
        traceError - if errors should be written
        Returns:
        if the server is running
      • isStopped

        public boolean isStopped()
      • stop

        public void stop()
        Description copied from interface: Service
        Stop the service.
        Specified by:
        stop in interface Service
      • trace

        void trace​(java.lang.String s)
        Write trace information if trace is enabled.
        Parameters:
        s - the message to write
      • traceError

        void traceError​(java.lang.Throwable e)
        Write the stack trace if trace is enabled.
        Parameters:
        e - the exception
      • supportsLanguage

        boolean supportsLanguage​(java.lang.String language)
        Check if this language is supported / translated.
        Parameters:
        language - the language
        Returns:
        true if a translation is available
      • readTranslations

        void readTranslations​(WebSession session,
                              java.lang.String language)
        Read the translation for this language and save them in the 'text' property of this session.
        Parameters:
        session - the session
        language - the language
      • getSessions

        java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> getSessions()
      • getType

        public java.lang.String getType()
        Description copied from interface: Service
        Get the human readable short name of the service.
        Specified by:
        getType in interface Service
        Returns:
        the type
      • getName

        public java.lang.String getName()
        Description copied from interface: Service
        Get the human readable name of the service.
        Specified by:
        getName in interface Service
        Returns:
        the name
      • setAllowOthers

        void setAllowOthers​(boolean b)
      • getAllowOthers

        public boolean getAllowOthers()
        Description copied from interface: Service
        Check if remote connections are allowed.
        Specified by:
        getAllowOthers in interface Service
        Returns:
        true if remote connections are allowed
      • setExternalNames

        void setExternalNames​(java.lang.String externalNames)
      • getExternalNames

        java.lang.String getExternalNames()
      • setSSL

        void setSSL​(boolean b)
      • setPort

        void setPort​(int port)
      • getSSL

        boolean getSSL()
      • getPort

        public int getPort()
        Description copied from interface: Service
        Gets the port this service is listening on.
        Specified by:
        getPort in interface Service
        Returns:
        the port
      • isCommandHistoryAllowed

        public boolean isCommandHistoryAllowed()
      • setCommandHistoryAllowed

        public void setCommandHistoryAllowed​(boolean allowed)
      • getCommandHistoryList

        public java.util.ArrayList<java.lang.String> getCommandHistoryList()
      • saveCommandHistoryList

        public void saveCommandHistoryList​(java.util.ArrayList<java.lang.String> commandHistory)
        Save the command history to the properties file.
        Parameters:
        commandHistory - the history
      • getSetting

        ConnectionInfo getSetting​(java.lang.String name)
        Get the connection information for this setting.
        Parameters:
        name - the setting name
        Returns:
        the connection information
      • updateSetting

        void updateSetting​(ConnectionInfo info)
        Update a connection information setting.
        Parameters:
        info - the connection information
      • removeSetting

        void removeSetting​(java.lang.String name)
        Remove a connection information setting from the list
        Parameters:
        name - the setting to remove
      • loadProperties

        private java.util.Properties loadProperties()
      • getSettingNames

        java.lang.String[] getSettingNames()
        Get the list of connection information setting names.
        Returns:
        the connection info names
      • getSettings

        java.util.ArrayList<ConnectionInfo> getSettings()
        Get the list of connection info objects.
        Returns:
        the list
      • saveProperties

        void saveProperties​(java.util.Properties prop)
        Save the settings to the properties file.
        Parameters:
        prop - null or the properties webPort, webAllowOthers, and webSSL
      • getConnection

        java.sql.Connection getConnection​(java.lang.String driver,
                                          java.lang.String databaseUrl,
                                          java.lang.String user,
                                          java.lang.String password,
                                          java.lang.String userKey,
                                          NetworkConnectionInfo networkConnectionInfo)
                                   throws java.sql.SQLException
        Open a database connection.
        Parameters:
        driver - the driver class name
        databaseUrl - the database URL
        user - the user name
        password - the password
        userKey - the key of privileged user
        networkConnectionInfo - the network connection information
        Returns:
        the database connection
        Throws:
        java.sql.SQLException - on failure
      • shutdown

        void shutdown()
        Shut down the web server.
      • setShutdownHandler

        public void setShutdownHandler​(ShutdownHandler shutdownHandler)
      • addSession

        public java.lang.String addSession​(java.sql.Connection conn)
                                    throws java.sql.SQLException
        Create a session with a given connection.
        Parameters:
        conn - the connection
        Returns:
        the URL of the web site to access this connection
        Throws:
        java.sql.SQLException - on failure
      • startTranslate

        java.lang.String startTranslate​(java.util.Map<java.lang.Object,​java.lang.Object> translation)
        Start the translation thread that reads the file once a second.
        Parameters:
        translation - the translation map
        Returns:
        the name of the file to translate
      • isDaemon

        public boolean isDaemon()
        Description copied from interface: Service
        Check if a daemon thread should be used.
        Specified by:
        isDaemon in interface Service
        Returns:
        true if a daemon thread should be used
      • setAllowChunked

        void setAllowChunked​(boolean allowChunked)
      • getAllowChunked

        boolean getAllowChunked()
      • getAdminPassword

        byte[] getAdminPassword()
      • setAdminPassword

        void setAdminPassword​(java.lang.String password)
      • checkAdminPassword

        boolean checkAdminPassword​(java.lang.String password)
        Check the admin password.
        Parameters:
        password - the password to test
        Returns:
        true if admin password not configure, or admin password correct