Search in sources :

Example 51 with Session

use of iaik.pkcs.pkcs11.Session in project rdf2neo by Rothamsted.

the class Neo4jDataManager method runCypher.

/**
 * <p>Runs a Cypher gommands against the current {@link #getNeo4jDriver()}.</p>
 *
 * <p>The keyVals parameter is passed to {@link Values#parameters(Object...)}.</p>
 *
 * <p>The command is wrapped into a single transaction, which is committed within the method.</p>
 *
 * <p>Because parallelism sometimes raises exceptions about race conditions, we use {@link MultipleAttemptsExecutor}
 * to re-attempt the command execution a couple of times, after such exceptions.</p>
 */
public void runCypher(String cypher, Object... keyVals) {
    if (log.isTraceEnabled())
        log.trace("Cypher: {} params: {}", cypher, ArrayUtils.toString(keyVals));
    // Re-attempt a couple of times, in case of exceptions due to deadlocks over locking nodes.
    MultipleAttemptsExecutor attempter = new MultipleAttemptsExecutor(TransientException.class, DatabaseException.class, ServiceUnavailableException.class);
    attempter.setMaxAttempts(10);
    attempter.setMinPauseTime(30 * 1000);
    attempter.setMaxPauseTime(3 * 60 * 1000);
    attempter.execute(() -> {
        try (Session session = this.neo4jDriver.session()) {
            session.run(cypher, parameters(keyVals));
        }
    });
}
Also used : MultipleAttemptsExecutor(uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor) Session(org.neo4j.driver.v1.Session)

Example 52 with Session

use of iaik.pkcs.pkcs11.Session in project rdf2neo by Rothamsted.

the class CypherHandlersIT method testNodes.

/**
 * Test {@link CyNodeLoadingHandler} to see if nodes are mapped from RDF and loaded into Neo4J
 */
