Search in sources :

Example 6 with ClientException

use of com.baidu.hugegraph.rest.ClientException in project incubator-hugegraph-toolchain by apache.

the class HugeClientUtil method tryConnect.

public static HugeClient tryConnect(GraphConnection connection) {
    String graph = connection.getGraph();
    String host = connection.getHost();
    Integer port = connection.getPort();
    String username = connection.getUsername();
    String password = connection.getPassword();
    int timeout = connection.getTimeout();
    String protocol = connection.getProtocol() == null ? DEFAULT_PROTOCOL : connection.getProtocol();
    String trustStoreFile = connection.getTrustStoreFile();
    String trustStorePassword = connection.getTrustStorePassword();
    String url = UriComponentsBuilder.newInstance().scheme(protocol).host(host).port(port).toUriString();
    if (username == null) {
        username = "";
        password = "";
    }
    HugeClient client;
    try {
        client = HugeClient.builder(url, graph).configUser(username, password).configTimeout(timeout).configSSL(trustStoreFile, trustStorePassword).build();
    } catch (IllegalStateException e) {
        String message = e.getMessage();
        if (message != null && message.startsWith("The version")) {
            throw new ExternalException("client-server.version.unmatched", e);
        }
        if (message != null && (message.startsWith("Error loading trust store from") || message.startsWith("Cannot find trust store file"))) {
            throw new ExternalException("https.load.truststore.error", e);
        }
        throw e;
    } catch (ServerException e) {
        String message = e.getMessage();
        if (Constant.STATUS_UNAUTHORIZED == e.status() || (message != null && message.startsWith("Authentication"))) {
            throw new ExternalException("graph-connection.username-or-password.incorrect", e);
        }
        if (message != null && message.contains("Invalid syntax for " + "username and password")) {
            throw new ExternalException("graph-connection.missing-username-password", e);
        }
        throw e;
    } catch (ClientException e) {
        Throwable cause = e.getCause();
        if (cause == null || cause.getMessage() == null) {
            throw e;
        }
        String message = cause.getMessage();
        if (message.contains("Connection refused")) {
            throw new ExternalException("service.unavailable", e, host, port);
        } else if (message.contains("java.net.UnknownHostException") || message.contains("Host name may not be null")) {
            throw new ExternalException("service.unknown-host", e, host);
        } else if (message.contains("<!doctype html>")) {
            throw new ExternalException("service.suspected-web", e, host, port);
        }
        throw e;
    }
    try {
        ResultSet rs = client.gremlin().gremlin("g.V().limit(1)").execute();
        rs.iterator().forEachRemaining(Result::getObject);
    } catch (ServerException e) {
        if (Constant.STATUS_UNAUTHORIZED == e.status()) {
            throw new ExternalException("graph-connection.username-or-password.incorrect", e);
        }
        String message = e.message();
        if (message != null && message.contains("Could not rebind [g]")) {
            throw new ExternalException("graph-connection.graph.unexist", e, graph, host, port);
        }
        if (!isAcceptable(message)) {
            throw e;
        }
    } catch (Exception e) {
        client.close();
        throw e;
    }
    return client;
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) ServerException(com.baidu.hugegraph.exception.ServerException) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) ClientException(com.baidu.hugegraph.rest.ClientException) ExternalException(com.baidu.hugegraph.exception.ExternalException) ServerException(com.baidu.hugegraph.exception.ServerException) ExternalException(com.baidu.hugegraph.exception.ExternalException) ClientException(com.baidu.hugegraph.rest.ClientException) Result(com.baidu.hugegraph.structure.gremlin.Result)

Example 7 with ClientException

use of com.baidu.hugegraph.rest.ClientException in project incubator-hugegraph-toolchain by apache.

the class HugeClientHolder method create.

