Class WebServer

java.lang.Object
org.h2.server.web.WebServer
All Implemented Interfaces:
Service

public class WebServer extends Object implements Service
The web server is a simple standalone HTTP server that implements the H2 Console application. It is not optimized for performance.
  • Field Details

    • LANGUAGES

      static final String[][] LANGUAGES
    • COMMAND_HISTORY

      private static final String COMMAND_HISTORY
      See Also:
    • DEFAULT_LANGUAGE

      private static final String DEFAULT_LANGUAGE
      See Also:
    • GENERIC

      private static final 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 String externalNames
    • isDaemon

      private boolean isDaemon
    • running

      private final Set<WebThread> running
    • ssl

      private boolean ssl
    • adminPassword

      private byte[] adminPassword
    • connInfoMap

      private final HashMap<String,ConnectionInfo> connInfoMap
    • lastTimeoutCheck

      private long lastTimeoutCheck
    • sessions

      private final HashMap<String,WebSession> sessions
    • languages

      private final HashSet<String> languages
    • startDateTime

      private String startDateTime
    • serverSocket

      private ServerSocket serverSocket
    • host

      private String host
    • url

      private String url
    • shutdownHandler

      private ShutdownHandler shutdownHandler
    • listenerThread

      private Thread listenerThread
    • ifExists

      private boolean ifExists
    • key

      private String key
    • allowSecureCreation

      private boolean allowSecureCreation
    • trace

      private boolean trace
    • translateThread

      private WebServer.TranslateThread translateThread
    • allowChunked

      private boolean allowChunked
    • serverPropertiesDir

      private String serverPropertiesDir
    • commandHistoryString

      private String commandHistoryString
  • Constructor Details

    • WebServer

      public WebServer()
  • Method Details

    • getFile

      byte[] getFile(String file) throws IOException
      Read the given file from the file system or from the resources.
      Parameters:
      file - the file name
      Returns:
      the data
      Throws:
      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 String generateSessionId()
    • getSession

      WebSession getSession(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(String hostAddr)
      Create a new web session id and object.
      Parameters:
      hostAddr - the host address
      Returns:
      the web session object
    • getStartDateTime

      String getStartDateTime()
    • getKey

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

      public void setKey(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(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 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 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(String s)
      Write trace information if trace is enabled.
      Parameters:
      s - the message to write
    • traceError

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

      boolean supportsLanguage(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, 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

      ArrayList<HashMap<String,Object>> getSessions()
    • getType

      public 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 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(String externalNames)
    • getExternalNames

      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 ArrayList<String> getCommandHistoryList()
    • saveCommandHistoryList

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

      ConnectionInfo getSetting(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(String name)
      Remove a connection information setting from the list
      Parameters:
      name - the setting to remove
    • loadProperties

      private Properties loadProperties()
    • getSettingNames

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

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

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

      Connection getConnection(String driver, String databaseUrl, String user, String password, String userKey, NetworkConnectionInfo networkConnectionInfo) throws 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:
      SQLException - on failure
    • shutdown

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

      public void setShutdownHandler(ShutdownHandler shutdownHandler)
    • addSession

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

      String startTranslate(Map<Object,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(String password)
    • checkAdminPassword

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