Top Description Inners Fields Constructors Methods
org.h2.server.web

public Class WebServer

extends Object
implements Service
Class Inheritance
All Implemented Interfaces
org.h2.server.Service
Imports
java.io.IOException, .InputStream, .OutputStream, java.net.ServerSocket, .Socket, java.nio.charset.StandardCharsets, java.nio.file.Files, .Path, .Paths, java.sql.Connection, .SQLException, java.time.ZoneId, .ZonedDateTime, java.time.format.DateTimeFormatter, java.util.ArrayList, .Arrays, .Collections, .HashMap, .HashSet, .Locale, .Map, .Map.Entry, .Properties, .Set, org.h2.engine.Constants, .SysProperties, org.h2.message.DbException, org.h2.security.SHA256, org.h2.server.Service, .ShutdownHandler, org.h2.store.fs.FileUtils, org.h2.util.JdbcUtils, .MathUtils, .NetUtils, .NetworkConnectionInfo, .SortedProperties, .StringUtils, .Tool, .Utils

The web server is a simple standalone HTTP server that implements the H2 Console application. It is not optimized for performance.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
WebServer.TranslateThread

The translate thread reads and writes the file translation.properties once a second.

Field Summary

Modifier and TypeField and Description
private byte[]
private boolean
private boolean
private boolean
private static final String
private String
private final HashMap<String, ConnectionInfo>
private static final String
private String
private static final String[]
private String
private boolean
private boolean
private String
pack-priv static final String[][]
private final HashSet<String>
private long
private Thread
private int
private final Set<WebThread>
private String
private ServerSocket
private static final long
SESSION_TIMEOUT

The session timeout (the default is 30 minutes).

private final HashMap<String, WebSession>
private ShutdownHandler
private boolean
private String
private static int
private boolean
private WebServer.TranslateThread
private String
pack-priv boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the URL of the web site to access this connection
addSession
(Connection
the connection
conn
)

Create a session with a given connection.

pack-priv boolean

Returns:

true if admin password not configure, or admin password correct
checkAdminPassword
(String
the password to test
password
)

Check the admin password.

pack-priv WebSession

Returns:

the web session object
createNewSession
(String
the host address
hostAddr
)

Create a new web session id and object.

public static String

Returns:

a salt and hash of salted password as a hex encoded string to be used in configuration file
encodeAdminPassword
(String
the password
password
)

Generates a random salt and returns it with a hash of specified password with this salt.

private static String
pack-priv byte[]
pack-priv boolean
public boolean
getAllowOthers()

Implements org.h2.server.Service.getAllowOthers.

Check if remote connections are allowed.

public ArrayList<String>
pack-priv Connection

Returns:

the database connection
getConnection
(String
the driver class name
driver
,
String
the database URL
databaseUrl
,
String
the user name
user
,
String
the password
password
,
String
the key of privileged user
userKey
,
NetworkConnectionInfo
the network connection information
networkConnectionInfo
)

Open a database connection.

pack-priv String
pack-priv byte[]

Returns:

the data
getFile
(String
the file name
file
)

Read the given file from the file system or from the resources.

public String

Returns:

host name
getHost
()

pack-priv String

Returns:

key key, or null
getKey
()

Returns the key for privileged connections.

public String
getName()

Implements org.h2.server.Service.getName.

Get the human readable name of the service.

public int
getPort()

Implements org.h2.server.Service.getPort.

Gets the port this service is listening on.

pack-priv WebSession

Returns:

the web session or null
getSession
(String
the session id
sessionId
)

Get the web session object for the given session id.

pack-priv ArrayList<HashMap<String, Object>>
pack-priv ConnectionInfo

Returns:

the connection information
getSetting
(String
the setting name
name
)

Get the connection information for this setting.

pack-priv String[]

Returns:

the connection info names
getSettingNames
()

Get the list of connection information setting names.

pack-priv synchronized ArrayList<ConnectionInfo>

Returns:

the list
getSettings
()

Get the list of connection info objects.

pack-priv boolean
pack-priv String
public String
getType()

Implements org.h2.server.Service.getType.

Get the human readable short name of the service.

public String
getURL()

Implements org.h2.server.Service.getURL.

Get the URL of this service in a human readable form

public void
init(String...
the command line options
args
)

Implements org.h2.server.Service.init.

Initialize the service from command line options.

public boolean
public boolean
isDaemon()

Implements org.h2.server.Service.isDaemon.

Check if a daemon thread should be used.

public boolean
isRunning(boolean
if errors should be written
traceError
)

Implements org.h2.server.Service.isRunning.

Check if the service is running.

public boolean
public void
listen()

Implements org.h2.server.Service.listen.

Listen for incoming connections.

