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.THeaderProtocol;
11 import com.facebook.thrift.protocol.TProtocol;
12 import com.facebook.thrift.transport.THeaderTransport;
13 import com.facebook.thrift.transport.THttp2Client;
14 import com.facebook.thrift.transport.TSocket;
15 import com.facebook.thrift.transport.TTransport;
16 import com.facebook.thrift.transport.TTransportException;
17 import com.facebook.thrift.utils.StandardCharsets;
18 import com.google.common.base.Charsets;
19 import com.vesoft.nebula.ErrorCode;
20 import com.vesoft.nebula.client.graph.data.CASignedSSLParam;
21 import com.vesoft.nebula.client.graph.data.HostAddress;
22 import com.vesoft.nebula.client.graph.data.SSLParam;
23 import com.vesoft.nebula.client.graph.data.SelfSignedSSLParam;
24 import com.vesoft.nebula.client.graph.exception.AuthFailedException;
25 import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException;
26 import com.vesoft.nebula.client.graph.exception.IOErrorException;
27 import com.vesoft.nebula.graph.AuthResponse;
28 import com.vesoft.nebula.graph.ExecutionResponse;
29 import com.vesoft.nebula.graph.GraphService;
30 import com.vesoft.nebula.graph.VerifyClientVersionReq;
31 import com.vesoft.nebula.graph.VerifyClientVersionResp;
32 import com.vesoft.nebula.util.SslUtil;
33 import java.io.IOException;
34 import java.util.Collections;
35 import java.util.HashMap;
37 import javax.net.ssl.SSLSocketFactory;
38 import javax.net.ssl.TrustManager;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
45 private static final Logger LOGGER = LoggerFactory.getLogger(
SyncConnection.class);
47 protected TTransport transport =
null;
48 protected TProtocol protocol =
null;
49 private GraphService.Client client =
null;
50 private int timeout = 0;
52 private boolean enabledSsl =
false;
53 private SSLSocketFactory sslSocketFactory =
null;
54 private boolean useHttp2 =
false;
56 private Map<String, String> headers =
new HashMap<>();
61 this.open(address, timeout, sslParam,
false, headers);
66 Map<String, String> headers)
69 this.serverAddr = address;
70 this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
71 this.enabledSsl =
true;
72 this.sslParam = sslParam;
73 this.useHttp2 = isUseHttp2;
74 this.headers = headers;
75 if (sslSocketFactory ==
null) {
76 if (sslParam ==
null) {
77 sslSocketFactory = SslUtil.getSSLSocketFactoryWithoutVerify();
87 getProtocolWithTlsHttp2();
92 client =
new GraphService.Client(protocol);
94 VerifyClientVersionResp resp =
95 client.verifyClientVersion(
new VerifyClientVersionReq());
96 if (resp.error_code != ErrorCode.SUCCEEDED) {
97 client.getInputProtocol().getTransport().close();
101 }
catch (TException | IOException e) {
110 this.open(address, timeout,
false, headers);
115 boolean isUseHttp2, Map<String, String> headers)
118 this.serverAddr = address;
119 this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
120 this.useHttp2 = isUseHttp2;
121 this.headers = headers;
123 getProtocolForHttp2();
127 client =
new GraphService.Client(protocol);
129 VerifyClientVersionResp resp =
130 client.verifyClientVersion(
new VerifyClientVersionReq());
131 if (resp.error_code != ErrorCode.SUCCEEDED) {
132 client.getInputProtocol().getTransport().close();
136 }
catch (TException e) {
145 private void getProtocolWithTlsHttp2() {
146 String url =
"https://" + serverAddr.getHost() +
":" + serverAddr.getPort();
147 TrustManager trustManager;
148 if (SslUtil.getTrustManagers() ==
null || SslUtil.getTrustManagers().length == 0) {
151 trustManager = SslUtil.getTrustManagers()[0];
153 this.transport =
new THttp2Client(url, sslSocketFactory, trustManager)
154 .setConnectTimeout(timeout)
155 .setReadTimeout(timeout)
156 .setCustomHeaders(headers);
159 this.protocol =
new TBinaryProtocol(transport);
165 private void getProtocolForTls()
throws IOException {
166 this.transport =
new THeaderTransport(
new TSocket(
167 sslSocketFactory.createSocket(serverAddr.getHost(),
168 serverAddr.getPort()),
this.timeout,
this.timeout));
169 this.protocol =
new THeaderProtocol((THeaderTransport) transport);
175 private void getProtocolForHttp2() {
176 String url =
"http://" + serverAddr.getHost() +
":" + serverAddr.getPort();
177 this.transport =
new THttp2Client(url)
178 .setConnectTimeout(timeout)
179 .setReadTimeout(timeout)
180 .setCustomHeaders(headers);
182 this.protocol =
new TBinaryProtocol(transport);
188 private void getProtocol() {
189 this.transport =
new THeaderTransport(
new TSocket(
190 serverAddr.getHost(), serverAddr.getPort(),
this.timeout,
this.timeout));
192 this.protocol =
new THeaderProtocol((THeaderTransport) transport);
210 open(serverAddr, timeout, sslParam, useHttp2, headers);
212 open(serverAddr, timeout, useHttp2, headers);
216 public AuthResult authenticate(String user, String password)
219 AuthResponse resp = client.authenticate(user.getBytes(Charsets.UTF_8),
220 password.getBytes(Charsets.UTF_8));
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(
260 stmt.getBytes(Charsets.UTF_8),
262 }
catch (TException e) {
263 if (e instanceof TTransportException) {
264 TTransportException te = (TTransportException) e;
265 if (te.getType() == TTransportException.END_OF_FILE) {
267 }
else if (te.getType() == TTransportException.NOT_OPEN) {
269 }
else if (te.getType() == TTransportException.TIMED_OUT
270 || te.getMessage().contains(
"Read timed out")) {
274 LOGGER.error(ex.getMessage());
283 public String executeJson(
long sessionID, String stmt)
285 return executeJsonWithParameter(sessionID, stmt,
286 (Map<
byte[], com.vesoft.nebula.Value>) Collections.EMPTY_MAP);
289 public String executeJsonWithParameter(
long sessionID, String stmt,
290 Map<
byte[], com.vesoft.nebula.Value> parameterMap)
294 client.executeJsonWithParameter(
296 stmt.getBytes(Charsets.UTF_8),
298 return new String(result, StandardCharsets.UTF_8);
299 }
catch (TException e) {
300 if (e instanceof TTransportException) {
301 TTransportException te = (TTransportException) e;
302 if (te.getType() == TTransportException.END_OF_FILE) {
304 }
else if (te.getType() == TTransportException.NOT_OPEN) {
306 }
else if (te.getType() == TTransportException.TIMED_OUT
307 || te.getMessage().contains(
"Read timed out")) {
311 LOGGER.error(ex.getMessage());
320 public void signout(
long sessionId) {
321 client.signout(sessionId);
325 public boolean ping() {
327 execute(0,
"YIELD 1;");
335 public boolean ping(
long sessionID) {
337 execute(sessionID,
"YIELD 1;");
345 public void close() {
346 if (transport !=
null && transport.isOpen()) {