Search in sources :

Example 41 with ExternalException

use of com.baidu.hugegraph.exception.ExternalException in project incubator-hugegraph-toolchain by apache.

the class GraphConnectionController method delete.

@DeleteMapping("{id}")
public GraphConnection delete(@PathVariable("id") int id) {
    GraphConnection oldEntity = this.connService.get(id);
    if (oldEntity == null) {
        throw new ExternalException("graph-connection.not-exist.id", id);
    }
    this.connService.remove(id);
    this.poolService.remove(oldEntity);
    return oldEntity;
}
Also used : GraphConnection(com.baidu.hugegraph.entity.GraphConnection) ExternalException(com.baidu.hugegraph.exception.ExternalException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Example 42 with ExternalException

use of com.baidu.hugegraph.exception.ExternalException 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)

Aggregations

ExternalException (com.baidu.hugegraph.exception.ExternalException)42 HugeClient (com.baidu.hugegraph.driver.HugeClient)14 FileMapping (com.baidu.hugegraph.entity.load.FileMapping)11 ServerException (com.baidu.hugegraph.exception.ServerException)11 GraphConnection (com.baidu.hugegraph.entity.GraphConnection)9 JobManager (com.baidu.hugegraph.entity.load.JobManager)9 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)9 IndexLabel (com.baidu.hugegraph.structure.schema.IndexLabel)8 PostMapping (org.springframework.web.bind.annotation.PostMapping)8 LoadTask (com.baidu.hugegraph.entity.load.LoadTask)5 VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)5 PutMapping (org.springframework.web.bind.annotation.PutMapping)5 EdgeLabel (com.baidu.hugegraph.structure.schema.EdgeLabel)4 PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)4 InternalException (com.baidu.hugegraph.exception.InternalException)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 EdgeMapping (com.baidu.hugegraph.entity.load.EdgeMapping)2 VertexMapping (com.baidu.hugegraph.entity.load.VertexMapping)2 GremlinCollection (com.baidu.hugegraph.entity.query.GremlinCollection)2 ClientException (com.baidu.hugegraph.rest.ClientException)2