private Properties
pack-priv void
readTranslations(WebSession
the session
session
,
String
the language
language
)

Read the translation for this language and save them in the 'text' property of this session.

pack-priv synchronized void
remove(WebThread
the thread to remove
t
)

Remove this web thread from the set of running threads.

pack-priv void
removeSetting(String
the setting to remove
name
)

Remove a connection information setting from the list

public void
saveCommandHistoryList(ArrayList<String>
the history
commandHistory
)

Save the command history to the properties file.

pack-priv synchronized void
saveProperties(Properties
null or the properties webPort, webAllowOthers, and webSSL
prop
)

Save the settings to the properties file.

pack-priv void
pack-priv void
setAllowChunked(boolean allowChunked)

pack-priv void
setAllowOthers(boolean b)

public void
setAllowSecureCreation(boolean
whether creation of databases using the key should be allowed
allowSecureCreation
)

public void
setCommandHistoryAllowed(boolean allowed)

pack-priv void
setExternalNames(String externalNames)

public void
setKey(String
key, or null
key
)

Sets the key for privileged connections.

pack-priv void
setPort(int port)

public void
pack-priv void
setSSL(boolean b)

pack-priv void
shutdown()

Shut down the web server.

public void
start()

Implements org.h2.server.Service.start.

Start the service.

pack-priv String

Returns:

the name of the file to translate
startTranslate
(Map<Object, Object>
the translation map
translation
)

Start the translation thread that reads the file once a second.

public void
stop()

Implements org.h2.server.Service.stop.

Stop the service.

pack-priv boolean

Returns:

true if a translation is available
supportsLanguage
(String
the language
language
)

Check if this language is supported / translated.

pack-priv void
trace(String
the message to write
s
)

Write trace information if trace is enabled.

pack-priv void
traceError(Throwable
the exception
e
)

Write the stack trace if trace is enabled.

pack-priv void
updateSetting(ConnectionInfo
the connection information
info
)

Update a connection information setting.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

adminPasswordback to summary
private byte[] adminPassword
allowChunkedback to summary
private boolean allowChunked
allowOthersback to summary
private boolean allowOthers
allowSecureCreationback to summary
private boolean allowSecureCreation
COMMAND_HISTORYback to summary
private static final String COMMAND_HISTORY
commandHistoryStringback to summary
private String commandHistoryString
connInfoMapback to summary
private final HashMap<String, ConnectionInfo> connInfoMap
DEFAULT_LANGUAGEback to summary
private static final String DEFAULT_LANGUAGE
externalNamesback to summary
private String externalNames
GENERICback to summary
private static final String[] GENERIC
hostback to summary
private String host
ifExistsback to summary
private boolean ifExists
isDaemonback to summary
private boolean isDaemon
keyback to summary
private String key
LANGUAGESback to summary
pack-priv static final String[][] LANGUAGES
languagesback to summary
private final HashSet<String> languages
lastTimeoutCheckback to summary
private long lastTimeoutCheck
listenerThreadback to summary
private Thread listenerThread
portback to summary
private int port
runningback to summary
private final Set<WebThread> running
serverPropertiesDirback to summary
private String serverPropertiesDir
serverSocketback to summary
private ServerSocket serverSocket
SESSION_TIMEOUTback to summary
private static final long SESSION_TIMEOUT

The session timeout (the default is 30 minutes).

sessionsback to summary
private final HashMap<String, WebSession> sessions
shutdownHandlerback to summary
private ShutdownHandler shutdownHandler
sslback to summary
private boolean ssl
startDateTimeback to summary
private String startDateTime
tickerback to summary
private static int ticker
traceback to summary
private boolean trace
translateThreadback to summary
private WebServer.TranslateThread translateThread
urlback to summary
private String url
virtualThreadsback to summary
pack-priv boolean virtualThreads

Constructor Detail

WebServerback to summary
public WebServer()

Method Detail

addSessionback to summary
public String addSession(Connection conn) throws SQLException

Create a session with a given connection.

Parameters
conn:Connection

the connection

Returns:String

the URL of the web site to access this connection

Exceptions
SQLException:
on failure
checkAdminPasswordback to summary
pack-priv boolean checkAdminPassword(String password)

Check the admin password.

Parameters
password:String

the password to test

Returns:boolean

true if admin password not configure, or admin password correct

createNewSessionback to summary
pack-priv WebSession createNewSession(String hostAddr)

Create a new web session id and object.

Parameters
hostAddr:String

the host address

Returns:WebSession

the web session object

encodeAdminPasswordback to summary
public static String encodeAdminPassword(String password)

Generates a random salt and returns it with a hash of specified password with this salt.

Parameters
password:String

the password

Returns:String

a salt and hash of salted password as a hex encoded string to be used in configuration file

