6 package com.vesoft.nebula.client.graph.net;
8 import com.facebook.thrift.TException;
9 import com.facebook.thrift.protocol.TBinaryProtocol;
10 import com.facebook.thrift.protocol.TCompactProtocol;
11 import com.facebook.thrift.protocol.THeaderProtocol;
12 import com.facebook.thrift.protocol.TProtocol;
13 import com.facebook.thrift.transport.THeaderTransport;
14 import com.facebook.thrift.transport.THttp2Client;
15 import com.facebook.thrift.transport.TSocket;
16 import com.facebook.thrift.transport.TTransport;
17 import com.facebook.thrift.transport.TTransportException;
18 import com.facebook.thrift.utils.StandardCharsets;
19 import com.google.common.base.Charsets;
20 import com.vesoft.nebula.ErrorCode;
21 import com.vesoft.nebula.client.graph.data.CASignedSSLParam;
22 import com.vesoft.nebula.client.graph.data.HostAddress;
23 import com.vesoft.nebula.client.graph.data.SSLParam;
24 import com.vesoft.nebula.client.graph.data.SelfSignedSSLParam;
25 import com.vesoft.nebula.client.graph.exception.AuthFailedException;
26 import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException;
27 import com.vesoft.nebula.client.graph.exception.IOErrorException;
28 import com.vesoft.nebula.graph.AuthResponse;
29 import com.vesoft.nebula.graph.ExecutionResponse;
30 import com.vesoft.nebula.graph.GraphService;
31 import com.vesoft.nebula.graph.VerifyClientVersionReq;
32 import com.vesoft.nebula.graph.VerifyClientVersionResp;
33 import com.vesoft.nebula.util.SslUtil;
34 import java.io.IOException;
35 import java.util.Collections;
36 import java.util.HashMap;
38 import javax.net.ssl.SSLSocketFactory;
39 import javax.net.ssl.TrustManager;
40 import okhttp3.internal.http2.Http2Connection;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
47 private static final Logger LOGGER = LoggerFactory.getLogger(
SyncConnection.class);
49 protected TTransport transport =
null;
50 protected TProtocol protocol =
null;
51 private GraphService.Client client =
null;
52 private int timeout = 0;
54 private boolean enabledSsl =
false;
55 private SSLSocketFactory sslSocketFactory =
null;
56 private boolean useHttp2 =
false;
58 private Map<String, String> headers =
new HashMap<>();
63 this.open(address, timeout, sslParam,
false, headers);
68 Map<String, String> headers)
71 this.serverAddr = address;
72 this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
73 this.enabledSsl =
true;
74 this.sslParam = sslParam;
75 this.useHttp2 = isUseHttp2;
76 this.headers = headers;
77 if (sslSocketFactory ==
null) {
87 getProtocolWithTlsHttp2();
92 client =
new GraphService.Client(protocol);
95 VerifyClientVersionResp resp =
96 client.verifyClientVersion(
new VerifyClientVersionReq());
97 if (resp.error_code != ErrorCode.SUCCEEDED) {
98 client.getInputProtocol().getTransport().close();
102 }
catch (TException | IOException e) {
111 this.open(address, timeout,
false, headers);
116 boolean isUseHttp2, Map<String,String> headers)
119 this.serverAddr = address;
120 this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
121 this.useHttp2 = isUseHttp2;
122 this.headers = headers;
124 getProtocolForHttp2();
128 client =
new GraphService.Client(protocol);
131 VerifyClientVersionResp resp =
132 client.verifyClientVersion(
new VerifyClientVersionReq());
133 if (resp.error_code != ErrorCode.SUCCEEDED) {
134 client.getInputProtocol().getTransport().close();
138 }
catch (TException e) {
146 private void getProtocolWithTlsHttp2() {
147 String url =
"https://" + serverAddr.getHost() +
":" + serverAddr.getPort();
148 TrustManager trustManager;
149 if (SslUtil.getTrustManagers() ==
null || SslUtil.getTrustManagers().length == 0) {
152 trustManager = SslUtil.getTrustManagers()[0];
154 this.transport =
new THttp2Client(url, sslSocketFactory, trustManager)
155 .setConnectTimeout(timeout)
156 .setReadTimeout(timeout)
157 .setCustomHeaders(headers);
160 this.protocol =
new TBinaryProtocol(transport);
166 private void getProtocolForTls()
throws IOException {
167 this.transport =
new THeaderTransport(
new TSocket(
168 sslSocketFactory.createSocket(serverAddr.getHost(),
169 serverAddr.getPort()),
this.timeout,
this.timeout));
170 this.protocol =
new THeaderProtocol((THeaderTransport) transport);
176 private void getProtocolForHttp2() {
177 String url =
"http://" + serverAddr.getHost() +
":" + serverAddr.getPort();
178 this.transport =
new THttp2Client(url)
179 .setConnectTimeout(timeout)
180 .setReadTimeout(timeout)
181 .setCustomHeaders(headers);
183 this.protocol =
new TBinaryProtocol(transport);
189 private void getProtocol() {
190 this.transport =
new THeaderTransport(
new TSocket(
191 serverAddr.getHost(), serverAddr.getPort(),
this.timeout,
this.timeout));
193 this.protocol =
new THeaderProtocol((THeaderTransport) transport);
211 open(serverAddr, timeout, sslParam, useHttp2, headers);
213 open(serverAddr, timeout, useHttp2, headers);
217 public AuthResult authenticate(String user, String password)
220 AuthResponse resp = client.authenticate(user.getBytes(), password.getBytes());
221 if (resp.error_code != ErrorCode.SUCCEEDED) {
222 if (resp.error_msg !=
null) {
226 "The error_msg is null, "
227 +
"maybe the service not set or the response is disorder.");
230 return new AuthResult(resp.getSession_id(), resp.getTime_zone_offset_seconds());
231 }
catch (TException e) {
232 if (e instanceof TTransportException) {
233 TTransportException te = (TTransportException) e;
234 if (te.getType() == TTransportException.END_OF_FILE) {
236 }
else if (te.getType() == TTransportException.TIMED_OUT
237 || te.getMessage().contains(
"Read timed out")) {
240 }
else if (te.getType() == TTransportException.NOT_OPEN) {
248 public ExecutionResponse execute(
long sessionID, String stmt)
250 return executeWithParameter(sessionID,
251 stmt, (Map<
byte[], com.vesoft.nebula.Value>) Collections.EMPTY_MAP);
254 public ExecutionResponse executeWithParameter(
long sessionID, String stmt,
255 Map<
byte[], com.vesoft.nebula.Value> parameterMap)
258 return client.executeWithParameter(sessionID, stmt.getBytes(), parameterMap);
259 }
catch (TException e) {
260 if (e instanceof TTransportException) {
261 TTransportException te = (TTransportException) e;
262 if (te.getType() == TTransportException.END_OF_FILE) {
264 }
else if (te.getType() == TTransportException.NOT_OPEN) {
266 }
else if (te.getType() == TTransportException.TIMED_OUT
267 || te.getMessage().contains(
"Read timed out")) {
271 LOGGER.error(ex.getMessage());
280 public String executeJson(
long sessionID, String stmt)
282 return executeJsonWithParameter(sessionID, stmt,
283 (Map<
byte[], com.vesoft.nebula.Value>) Collections.EMPTY_MAP);
286 public String executeJsonWithParameter(
long sessionID, String stmt,
287 Map<
byte[], com.vesoft.nebula.Value> parameterMap)
291 client.executeJsonWithParameter(sessionID, stmt.getBytes(), parameterMap);
292 return new String(result, StandardCharsets.UTF_8);
293 }
catch (TException e) {
294 if (e instanceof TTransportException) {
295 TTransportException te = (TTransportException) e;
296 if (te.getType() == TTransportException.END_OF_FILE) {
298 }
else if (te.getType() == TTransportException.NOT_OPEN) {
300 }
else if (te.getType() == TTransportException.TIMED_OUT
301 || te.getMessage().contains(
"Read timed out")) {
305 LOGGER.error(ex.getMessage());
314 public void signout(
long sessionId) {
315 client.signout(sessionId);
319 public boolean ping() {
321 execute(0,
"YIELD 1;");
329 public boolean ping(
long sessionID) {
331 execute(sessionID,
"YIELD 1;");
339 public void close() {
340 if (transport !=
null && transport.isOpen()) {