Search in sources :

Example 1 with HugeClientBuilder

use of com.baidu.hugegraph.driver.HugeClientBuilder 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)

Aggregations

HugeClientBuilder (com.baidu.hugegraph.driver.HugeClientBuilder)1 ServerException (com.baidu.hugegraph.exception.ServerException)1 LoadException (com.baidu.hugegraph.loader.exception.LoadException)1 ClientException (com.baidu.hugegraph.rest.ClientException)1