Search in sources :

Example 6 with Session

use of ch.ethz.ssh2.Session in project neo4j by neo4j.

the class BoltCausalClusteringIT method sessionShouldExpireOnFailingReadQuery.

@Test
public void sessionShouldExpireOnFailingReadQuery() throws Exception {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
    CoreClusterMember coreServer = cluster.getCoreMemberById(0);
    Driver driver = GraphDatabase.driver(coreServer.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
    inExpirableSession(driver, Driver::session, (session) -> {
        session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
        return null;
    });
    try (Session readSession = driver.session(AccessMode.READ)) {
        // when
        connectedServer(readSession).shutdown();
        // then
        readSession.run("MATCH (n) RETURN n LIMIT 1").consume();
        fail("Should have thrown an exception as the read replica went away mid query");
    } catch (SessionExpiredException sep) {
        // then
        assertThat(sep.getMessage(), containsString("is no longer available"));
    } finally {
        driver.close();
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 7 with Session

use of ch.ethz.ssh2.Session in project neo4j by neo4j.

the class BoltCausalClusteringIT method shouldNotBeAbleToWriteOnAReadSession.

@Test
public void shouldNotBeAbleToWriteOnAReadSession() throws Exception {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(0).startCluster();
    assertEventually("Failed to execute write query on read server", () -> {
        switchLeader(cluster.awaitLeader());
        CoreClusterMember leader = cluster.awaitLeader();
        Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
        try (Session session = driver.session(AccessMode.READ)) {
            // when
            session.run("CREATE (n:Person {name: 'Jim'})").consume();
            return false;
        } catch (ClientException ex) {
            assertEquals("Write queries cannot be performed in READ access mode.", ex.getMessage());
            return true;
        } finally {
            driver.close();
        }
    }, is(true), 30, SECONDS);
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) ClientException(org.neo4j.driver.v1.exceptions.ClientException) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 8 with Session

use of ch.ethz.ssh2.Session in project jstorm by alibaba.

the class MyScpClient method get.

/**
     * Download a set of files from the remote server to a local directory.
     *
     * @param remoteFiles          Paths and names of the remote files.
     * @param localTargetDirectory Local directory to put the downloaded files.
     * @throws IOException
     */
public void get(String[] remoteFiles, String localTargetDirectory) throws IOException {
    Session sess = null;
    if ((remoteFiles == null) || (localTargetDirectory == null))
        throw new IllegalArgumentException("Null argument.");
    if (remoteFiles.length == 0)
        return;
    //        String cmd = "scp -f -r";
    String cmd = "scp -f ";
    for (int i = 0; i < remoteFiles.length; i++) {
        if (remoteFiles[i] == null)
            throw new IllegalArgumentException("Cannot accept null filename.");
        cmd += (" " + remoteFiles[i]);
    }
    try {
        sess = conn.openSession();
        sess.execCommand(cmd);
        receiveFiles(sess, remoteFiles, localTargetDirectory);
        sess.close();
    } catch (IOException e) {
        if (sess != null)
            sess.close();
        throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
    }
}
Also used : IOException(java.io.IOException) Session(ch.ethz.ssh2.Session)

Example 9 with Session

use of ch.ethz.ssh2.Session in project jstorm by alibaba.

the class MyScpClient method put.

/**
     * Create a remote file and copy the contents of the passed byte array into it.
     * The method use the specified mode when creating the file on the remote side.
     *
     * @param data                  the data to be copied into the remote file.
     * @param remoteFileName        The name of the file which will be created in the remote target directory.
     * @param remoteTargetDirectory Remote target directory.
     * @param mode                  a four digit string (e.g., 0644, see "man chmod", "man open")
     * @throws IOException
     */
public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode) throws IOException {
    Session sess = null;
    if ((remoteFileName == null) || (remoteTargetDirectory == null) || (mode == null))
        throw new IllegalArgumentException("Null argument.");
    if (mode.length() != 4)
        throw new IllegalArgumentException("Invalid mode.");
    for (int i = 0; i < mode.length(); i++) if (Character.isDigit(mode.charAt(i)) == false)
        throw new IllegalArgumentException("Invalid mode.");
    String cmd = "scp -t -d " + remoteTargetDirectory;
    try {
        sess = conn.openSession();
        sess.execCommand(cmd);
        sendBytes(sess, data, remoteFileName, mode);
        sess.close();
    } catch (IOException e) {
        if (sess != null)
            sess.close();
        throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
    }
}
Also used : IOException(java.io.IOException) Session(ch.ethz.ssh2.Session)

Example 10 with Session

use of ch.ethz.ssh2.Session in project jstorm by alibaba.

the class MyScpClient method put.

/**
     * Copy a set of local files to a remote directory, uses the specified mode
     * when creating the files on the remote side.
     *
     * @param localFiles            Paths and names of the local files.
     * @param remoteTargetDirectory Remote target directory.
     * @param mode                  a four digit string (e.g., 0644, see "man chmod", "man open")
     * @throws IOException
     */
public void put(String[] localFiles, String remoteTargetDirectory, String mode) throws IOException {
    Session sess = null;
    if ((localFiles == null) || (remoteTargetDirectory == null) || (mode == null))
        throw new IllegalArgumentException("Null argument.");
    if (mode.length() != 4)
        throw new IllegalArgumentException("Invalid mode.");
    for (int i = 0; i < mode.length(); i++) if (Character.isDigit(mode.charAt(i)) == false)
        throw new IllegalArgumentException("Invalid mode.");
    if (localFiles.length == 0)
        return;
    String cmd = "scp -t -d " + remoteTargetDirectory;
    for (int i = 0; i < localFiles.length; i++) {
        if (localFiles[i] == null)
            throw new IllegalArgumentException("Cannot accept null filename.");
    }
    try {
        sess = conn.openSession();
        sess.execCommand(cmd);
        sendFiles(sess, localFiles, mode);
        sess.close();
    } catch (IOException e) {
        if (sess != null)
            sess.close();
        throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
    }
}
Also used : IOException(java.io.IOException) Session(ch.ethz.ssh2.Session)

Aggregations

Session (com.trilead.ssh2.Session)35 Connection (com.trilead.ssh2.Connection)31 IOException (java.io.IOException)25 InputStream (java.io.InputStream)24 Session (org.neo4j.driver.v1.Session)16 Test (org.junit.Test)15 Driver (org.neo4j.driver.v1.Driver)14 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)9 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HttpException (org.apache.commons.httpclient.HttpException)8 SCPClient (com.trilead.ssh2.SCPClient)6 Session (ch.ethz.ssh2.Session)5 StreamGobbler (com.trilead.ssh2.StreamGobbler)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 Transaction (org.neo4j.driver.v1.Transaction)5 Record (org.neo4j.driver.v1.Record)4 SessionExpiredException (org.neo4j.driver.v1.exceptions.SessionExpiredException)4 BufferedReader (java.io.BufferedReader)3