Exceptions
IllegalArgumentException:
when password is too short
generateSessionIdback to summary
private static String generateSessionId()
getAdminPasswordback to summary
pack-priv byte[] getAdminPassword()
getAllowChunkedback to summary
pack-priv boolean getAllowChunked()
getAllowOthersback to summary
public boolean getAllowOthers()

Implements org.h2.server.Service.getAllowOthers.

Doc from org.h2.server.Service.getAllowOthers.

Check if remote connections are allowed.

Returns:boolean

true if remote connections are allowed

Annotations
@Override
getCommandHistoryListback to summary
public ArrayList<String> getCommandHistoryList()
getConnectionback to summary
pack-priv Connection getConnection(String driver, String databaseUrl, String user, String password, String userKey, NetworkConnectionInfo networkConnectionInfo) throws SQLException

Open a database connection.

Parameters
driver:String

the driver class name

databaseUrl:String

the database URL

user:String

the user name

password:String

the password

userKey:String

the key of privileged user

networkConnectionInfo:NetworkConnectionInfo

the network connection information

Returns:Connection

the database connection

Exceptions
SQLException:
on failure
getExternalNamesback to summary
pack-priv String getExternalNames()
getFileback to summary
pack-priv byte[] getFile(String file) throws IOException

Read the given file from the file system or from the resources.

Parameters
file:String

the file name

Returns:byte[]

the data

Exceptions
IOException:
on failure
getHostback to summary
public String getHost()
Returns:String

host name

getKeyback to summary
pack-priv String getKey()

Returns the key for privileged connections.

Returns:String

key key, or null

getNameback to summary
public String getName()

Implements org.h2.server.Service.getName.

Doc from org.h2.server.Service.getName.

Get the human readable name of the service.

Returns:String

the name

Annotations
@Override
getPortback to summary
public int getPort()

Implements org.h2.server.Service.getPort.

Doc from org.h2.server.Service.getPort.

Gets the port this service is listening on.

Returns:int

the port

Annotations
@Override
getSessionback to summary
pack-priv WebSession getSession(String sessionId)

Get the web session object for the given session id.

Parameters
sessionId:String

the session id

Returns:WebSession

the web session or null

getSessionsback to summary
pack-priv ArrayList<HashMap<String, Object>> getSessions()
getSettingback to summary
pack-priv ConnectionInfo getSetting(String name)

Get the connection information for this setting.

Parameters
name:String

the setting name

Returns:ConnectionInfo

the connection information

getSettingNamesback to summary
pack-priv String[] getSettingNames()

Get the list of connection information setting names.

Returns:String[]

the connection info names

getSettingsback to summary
pack-priv synchronized ArrayList<ConnectionInfo> getSettings()

Get the list of connection info objects.

Returns:ArrayList<ConnectionInfo>

the list

getSSLback to summary
pack-priv boolean getSSL()
getStartDateTimeback to summary
pack-priv String getStartDateTime()
getTypeback to summary
public String getType()

Implements org.h2.server.Service.getType.

Doc from org.h2.server.Service.getType.

Get the human readable short name of the service.

Returns:String

the type

Annotations
@Override
getURLback to summary
public String getURL()

Implements org.h2.server.Service.getURL.

Doc from org.h2.server.Service.getURL.

Get the URL of this service in a human readable form

Returns:String

the url

Annotations
@Override
initback to summary
public void init(String... args)

Implements org.h2.server.Service.init.

Doc from org.h2.server.Service.init.

Initialize the service from command line options.

Parameters
args:String[]

the command line options

Annotations
@Override
isCommandHistoryAllowedback to summary
public boolean isCommandHistoryAllowed()
isDaemonback to summary
public boolean isDaemon()

Implements org.h2.server.Service.isDaemon.

Doc from org.h2.server.Service.isDaemon.

Check if a daemon thread should be used.

Returns:boolean

true if a daemon thread should be used

Annotations
@Override
isRunningback to summary
public boolean isRunning(boolean traceError)

Implements org.h2.server.Service.isRunning.

Doc from org.h2.server.Service.isRunning.

Check if the service is running.

Parameters
traceError:boolean

if errors should be written

Returns:boolean

if the server is running

Annotations
@Override
isStoppedback to summary
public boolean isStopped()
listenback to summary
public void listen()

Implements org.h2.server.Service.listen.

Doc from org.h2.server.Service.listen.

Listen for incoming connections. This method blocks.

Annotations
@Override
loadPropertiesback to summary
private Properties loadProperties()
readTranslationsback to summary
pack-priv void readTranslations(WebSession session, String language)

Read the translation for this language and save them in the 'text' property of this session.

Parameters
session:WebSession

the session

language:String

the language

removeback to summary
pack-priv synchronized void remove(WebThread t)

Remove this web thread from the set of running threads.

