Search in sources :

Example 1 with ClientConnectionSecurityException

use of org.apache.qpid.protonj2.client.exceptions.ClientConnectionSecurityException in project qpid-protonj2 by apache.

the class ClientExceptionSupport method convertToConnectionClosedException.

/**
 * Given an ErrorCondition instance create a new Exception that best matches
 * the error type that indicates the connection creation failed for some reason.
 *
 * @param errorCondition
 *      The ErrorCondition returned from the remote peer.
 *
 * @return a new Exception instance that best matches the ErrorCondition value.
 */
public static ClientConnectionRemotelyClosedException convertToConnectionClosedException(ErrorCondition errorCondition) {
    final ClientConnectionRemotelyClosedException remoteError;
    if (errorCondition != null && errorCondition.getCondition() != null) {
        Symbol error = errorCondition.getCondition();
        String message = extractErrorMessage(errorCondition);
        if (error.equals(AmqpError.UNAUTHORIZED_ACCESS)) {
            remoteError = new ClientConnectionSecurityException(message, new ClientErrorCondition(errorCondition));
        } else if (error.equals(ConnectionError.REDIRECT)) {
            remoteError = createConnectionRedirectException(error, message, errorCondition);
        } else {
            remoteError = new ClientConnectionRemotelyClosedException(message, new ClientErrorCondition(errorCondition));
        }
    } else {
        remoteError = new ClientConnectionRemotelyClosedException("Unknown error from remote peer");
    }
    return remoteError;
}
Also used : ClientConnectionSecurityException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionSecurityException) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) Symbol(org.apache.qpid.protonj2.types.Symbol)

Example 2 with ClientConnectionSecurityException

use of org.apache.qpid.protonj2.client.exceptions.ClientConnectionSecurityException in project qpid-protonj2 by apache.

the class ReconnectTest method testConnectThrowsSecurityViolationOnFailureFromOpen.

@Test
public void testConnectThrowsSecurityViolationOnFailureFromOpen() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().reject(AmqpError.UNAUTHORIZED_ACCESS.toString(), "Anonymous connections not allowed");
        // Could arrive if remote open response not processed in time
        peer.expectBegin().optional();
        peer.expectClose();
        peer.start();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        URI remoteURI = peer.getServerURI();
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        try {
            connection.openFuture().get();
        } catch (ExecutionException exe) {
            // Possible based on time of rejecting open arrival.
            assertTrue(exe.getCause() instanceof ClientConnectionSecurityException);
        }
        try {
            connection.defaultSession().openFuture().get();
            fail("Should fail connection since remote rejected open with auth error");
        } catch (ClientConnectionSecurityException cliEx) {
        } catch (ExecutionException exe) {
            assertTrue(exe.getCause() instanceof ClientConnectionSecurityException);
        }
        connection.close();
        try {
            connection.defaultSession();
            fail("Should fail as illegal state as connection was closed.");
        } catch (ClientIllegalStateException exe) {
        }
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientConnectionSecurityException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionSecurityException) Connection(org.apache.qpid.protonj2.client.Connection) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

ClientConnectionSecurityException (org.apache.qpid.protonj2.client.exceptions.ClientConnectionSecurityException)2 URI (java.net.URI)1 ExecutionException (java.util.concurrent.ExecutionException)1 Client (org.apache.qpid.protonj2.client.Client)1 Connection (org.apache.qpid.protonj2.client.Connection)1 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)1 ClientConnectionRemotelyClosedException (org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException)1 ClientIllegalStateException (org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException)1 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)1 Symbol (org.apache.qpid.protonj2.types.Symbol)1 Test (org.junit.jupiter.api.Test)1