public static HugeClient create(LoadOptions options) {
    boolean useHttps = options.protocol != null && options.protocol.equals(LoadOptions.HTTPS_SCHEMA);
    String address = options.host + ":" + options.port;
    if (!options.host.startsWith(Constants.HTTP_PREFIX) && !options.host.startsWith(Constants.HTTPS_PREFIX)) {
        if (useHttps) {
            address = Constants.HTTPS_PREFIX + address;
        } else {
            address = Constants.HTTP_PREFIX + address;
        }
    }
    String username = options.username != null ? options.username : options.graph;
    HugeClientBuilder builder;
    try {
        builder = HugeClient.builder(address, options.graph).configUser(username, options.token).configTimeout(options.timeout).configPool(options.maxConnections, options.maxConnectionsPerRoute);
        if (useHttps) {
            String trustFile;
            if (options.trustStoreFile == null) {
                String homePath = System.getProperty("loader.home.path");
                E.checkArgument(StringUtils.isNotEmpty(homePath), "The system property 'loader.home.path' " + "can't be null or empty when enable " + "https protocol");
                trustFile = Paths.get(homePath, Constants.TRUST_STORE_FILE).toString();
            } else {
                trustFile = options.trustStoreFile;
            }
            // Hard code: "hugegraph"
            String token = options.trustStoreToken == null ? "hugegraph" : options.trustStoreToken;
            builder.configSSL(trustFile, token);
        }
        return builder.build();
    } catch (IllegalStateException e) {
        String message = e.getMessage();
        if (message != null && message.startsWith("The version")) {
            throw new LoadException("The version of hugegraph-client and " + "hugegraph-server don't match", e);
        }
        throw e;
    } catch (ServerException e) {
        String message = e.getMessage();
        if (Constants.STATUS_UNAUTHORIZED == e.status() || (message != null && message.startsWith("Authentication"))) {
            throw new LoadException("Incorrect username or password", e);
        }
        throw e;
    } catch (ClientException e) {
        Throwable cause = e.getCause();
        if (cause == null || cause.getMessage() == null) {
            throw e;
        }
        String message = cause.getMessage();
        if (message.contains("Connection refused")) {
            throw new LoadException("The service %s:%s is unavailable", e, options.host, options.port);
        } else if (message.contains("java.net.UnknownHostException") || message.contains("Host name may not be null")) {
            throw new LoadException("The host %s is unknown", e, options.host);
        } else if (message.contains("connect timed out")) {
            throw new LoadException("Connect service %s:%s timeout, " + "please check service is available " + "and network is unobstructed", e, options.host, options.port);
        }
        throw e;
    }
}
Also used : ServerException(com.baidu.hugegraph.exception.ServerException) ClientException(com.baidu.hugegraph.rest.ClientException) HugeClientBuilder(com.baidu.hugegraph.driver.HugeClientBuilder) LoadException(com.baidu.hugegraph.loader.exception.LoadException)

Example 8 with ClientException

use of com.baidu.hugegraph.rest.ClientException in project incubator-hugegraph-toolchain by apache.

the class LocalDirectory method outputStream.

@Override
public OutputStream outputStream(String file, boolean compress, boolean override) {
    String path = Paths.get(this.directory(), file + this.suffix(compress)).toString();
    FileOutputStream os = null;
    ZipOutputStream zos = null;
    try {
        os = new FileOutputStream(path, !override);
        if (!compress) {
            return os;
        }
        zos = new ZipOutputStream(os);
        ZipEntry entry = new ZipEntry(file);
        zos.putNextEntry(entry);
    } catch (IOException e) {
        closeAndIgnoreException(zos);
        closeAndIgnoreException(os);
        throw new ClientException("Failed to write to local file: %s", e, path);
    }
    return zos;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ClientException(com.baidu.hugegraph.rest.ClientException)

Aggregations

ClientException (com.baidu.hugegraph.rest.ClientException)8 IOException (java.io.IOException)6 HugeClient (com.baidu.hugegraph.driver.HugeClient)3 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)3 ServerException (com.baidu.hugegraph.exception.ServerException)2 Edge (com.baidu.hugegraph.structure.graph.Edge)2 Vertex (com.baidu.hugegraph.structure.graph.Vertex)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 Test (org.junit.Test)2 HugeClientBuilder (com.baidu.hugegraph.driver.HugeClientBuilder)1 ExternalException (com.baidu.hugegraph.exception.ExternalException)1 LoadException (com.baidu.hugegraph.loader.exception.LoadException)1 Result (com.baidu.hugegraph.structure.gremlin.Result)1 FileInputStream (java.io.FileInputStream)1