| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.server.web; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.DatabaseMetaData; |
| 10 |
import java.sql.ResultSet; |
| 11 |
import java.sql.SQLException; |
| 12 |
import java.sql.Statement; |
| 13 |
import java.sql.Timestamp; |
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.HashMap; |
| 16 |
import java.util.Locale; |
| 17 |
|
| 18 |
import org.h2.bnf.Bnf; |
| 19 |
import org.h2.bnf.context.DbContents; |
| 20 |
import org.h2.bnf.context.DbContextRule; |
| 21 |
import org.h2.message.DbException; |
| 22 |
|
| 23 |
/** |
| 24 |
* The web session keeps all data of a user session. |
| 25 |
* This class is used by the H2 Console. |
| 26 |
*/ |
| 27 |
class WebSession { |
| 28 |
|
| 29 |
private static final int MAX_HISTORY = 1000; |
| 30 |
|
| 31 |
/** |
| 32 |
* The last time this client sent a request. |
| 33 |
*/ |
| 34 |
long lastAccess; |
| 35 |
|
| 36 |
/** |
| 37 |
* The session attribute map. |
| 38 |
*/ |
| 39 |
final HashMap<String, Object> map = new HashMap<>(); |
| 40 |
|
| 41 |
/** |
| 42 |
* The current locale. |
| 43 |
*/ |
| 44 |
Locale locale; |
| 45 |
|
| 46 |
/** |
| 47 |
* The currently executing statement. |
| 48 |
*/ |
| 49 |
Statement executingStatement; |
| 50 |
|
| 51 |
/** |
| 52 |
* The current updatable result set. |
| 53 |
*/ |
| 54 |
ResultSet result; |
| 55 |
|
| 56 |
private final WebServer server; |
| 57 |
|
| 58 |
private final ArrayList<String> commandHistory; |
| 59 |
|
| 60 |
private Connection conn; |
| 61 |
private DatabaseMetaData meta; |
| 62 |
private DbContents contents = new DbContents(); |
| 63 |
private Bnf bnf; |
| 64 |
private boolean shutdownServerOnDisconnect; |
| 65 |
|
| 66 |
WebSession(WebServer server) { |
| 67 |
this.server = server; |
| 68 |
// This must be stored in the session rather than in the server. |
| 69 |
// Otherwise, one client could allow |
| 70 |
// saving history for others (insecure). |
| 71 |
this.commandHistory = server.getCommandHistoryList(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Put an attribute value in the map. |
| 76 |
* |
| 77 |
* @param key the key |
| 78 |
* @param value the new value |
| 79 |
*/ |
| 80 |
void put(String key, Object value) { |
| 81 |
map.put(key, value); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the value for the given key. |
| 86 |
* |
| 87 |
* @param key the key |
| 88 |
* @return the value |
| 89 |
*/ |
| 90 |
Object get(String key) { |
| 91 |
if ("sessions".equals(key)) { |
| 92 |
return server.getSessions(); |
| 93 |
} |
| 94 |
return map.get(key); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Remove a session attribute from the map. |
| 99 |
* |
| 100 |
* @param key the key |
| 101 |
* @return value that was associated with the key, or null |
| 102 |
*/ |
| 103 |
Object remove(String key) { |
| 104 |
return map.remove(key); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get the BNF object. |
| 109 |
* |
| 110 |
* @return the BNF object |
| 111 |
*/ |
| 112 |
Bnf getBnf() { |
| 113 |
return bnf; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Load the SQL grammar BNF. |
| 118 |
*/ |
| 119 |
void loadBnf() { |
| 120 |
try { |
| 121 |
Bnf newBnf = Bnf.getInstance(null); |
| 122 |
DbContextRule columnRule = |
| 123 |
new DbContextRule(contents, DbContextRule.COLUMN); |
| 124 |
DbContextRule newAliasRule = |
| 125 |
new DbContextRule(contents, DbContextRule.NEW_TABLE_ALIAS); |
| 126 |
DbContextRule aliasRule = |
| 127 |
new DbContextRule(contents, DbContextRule.TABLE_ALIAS); |
| 128 |
DbContextRule tableRule = |
| 129 |
new DbContextRule(contents, DbContextRule.TABLE); |
| 130 |
DbContextRule schemaRule = |
| 131 |
new DbContextRule(contents, DbContextRule.SCHEMA); |
| 132 |
DbContextRule columnAliasRule = |
| 133 |
new DbContextRule(contents, DbContextRule.COLUMN_ALIAS); |
| 134 |
DbContextRule procedure = |
| 135 |
new DbContextRule(contents, DbContextRule.PROCEDURE); |
| 136 |
newBnf.updateTopic("procedure", procedure); |
| 137 |
newBnf.updateTopic("column_name", columnRule); |
| 138 |
newBnf.updateTopic("new_table_alias", newAliasRule); |
| 139 |
newBnf.updateTopic("table_alias", aliasRule); |
| 140 |
newBnf.updateTopic("column_alias", columnAliasRule); |
| 141 |
newBnf.updateTopic("table_name", tableRule); |
| 142 |
newBnf.updateTopic("schema_name", schemaRule); |
| 143 |
newBnf.linkStatements(); |
| 144 |
bnf = newBnf; |
| 145 |
} catch (Exception e) { |
| 146 |
// ok we don't have the bnf |
| 147 |
server.traceError(e); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get the SQL statement from history. |
| 153 |
* |
| 154 |
* @param id the history id |
| 155 |
* @return the SQL statement |
| 156 |
*/ |
| 157 |
String getCommand(int id) { |
| 158 |
return commandHistory.get(id); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add a SQL statement to the history. |
| 163 |
* |
| 164 |
* @param sql the SQL statement |
| 165 |
*/ |
| 166 |
void addCommand(String sql) { |
| 167 |
if (sql == null) { |
| 168 |
return; |
| 169 |
} |
| 170 |
sql = sql.trim(); |
| 171 |
if (sql.isEmpty()) { |
| 172 |
return; |
| 173 |
} |
| 174 |
if (commandHistory.size() > MAX_HISTORY) { |
| 175 |
commandHistory.remove(0); |
| 176 |
} |
| 177 |
int idx = commandHistory.indexOf(sql); |
| 178 |
if (idx >= 0) { |
| 179 |
commandHistory.remove(idx); |
| 180 |
} |
| 181 |
commandHistory.add(sql); |
| 182 |
if (server.isCommandHistoryAllowed()) { |
| 183 |
server.saveCommandHistoryList(commandHistory); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get the list of SQL statements in the history. |
| 189 |
* |
| 190 |
* @return the commands |
| 191 |
*/ |
| 192 |
ArrayList<String> getCommandHistory() { |
| 193 |
return commandHistory; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Update session meta data information and get the information in a map. |
| 198 |
* |
| 199 |
* @return a map containing the session meta data |
| 200 |
*/ |
| 201 |
HashMap<String, Object> getInfo() { |
| 202 |
HashMap<String, Object> m = new HashMap<>(); |
| 203 |
m.putAll(map); |
| 204 |
m.put("lastAccess", new Timestamp(lastAccess).toString()); |
| 205 |
try { |
| 206 |
m.put("url", conn == null ? |
| 207 |
"${text.admin.notConnected}" : conn.getMetaData().getURL()); |
| 208 |
m.put("user", conn == null ? |
| 209 |
"-" : conn.getMetaData().getUserName()); |
| 210 |
m.put("lastQuery", commandHistory.isEmpty() ? |
| 211 |
"" : commandHistory.get(0)); |
| 212 |
m.put("executing", executingStatement == null ? |
| 213 |
"${text.admin.no}" : "${text.admin.yes}"); |
| 214 |
} catch (SQLException e) { |
| 215 |
DbException.traceThrowable(e); |
| 216 |
} |
| 217 |
return m; |
| 218 |
} |
| 219 |
|
| 220 |
void setConnection(Connection conn) throws SQLException { |
| 221 |
this.conn = conn; |
| 222 |
if (conn == null) { |
| 223 |
meta = null; |
| 224 |
} else { |
| 225 |
meta = conn.getMetaData(); |
| 226 |
} |
| 227 |
contents = new DbContents(); |
| 228 |
} |
| 229 |
|
| 230 |
DatabaseMetaData getMetaData() { |
| 231 |
return meta; |
| 232 |
} |
| 233 |
|
| 234 |
Connection getConnection() { |
| 235 |
return conn; |
| 236 |
} |
| 237 |
|
| 238 |
DbContents getContents() { |
| 239 |
return contents; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Shutdown the server when disconnecting. |
| 244 |
*/ |
| 245 |
void setShutdownServerOnDisconnect() { |
| 246 |
this.shutdownServerOnDisconnect = true; |
| 247 |
} |
| 248 |
|
| 249 |
boolean getShutdownServerOnDisconnect() { |
| 250 |
return shutdownServerOnDisconnect; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Close the connection and stop the statement if one is currently |
| 255 |
* executing. |
| 256 |
*/ |
| 257 |
void close() { |
| 258 |
if (executingStatement != null) { |
| 259 |
try { |
| 260 |
executingStatement.cancel(); |
| 261 |
} catch (Exception e) { |
| 262 |
// ignore |
| 263 |
} |
| 264 |
} |
| 265 |
if (conn != null) { |
| 266 |
try { |
| 267 |
conn.close(); |
| 268 |
} catch (Exception e) { |
| 269 |
// ignore |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
} |