@Test
public void testNodes() throws Exception {
    try (Driver neoDriver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "test"))) {
        Session session = neoDriver.session(AccessMode.READ);
        StatementResult cursor = session.run("MATCH ( n:TestNode ) RETURN COUNT ( n ) AS ct");
        Assert.assertEquals("Wrong count for TestNode", 2, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH ( n:TestNode { iri:'" + iri("ex:2") + "'} ) RETURN properties ( n ) AS props");
        assertTrue("ex:2 not returned!", cursor.hasNext());
        Map<String, Object> map = cursor.next().get("props").asMap();
        assertEquals("Wrong property!", "another string", map.get("attrib3"));
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Driver(org.neo4j.driver.v1.Driver) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 53 with Session

use of iaik.pkcs.pkcs11.Session in project cypher-for-gremlin by opencypher.

the class GremlinNeo4jDriverTest method returnPath.

@Test
public void returnPath() {
    Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort());
    try (Session session = driver.session()) {
        StatementResult setup = session.run("CREATE (n1:Person {name: 'Anders'})-[r:knows]->(n2:Person)" + "RETURN n1,r,n2");
        Record createdNodes = setup.single();
        Node n1 = createdNodes.get("n1").asNode();
        Node n2 = createdNodes.get("n2").asNode();
        Relationship r = createdNodes.get("r").asRelationship();
        StatementResult result = session.run("MATCH p =(b1 { name: 'Anders' })-->()" + "RETURN p");
        Path path = result.single().get("p").asPath();
        assertThat(path.contains(n1)).isTrue();
        assertThat(path.contains(n2)).isTrue();
        assertThat(path.contains(r)).isTrue();
        assertThat(path.relationships()).hasSize(1);
        assertThat(path.nodes()).hasSize(2);
    }
}
Also used : Path(org.neo4j.driver.v1.types.Path) StatementResult(org.neo4j.driver.v1.StatementResult) Node(org.neo4j.driver.v1.types.Node) Relationship(org.neo4j.driver.v1.types.Relationship) Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 54 with Session

use of iaik.pkcs.pkcs11.Session in project nimbus by nimbus-org.

the class SCPClientImpl method mget.

public File[] mget(String remote, String localDir) throws SCPException {
    if (remote == null || remote.length() == 0) {
        throw new SCPException("Path is null.");
    }
    if (localDir == null) {
        localDir = ".";
    }
    File localDirFile = new File(localDir);
    if (homeDir != null && !localDirFile.isAbsolute()) {
        localDirFile = new File(homeDir, localDir);
    }
    List localFiles = new ArrayList();
    Session session = null;
    final String cmd = "scp -f " + remote;
    File localFile = null;
    try {
        session = connection.openSession();
        session.execCommand(cmd);
        byte[] buf = new byte[1024];
        OutputStream os = new BufferedOutputStream(session.getStdin(), 512);
        InputStream is = new BufferedInputStream(session.getStdout(), 40000);
        os.write(0);
        os.flush();
        while (true) {
            int c = checkAck(is);
            if (c == -1) {
                if (localFiles.size() == 0) {
                    throw new IOException("Remote SCP terminated unexpectedly.");
                }
                break;
            }
            if (c != 'C') {
                break;
            }
            is.read(buf, 0, 5);
            long fileSize = 0L;
            while (true) {
                if (is.read(buf, 0, 1) < 0) {
                    throw new SCPException("Unexpected EOF.");
                }
                if (buf[0] == ' ') {
                    break;
                }
                fileSize = fileSize * 10L + (long) (buf[0] - '0');
            }
            String fileName = null;
            for (int i = 0; ; i++) {
                is.read(buf, i, 1);
                if (buf[i] == (byte) 0x0a) {
                    fileName = new String(buf, 0, i);
                    break;
                }
            }
            buf[0] = 0;
            os.write(buf, 0, 1);
            os.flush();
            localFile = new File(localDirFile, fileName);
            localFiles.add(localFile);
            FileOutputStream fos = new FileOutputStream(localFile);
            try {
                int readLen = 0;
                while (true) {
                    if (buf.length < fileSize) {
                        readLen = buf.length;
                    } else {
                        readLen = (int) fileSize;
                    }
                    readLen = is.read(buf, 0, readLen);
                    if (readLen < 0) {
                        throw new SCPException("Unexpected EOF.");
                    }
                    fos.write(buf, 0, readLen);
                    fileSize -= readLen;
                    if (fileSize == 0L) {
                        break;
                    }
                }
            } finally {
                fos.close();
                fos = null;
            }
            checkAck(is);
            localFile = null;
            buf[0] = 0;
            os.write(buf, 0, 1);
            os.flush();
        }
    } catch (IOException e) {
        throw new SCPException("It failed to mget! from=" + remote + ", to=" + (localFile == null ? localDirFile : localFile), e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return (File[]) localFiles.toArray(new File[localFiles.size()]);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SCPException(jp.ossc.nimbus.service.scp.SCPException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) BufferedOutputStream(java.io.BufferedOutputStream) Session(ch.ethz.ssh2.Session)

Example 55 with Session

use of iaik.pkcs.pkcs11.Session in project cosmic by MissionCriticalCloud.

the class SshHelperTest method canEndTheSshConnectionTest.

@Test
public void canEndTheSshConnectionTest() throws Exception {
    PowerMockito.spy(SshHelper.class);
    final Session mockedSession = Mockito.mock(Session.class);
    PowerMockito.doReturn(true).when(SshHelper.class, "isChannelConditionEof", Mockito.anyInt());
    Mockito.when(mockedSession.waitForCondition(ChannelCondition.EXIT_STATUS, 1l)).thenReturn(0);
    PowerMockito.doNothing().when(SshHelper.class, "throwSshExceptionIfConditionsTimeout", Mockito.anyInt());
    SshHelper.canEndTheSshConnection(1, mockedSession, 0);
    PowerMockito.verifyStatic();
    SshHelper.isChannelConditionEof(Mockito.anyInt());
    SshHelper.throwSshExceptionIfConditionsTimeout(Mockito.anyInt());
    Mockito.verify(mockedSession).waitForCondition(ChannelCondition.EXIT_STATUS, 1l);
}
Also used : Session(com.trilead.ssh2.Session) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Session (com.trilead.ssh2.Session)42 Session (org.neo4j.driver.v1.Session)38 Connection (com.trilead.ssh2.Connection)32 IOException (java.io.IOException)29 Test (org.junit.Test)29 InputStream (java.io.InputStream)27 Driver (org.neo4j.driver.v1.Driver)27 StatementResult (org.neo4j.driver.v1.StatementResult)20 TokenException (iaik.pkcs.pkcs11.TokenException)15 P11TokenException (org.xipki.security.exception.P11TokenException)15 Record (org.neo4j.driver.v1.Record)12 Session (iaik.pkcs.pkcs11.Session)10 SecretKey (iaik.pkcs.pkcs11.objects.SecretKey)10 ValuedSecretKey (iaik.pkcs.pkcs11.objects.ValuedSecretKey)10 DSAPrivateKey (iaik.pkcs.pkcs11.objects.DSAPrivateKey)9 ECPrivateKey (iaik.pkcs.pkcs11.objects.ECPrivateKey)9 PrivateKey (iaik.pkcs.pkcs11.objects.PrivateKey)9 RSAPrivateKey (iaik.pkcs.pkcs11.objects.RSAPrivateKey)9 SM2PrivateKey (iaik.pkcs.pkcs11.objects.SM2PrivateKey)9 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)9