NebulaGraph Java Client  release-3.8
GraphStorageConnection.java
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.storage;
7 
8 import com.facebook.thrift.TException;
9 import com.facebook.thrift.protocol.TCompactProtocol;
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.TSocket;
14 import com.facebook.thrift.transport.TTransport;
15 import com.facebook.thrift.transport.TTransportException;
16 import com.vesoft.nebula.client.graph.data.CASignedSSLParam;
17 import com.vesoft.nebula.client.graph.data.HostAddress;
18 import com.vesoft.nebula.client.graph.data.SSLParam;
19 import com.vesoft.nebula.client.graph.data.SelfSignedSSLParam;
20 import com.vesoft.nebula.client.graph.exception.IOErrorException;
21 import com.vesoft.nebula.storage.GraphStorageService;
22 import com.vesoft.nebula.storage.ScanEdgeRequest;
23 import com.vesoft.nebula.storage.ScanResponse;
24 import com.vesoft.nebula.storage.ScanVertexRequest;
25 import com.vesoft.nebula.util.SslUtil;
26 import java.io.IOException;
27 import java.io.Serializable;
28 import java.net.InetAddress;
29 import javax.net.ssl.SSLSocketFactory;
30 
31 public class GraphStorageConnection implements Serializable {
32 
33  private static final long serialVersionUID = -3631352515689239788L;
34 
35  protected THeaderTransport transport = null;
36  protected THeaderProtocol protocol = null;
37  public HostAddress address;
38  private GraphStorageService.Client client;
39 
40  protected GraphStorageConnection() {
41  }
42 
43  protected GraphStorageConnection open(HostAddress address, int timeout, boolean enableSSL,
44  SSLParam sslParam) throws Exception {
45  this.address = address;
46  int newTimeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
47  if (enableSSL) {
48  SSLSocketFactory sslSocketFactory;
49  if (sslParam.getSignMode() == SSLParam.SignMode.CA_SIGNED) {
50  sslSocketFactory = SslUtil.getSSLSocketFactoryWithCA((CASignedSSLParam) sslParam);
51  } else {
52  sslSocketFactory =
53  SslUtil.getSSLSocketFactoryWithoutCA((SelfSignedSSLParam) sslParam);
54  }
55  try {
56  transport =
57  new THeaderTransport(new TSocket(
58  sslSocketFactory.createSocket(
59  InetAddress.getByName(address.getHost()).getHostAddress(),
60  address.getPort()),
61  newTimeout,
62  newTimeout));
63  } catch (IOException e) {
64  throw new TTransportException(IOErrorException.E_UNKNOWN, e);
65  }
66  } else {
67  this.transport = new THeaderTransport(new TSocket(
68  InetAddress.getByName(address.getHost()).getHostAddress(),
69  address.getPort(),
70  newTimeout,
71  newTimeout));
72  this.transport.open();
73  }
74  this.protocol = new THeaderProtocol(transport);
75  client = new GraphStorageService.Client(protocol);
76  return this;
77  }
78 
79 
80  public ScanResponse scanVertex(ScanVertexRequest request) throws TException {
81  return client.scanVertex(request);
82  }
83 
84  public ScanResponse scanEdge(ScanEdgeRequest request) throws TException {
85  return client.scanEdge(request);
86  }
87 
88  public void close() {
89  if (transport != null && transport.isOpen()) {
90  transport.close();
91  }
92  }
93 
94  public HostAddress getAddress() {
95  return address;
96  }
97 
98 }