Search in sources :

Example 11 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project activemq-artemis by apache.

the class AMQPSessionCallback method serverSend.

private void serverSend(final Transaction transaction, final Message message, final Delivery delivery, final Receiver receiver) throws Exception {
    message.setConnectionID(receiver.getSession().getConnection().getRemoteContainer());
    invokeIncoming((AMQPMessage) message, (ActiveMQProtonRemotingConnection) transportConnection.getProtocolConnection());
    serverSession.send(transaction, message, false, false);
    afterIO(new IOCallback() {

        @Override
        public void done() {
            connection.lock();
            try {
                if (delivery.getRemoteState() instanceof TransactionalState) {
                    TransactionalState txAccepted = new TransactionalState();
                    txAccepted.setOutcome(Accepted.getInstance());
                    txAccepted.setTxnId(((TransactionalState) delivery.getRemoteState()).getTxnId());
                    delivery.disposition(txAccepted);
                } else {
                    delivery.disposition(Accepted.getInstance());
                }
                delivery.settle();
            } finally {
                connection.unlock();
            }
            connection.flush();
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            connection.lock();
            try {
                receiver.setCondition(new ErrorCondition(AmqpError.ILLEGAL_STATE, errorCode + ":" + errorMessage));
                connection.flush();
            } finally {
                connection.unlock();
            }
        }
    });
}
Also used : ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState)

Example 12 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project activemq-artemis by apache.

the class ActiveMQProtonRemotingConnection method disconnect.

@Override
public void disconnect(boolean criticalError) {
    ErrorCondition errorCondition = new ErrorCondition();
    errorCondition.setCondition(AmqpSupport.CONNECTION_FORCED);
    amqpConnection.close(errorCondition);
    getTransportConnection().close();
}
Also used : ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition)

Example 13 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project activemq-artemis by apache.

the class ProtonServerReceiverContext method onMessage.

/*
   * called when Proton receives a message to be delivered via a Delivery.
   *
   * This may be called more than once per deliver so we have to cache the buffer until we have received it all.
   *
   * */
@Override
public void onMessage(Delivery delivery) throws ActiveMQAMQPException {
    Receiver receiver;
    try {
        if (!delivery.isReadable()) {
            return;
        }
        if (delivery.isPartial()) {
            return;
        }
        receiver = ((Receiver) delivery.getLink());
        Transaction tx = null;
        byte[] data;
        data = new byte[delivery.available()];
        receiver.recv(data, 0, data.length);
        receiver.advance();
        if (delivery.getRemoteState() instanceof TransactionalState) {
            TransactionalState txState = (TransactionalState) delivery.getRemoteState();
            tx = this.sessionSPI.getTransaction(txState.getTxnId(), false);
        }
        sessionSPI.serverSend(this, tx, receiver, delivery, address, delivery.getMessageFormat(), data);
        flow(amqpCredits, minCreditRefresh);
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
        Rejected rejected = new Rejected();
        ErrorCondition condition = new ErrorCondition();
        if (e instanceof ActiveMQSecurityException) {
            condition.setCondition(AmqpError.UNAUTHORIZED_ACCESS);
        } else {
            condition.setCondition(Symbol.valueOf("failed"));
        }
        condition.setDescription(e.getMessage());
        rejected.setError(condition);
        connection.lock();
        try {
            delivery.disposition(rejected);
            delivery.settle();
        } finally {
            connection.unlock();
        }
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Receiver(org.apache.qpid.proton.engine.Receiver) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) ActiveMQAMQPInternalErrorException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPInternalErrorException) ActiveMQAMQPNotFoundException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPNotFoundException) ActiveMQAMQPException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState)

Example 14 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project activemq-artemis by apache.

the class ProtonHandler method dispatch.

private void dispatch() {
    Event ev;
    lock.lock();
    try {
        if (inDispatch) {
            // Avoid recursion from events
            return;
        }
        try {
            inDispatch = true;
            while ((ev = collector.peek()) != null) {
                for (EventHandler h : handlers) {
                    if (log.isTraceEnabled()) {
                        log.trace("Handling " + ev + " towards " + h);
                    }
                    try {
                        Events.dispatch(ev, h);
                    } catch (Exception e) {
                        log.warn(e.getMessage(), e);
                        ErrorCondition error = new ErrorCondition();
                        error.setCondition(AmqpError.INTERNAL_ERROR);
                        error.setDescription("Unrecoverable error: " + (e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()));
                        connection.setCondition(error);
                        connection.close();
                    }
                }
                collector.pop();
            }
        } finally {
            inDispatch = false;
        }
    } finally {
        lock.unlock();
    }
    flushBytes();
}
Also used : ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Event(org.apache.qpid.proton.engine.Event)

Example 15 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project activemq-artemis by apache.

the class AmqpSecurityTest method testSendMessageFailsOnAnonymousRelayWhenNotAuthorizedToSendToAddress.

@Test(timeout = 60000)
public void testSendMessageFailsOnAnonymousRelayWhenNotAuthorizedToSendToAddress() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    AmqpClient client = createAmqpClient(guestUser, guestPass);
    client.setValidator(new AmqpValidator() {

        @Override
        public void inspectDeliveryUpdate(Sender sender, Delivery delivery) {
            DeliveryState state = delivery.getRemoteState();
            if (!delivery.remotelySettled()) {
                markAsInvalid("delivery is not remotely settled");
            }
            if (state instanceof Rejected) {
                Rejected rejected = (Rejected) state;
                if (rejected.getError() == null || rejected.getError().getCondition() == null) {
                    markAsInvalid("Delivery should have been Rejected with an error condition");
                } else {
                    ErrorCondition error = rejected.getError();
                    if (!error.getCondition().equals(AmqpError.UNAUTHORIZED_ACCESS)) {
                        markAsInvalid("Should have been tagged with unauthorized access error");
                    }
                }
            } else {
                markAsInvalid("Delivery should have been Rejected");
            }
            latch.countDown();
        }
    });
    AmqpConnection connection = client.connect();
    try {
        AmqpSession session = connection.createSession();
        AmqpSender sender = session.createAnonymousSender();
        AmqpMessage message = new AmqpMessage();
        message.setAddress(getQueueName());
        message.setMessageId("msg" + 1);
        message.setText("Test-Message");
        try {
            sender.send(message);
            fail("Should not be able to send, message should be rejected");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            sender.close();
        }
        assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
        connection.getStateInspector().assertValid();
    } finally {
        connection.close();
    }
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) CountDownLatch(java.util.concurrent.CountDownLatch) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Sender(org.apache.qpid.proton.engine.Sender) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Delivery(org.apache.qpid.proton.engine.Delivery) Test(org.junit.Test)

Aggregations

ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)45 Symbol (org.apache.qpid.proton.amqp.Symbol)13 Test (org.junit.Test)11 Handler (io.vertx.core.Handler)10 ProtonConnection (io.vertx.proton.ProtonConnection)10 DeliveryState (org.apache.qpid.proton.amqp.transport.DeliveryState)10 Message (org.apache.qpid.proton.message.Message)10 Vertx (io.vertx.core.Vertx)8 Logger (io.vertx.core.impl.logging.Logger)8 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeUnit (java.util.concurrent.TimeUnit)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)8 ProtonClient (io.vertx.proton.ProtonClient)7 Map (java.util.Map)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 MockServer (io.vertx.proton.MockServer)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Proton (org.apache.qpid.proton.Proton)6