-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrbitSqlServer.java
More file actions
427 lines (399 loc) 路 16.8 KB
/
Copy pathOrbitSqlServer.java
File metadata and controls
427 lines (399 loc) 路 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
/** Retained, protocol-only JDBC process used by Orbit's SQL Server Connector. */
public final class OrbitSqlServer {
private static final String PROTOCOL = "ORBIT/1";
private static final int MAX_HEADER_BYTES = 8192;
private static final int MAX_FIELD_BYTES = 128 * 1024 * 1024;
private final BufferedInputStream input = new BufferedInputStream(System.in);
private final BufferedOutputStream output = new BufferedOutputStream(System.out);
private Connection connection;
private ConnectionSettings connectionSettings;
public static void main(String[] args) {
if (Runtime.version().feature() < 11) {
System.err.println("Orbit SQL Server helper requires Java 11 or newer");
System.exit(2);
}
try {
Class<?> driverClass = Class.forName("net.sourceforge.jtds.jdbc.Driver");
String driverVersion = driverClass.getPackage().getImplementationVersion();
if (!"1.3.1".equals(driverVersion)) {
throw new IllegalStateException("Orbit SQL Server helper requires jTDS 1.3.1, found " + driverVersion);
}
if (args.length == 1 && "--doctor".equals(args[0])) {
System.out.println("Orbit SQL Server helper: Java " + Runtime.version().feature() + "; jTDS 1.3.1 loaded");
return;
}
if (args.length != 0) {
throw new IllegalArgumentException("Orbit SQL Server helper accepts only --doctor");
}
new OrbitSqlServer().run();
} catch (Throwable error) {
System.err.println("Orbit SQL Server helper failed: " + safeMessage(error));
System.exit(1);
}
}
private void run() throws IOException {
while (true) {
Request request = readRequest();
if (request == null) {
closeConnection();
return;
}
boolean fatal = false;
String response;
try {
ensureConnection(request.settings);
response = execute(request.statement);
} catch (SQLException error) {
fatal = connection == null || connectionFailure(error);
response = errorResponse(safeMessage(error), fatal);
}
writeResponse(request.marker, response);
if (fatal) {
closeConnection();
return;
}
}
}
private void ensureConnection(ConnectionSettings settings) throws SQLException {
if (connection != null) {
if (connection.isClosed()) {
throw new SQLException("SQL Server JDBC connection is closed", "08003");
}
if (!settings.equals(connectionSettings)) {
throw new SQLException("SQL Server JDBC settings changed inside a retained session", "08003");
}
return;
}
Properties properties = new Properties();
properties.setProperty("user", settings.user);
properties.setProperty("password", settings.password);
properties.setProperty("ssl", settings.trustServerCertificate ? "require" : "authenticate");
if (!settings.database.isEmpty()) {
properties.setProperty("databaseName", settings.database);
}
if (!settings.instance.isEmpty()) {
properties.setProperty("instance", settings.instance);
}
if (settings.domainAuthentication) {
properties.setProperty("domain", settings.domain);
properties.setProperty("useNTLMv2", "true");
}
// Credentials and driver properties stay in Properties; only the
// validated host and numeric port become part of the JDBC URL.
String url = "jdbc:jtds:sqlserver://" + settings.host;
if (settings.port != 0) {
url += ":" + settings.port;
}
connection = DriverManager.getConnection(url, properties);
connectionSettings = settings;
}
private String execute(String sql) throws SQLException {
List<String> columns = null;
List<List<String>> rows = null;
int tabularResults = 0;
try (Statement statement = connection.createStatement()) {
boolean resultAvailable = statement.execute(sql);
while (true) {
if (resultAvailable) {
tabularResults++;
if (tabularResults > 1) {
throw new SQLException("SQL Server JDBC statements may return only one tabular result");
}
try (ResultSet result = statement.getResultSet()) {
ResultData data = readResult(result);
columns = data.columns;
rows = data.rows;
}
} else if (statement.getUpdateCount() == -1) {
break;
}
resultAvailable = statement.getMoreResults(Statement.CLOSE_CURRENT_RESULT);
}
}
if (columns == null) {
columns = new ArrayList<>();
rows = new ArrayList<>();
}
return successResponse(columns, rows);
}
private ResultData readResult(ResultSet result) throws SQLException {
ResultSetMetaData metadata = result.getMetaData();
int width = metadata.getColumnCount();
List<String> columns = new ArrayList<>(width);
Set<String> seen = new HashSet<>();
for (int index = 1; index <= width; index++) {
String label = metadata.getColumnLabel(index);
if (label == null || label.isEmpty()) {
throw new SQLException("SQL Server JDBC result column " + index + " label must not be empty");
}
if (!seen.add(label)) {
throw new SQLException("SQL Server JDBC result has duplicate label " + label);
}
columns.add(label);
}
List<List<String>> rows = new ArrayList<>();
while (result.next()) {
List<String> row = new ArrayList<>(width);
for (int index = 1; index <= width; index++) {
String value = result.getString(index);
row.add(result.wasNull() ? null : value);
}
rows.add(row);
}
return new ResultData(columns, rows);
}
private Request readRequest() throws IOException {
String header = readAsciiLine();
if (header == null) {
return null;
}
String[] parts = header.split(" ", -1);
if (parts.length != 12 || !PROTOCOL.equals(parts[0]) || !parts[1].matches("[A-Za-z0-9_]+")) {
throw new IOException("malformed request header");
}
int hostLength = length(parts[2]);
int port = integer(parts[3], 0, 65535, "port");
int instanceLength = length(parts[4]);
int databaseLength = length(parts[5]);
boolean domainAuthentication;
if ("D".equals(parts[6])) {
domainAuthentication = true;
} else if ("S".equals(parts[6])) {
domainAuthentication = false;
} else {
throw new IOException("unsupported authentication mode");
}
int domainLength = length(parts[7]);
int userLength = length(parts[8]);
int passwordLength = length(parts[9]);
int statementLength = length(parts[10]);
boolean trustServerCertificate;
if ("1".equals(parts[11])) {
trustServerCertificate = true;
} else if ("0".equals(parts[11])) {
trustServerCertificate = false;
} else {
throw new IOException("invalid certificate trust flag");
}
String host = readUtf8(hostLength);
String instance = readUtf8(instanceLength);
String database = readUtf8(databaseLength);
String domain = readUtf8(domainLength);
String user = readUtf8(userLength);
String password = readUtf8(passwordLength);
String statement = readUtf8(statementLength);
ConnectionSettings settings = new ConnectionSettings(
host, port, instance, database, domainAuthentication, domain, user, password, trustServerCertificate);
return new Request(parts[1], settings, statement);
}
private String readAsciiLine() throws IOException {
ByteArrayOutputStream line = new ByteArrayOutputStream();
while (line.size() <= MAX_HEADER_BYTES) {
int value = input.read();
if (value == -1) {
if (line.size() == 0) {
return null;
}
throw new EOFException("incomplete request header");
}
if (value == '\n') {
return line.toString(StandardCharsets.US_ASCII);
}
if (value < 0x20 || value > 0x7e) {
throw new IOException("request header is not ASCII");
}
line.write(value);
}
throw new IOException("request header is too large");
}
private String readUtf8(int length) throws IOException {
byte[] bytes = input.readNBytes(length);
if (bytes.length != length) {
throw new EOFException("incomplete request payload");
}
try {
return StandardCharsets.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
.decode(java.nio.ByteBuffer.wrap(bytes))
.toString();
} catch (CharacterCodingException error) {
throw new IOException("request payload is not UTF-8", error);
}
}
private void writeResponse(String marker, String payload) throws IOException {
byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
output.write((PROTOCOL + " " + marker + " " + bytes.length + "\n").getBytes(StandardCharsets.US_ASCII));
output.write(bytes);
output.flush();
}
private static int length(String value) throws IOException {
return integer(value, 0, MAX_FIELD_BYTES, "field length");
}
private static int integer(String value, int minimum, int maximum, String label) throws IOException {
try {
int parsed = Integer.parseInt(value);
if (parsed < minimum || parsed > maximum) {
throw new IOException(label + " is out of range");
}
return parsed;
} catch (NumberFormatException error) {
throw new IOException(label + " is not an integer", error);
}
}
private static boolean connectionFailure(SQLException error) {
for (SQLException current = error; current != null; current = current.getNextException()) {
String state = current.getSQLState();
if (state != null && state.startsWith("08")) {
return true;
}
}
return false;
}
private static String successResponse(List<String> columns, List<List<String>> rows) {
StringBuilder json = new StringBuilder();
json.append("{\"ok\":true,\"columns\":[");
for (int index = 0; index < columns.size(); index++) {
if (index != 0) json.append(',');
appendJsonString(json, columns.get(index));
}
json.append("],\"rows\":[");
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
if (rowIndex != 0) json.append(',');
json.append('[');
List<String> row = rows.get(rowIndex);
for (int columnIndex = 0; columnIndex < row.size(); columnIndex++) {
if (columnIndex != 0) json.append(',');
String value = row.get(columnIndex);
if (value == null) json.append("null");
else appendJsonString(json, value);
}
json.append(']');
}
return json.append("]}").toString();
}
private static String errorResponse(String message, boolean fatal) {
StringBuilder json = new StringBuilder("{\"ok\":false,\"fatal\":");
json.append(fatal ? "true" : "false").append(",\"error\":");
appendJsonString(json, message);
return json.append('}').toString();
}
private static void appendJsonString(StringBuilder json, String value) {
json.append('"');
for (int index = 0; index < value.length(); index++) {
char character = value.charAt(index);
switch (character) {
case '"': json.append("\\\""); break;
case '\\': json.append("\\\\"); break;
case '\b': json.append("\\b"); break;
case '\f': json.append("\\f"); break;
case '\n': json.append("\\n"); break;
case '\r': json.append("\\r"); break;
case '\t': json.append("\\t"); break;
default:
if (character < 0x20) json.append(String.format("\\u%04x", (int) character));
else json.append(character);
}
}
json.append('"');
}
private static String safeMessage(Throwable error) {
String message = error.getMessage();
return message == null || message.isEmpty() ? error.getClass().getSimpleName() : message;
}
private void closeConnection() {
if (connection == null) return;
try {
connection.close();
} catch (SQLException ignored) {
// The process is already terminating; there is no caller to recover.
} finally {
connection = null;
connectionSettings = null;
}
}
private static final class Request {
final String marker;
final ConnectionSettings settings;
final String statement;
Request(String marker, ConnectionSettings settings, String statement) {
this.marker = marker;
this.settings = settings;
this.statement = statement;
}
}
private static final class ResultData {
final List<String> columns;
final List<List<String>> rows;
ResultData(List<String> columns, List<List<String>> rows) {
this.columns = columns;
this.rows = rows;
}
}
private static final class ConnectionSettings {
final String host;
final int port;
final String instance;
final String database;
final boolean domainAuthentication;
final String domain;
final String user;
final String password;
final boolean trustServerCertificate;
ConnectionSettings(String host, int port, String instance, String database, boolean domainAuthentication,
String domain, String user, String password, boolean trustServerCertificate) {
this.host = host;
this.port = port;
this.instance = instance;
this.database = database;
this.domainAuthentication = domainAuthentication;
this.domain = domain;
this.user = user;
this.password = password;
this.trustServerCertificate = trustServerCertificate;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ConnectionSettings)) return false;
ConnectionSettings settings = (ConnectionSettings) other;
return port == settings.port
&& domainAuthentication == settings.domainAuthentication
&& trustServerCertificate == settings.trustServerCertificate
&& host.equals(settings.host)
&& instance.equals(settings.instance)
&& database.equals(settings.database)
&& domain.equals(settings.domain)
&& user.equals(settings.user)
&& password.equals(settings.password);
}
@Override
public int hashCode() {
int result = host.hashCode();
result = 31 * result + port;
result = 31 * result + instance.hashCode();
result = 31 * result + database.hashCode();
result = 31 * result + domain.hashCode();
result = 31 * result + user.hashCode();
return result;
}
}
}