Parameters
t:WebThread

the thread to remove

removeSettingback to summary
pack-priv void removeSetting(String name)

Remove a connection information setting from the list

Parameters
name:String

the setting to remove

saveCommandHistoryListback to summary
public void saveCommandHistoryList(ArrayList<String> commandHistory)

Save the command history to the properties file.

Parameters
commandHistory:ArrayList<String>

the history

savePropertiesback to summary
pack-priv synchronized void saveProperties(Properties prop)

Save the settings to the properties file.

Parameters
prop:Properties

null or the properties webPort, webAllowOthers, and webSSL

setAdminPasswordback to summary
pack-priv void setAdminPassword(String password)
setAllowChunkedback to summary
pack-priv void setAllowChunked(boolean allowChunked)
setAllowOthersback to summary
pack-priv void setAllowOthers(boolean b)
setAllowSecureCreationback to summary
public void setAllowSecureCreation(boolean allowSecureCreation)
Parameters
allowSecureCreation:boolean

whether creation of databases using the key should be allowed

setCommandHistoryAllowedback to summary
public void setCommandHistoryAllowed(boolean allowed)
setExternalNamesback to summary
pack-priv void setExternalNames(String externalNames)
setKeyback to summary
public void setKey(String key)

Sets the key for privileged connections.

Parameters
key:String

key, or null

setPortback to summary
pack-priv void setPort(int port)
setShutdownHandlerback to summary
public void setShutdownHandler(ShutdownHandler shutdownHandler)
setSSLback to summary
pack-priv void setSSL(boolean b)
shutdownback to summary
pack-priv void shutdown()

Shut down the web server.

startback to summary
public void start()

Implements org.h2.server.Service.start.

Doc from org.h2.server.Service.start.

Start the service. This usually means create the server socket. This method must not block.

Annotations
@Override
startTranslateback to summary
pack-priv String startTranslate(Map<Object, Object> translation)

Start the translation thread that reads the file once a second.

Parameters
translation:Map<Object, Object>

the translation map

Returns:String

the name of the file to translate

stopback to summary
public void stop()

Implements org.h2.server.Service.stop.

Doc from org.h2.server.Service.stop.

Stop the service.

Annotations
@Override
supportsLanguageback to summary
pack-priv boolean supportsLanguage(String language)

Check if this language is supported / translated.

Parameters
language:String

the language

Returns:boolean

true if a translation is available

traceback to summary
pack-priv void trace(String s)

Write trace information if trace is enabled.

Parameters
s:String

the message to write

traceErrorback to summary
pack-priv void traceError(Throwable e)

Write the stack trace if trace is enabled.

Parameters
e:Throwable

the exception

updateSettingback to summary
pack-priv void updateSetting(ConnectionInfo info)

Update a connection information setting.

Parameters
info:ConnectionInfo

the connection information

updateURLback to summary
private void updateURL()
org.h2.server.web back to summary

private Class WebServer.TranslateThread

extends Thread
Class Inheritance

The translate thread reads and writes the file translation.properties once a second.

Field Summary

Modifier and TypeField and Description
private final Path
private volatile boolean
private final Map<Object, Object>
Inherited from java.lang.Thread:
MAX_PRIORITYMIN_PRIORITYNORM_PRIORITY

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public String
public void
run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

This method is run by the thread when it executes.

public void
Inherited from java.lang.Thread:
activeCountcheckAccessclonecurrentThreaddumpStackenumerategetAllStackTracesgetContextClassLoadergetDefaultUncaughtExceptionHandlergetIdgetNamegetPrioritygetStackTracegetStategetThreadGroupgetUncaughtExceptionHandlerholdsLockinterruptinterruptedisAliveisDaemonisInterruptedisVirtualjoinjoinjoinjoinofPlatformofVirtualonSpinWaitsetContextClassLoadersetDaemonsetDefaultUncaughtExceptionHandlersetNamesetPrioritysetUncaughtExceptionHandlersleepsleepsleepstartstartVirtualThreadstopthreadIdtoStringyield

Field Detail

fileback to summary
private final Path file
stopNowback to summary
private volatile boolean stopNow
translationback to summary
private final Map<Object, Object> translation

Constructor Detail

TranslateThreadback to summary
pack-priv TranslateThread(Map<Object, Object> translation)

Method Detail

getFileNameback to summary
public String getFileName()
runback to summary
public void run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

Doc from java.lang.Thread.run.

This method is run by the thread when it executes. Subclasses of Thread may override this method.

This method is not intended to be invoked directly. If this thread is a platform thread created with a Runnable task then invoking this method will invoke the task's run method. If this thread is a virtual thread then invoking this method directly does nothing.

Annotations
@Override
stopNowback to summary
public void stopNow()