Java Code Examples for org.apache.qpid.proton.amqp.transport.ErrorCondition#setCondition()

The following examples show how to use org.apache.qpid.proton.amqp.transport.ErrorCondition#setCondition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorConditionType.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
public ErrorCondition newInstance(Object described)
{
    List l = (List) described;

    ErrorCondition o = new ErrorCondition();

    if(l.isEmpty())
    {
        throw new DecodeException("The condition field cannot be omitted");
    }

    switch(3 - l.size())
    {

        case 0:
            o.setInfo( (Map) l.get( 2 ) );
        case 1:
            o.setDescription( (String) l.get( 1 ) );
        case 2:
            o.setCondition( (Symbol) l.get( 0 ) );
    }


    return o;
}
 
Example 2
Source File: ProtonServerReceiverContext.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private Rejected createRejected(final Exception e) {
   ErrorCondition condition = new ErrorCondition();

   // Set condition
   if (e instanceof ActiveMQSecurityException) {
      condition.setCondition(AmqpError.UNAUTHORIZED_ACCESS);
   } else if (isAddressFull(e)) {
      condition.setCondition(AmqpError.RESOURCE_LIMIT_EXCEEDED);
   } else {
      condition.setCondition(Symbol.valueOf("failed"));
   }
   condition.setDescription(e.getMessage());

   Rejected rejected = new Rejected();
   rejected.setError(condition);
   return rejected;
}
 
Example 3
Source File: IOHandler.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void run(Selectable selectable) {
    Reactor reactor = selectable.getReactor();
    Transport transport = ((SelectableImpl)selectable).getTransport();
    int capacity = transport.capacity();
    if (capacity > 0) {
        SocketChannel socketChannel = (SocketChannel)selectable.getChannel();
        try {
            int n = socketChannel.read(transport.tail());
            if (n == -1) {
                transport.close_tail();
            } else {
                transport.process();
            }
        } catch (IOException | TransportException e) {
            ErrorCondition condition = new ErrorCondition();
            condition.setCondition(Symbol.getSymbol("proton:io"));
            condition.setDescription(e.getMessage());
            transport.setCondition(condition);
            transport.close_tail();
        }
    }
    // (Comment from C code:) occasionally transport events aren't
    // generated when expected, so the following hack ensures we
    // always update the selector
    update(selectable);
    reactor.update(selectable);
}
 
Example 4
Source File: IOHandler.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void run(Selectable selectable) {
    Reactor reactor = selectable.getReactor();
    Transport transport = ((SelectableImpl)selectable).getTransport();
    int pending = transport.pending();
    if (pending > 0) {
        SocketChannel channel = (SocketChannel)selectable.getChannel();
        try {
            int n = channel.write(transport.head());
            if (n < 0) {
                transport.close_head();
            } else {
                transport.pop(n);
            }
        } catch(IOException ioException) {
            ErrorCondition condition = new ErrorCondition();
            condition.setCondition(Symbol.getSymbol("proton:io"));
            condition.setDescription(ioException.getMessage());
            transport.setCondition(condition);
            transport.close_head();
        }
    }

    int newPending = transport.pending();
    if (newPending != pending) {
        update(selectable);
        reactor.update(selectable);
    }
}
 
Example 5
Source File: ActiveMQProtonRemotingConnection.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void disconnect(boolean criticalError) {
   ErrorCondition errorCondition = new ErrorCondition();
   errorCondition.setCondition(AmqpSupport.CONNECTION_FORCED);
   amqpConnection.close(errorCondition);
   // There's no need to flush, amqpConnection.close() is calling flush
   // as long this semantic is kept no need to flush here
}
 
Example 6
Source File: ProtonTransactionHandler.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private Rejected createRejected(Symbol amqpError, String message) {
   Rejected rejected = new Rejected();
   ErrorCondition condition = new ErrorCondition();
   condition.setCondition(amqpError);
   condition.setDescription(message);
   rejected.setError(condition);
   return rejected;
}
 
Example 7
Source File: ProtonHandler.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void internalHandlerError(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();
   flush();
}
 
Example 8
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorConditionSetGet() {
    // Try setting with an empty condition object, expect to get a null back per historic behaviour.
    TransportImpl transport = new TransportImpl();

    ErrorCondition emptyErrorCondition = new ErrorCondition();
    assertNull("Expected empty Condition given historic behaviour", emptyErrorCondition.getCondition());
    transport.setCondition(emptyErrorCondition);
    assertNull("Expected null ErrorCondition given historic behaviour", transport.getCondition());

    // Try setting with a populated condition object.
    transport = new TransportImpl();

    Symbol condition = Symbol.getSymbol("some-error");
    String description = "some-error-description";
    ErrorCondition populatedErrorCondition = new ErrorCondition();
    populatedErrorCondition.setCondition(condition);
    populatedErrorCondition.setDescription(description);
    assertNotNull("Expected a Condition", populatedErrorCondition.getCondition());

    transport.setCondition(populatedErrorCondition);
    assertNotNull("Expected an ErrorCondition to be returned", transport.getCondition());
    assertEquals("Unexpected ErrorCondition returned", populatedErrorCondition, transport.getCondition());

    // Try setting again with another populated condition object.
    Symbol otherCondition = Symbol.getSymbol("some-other-error");
    String otherDescription = "some-other-error-description";
    ErrorCondition otherErrorCondition = new ErrorCondition();
    otherErrorCondition.setCondition(otherCondition);
    otherErrorCondition.setDescription(otherDescription);
    assertNotNull("Expected a Condition", otherErrorCondition.getCondition());

    assertNotEquals(condition, otherCondition);
    assertNotEquals(populatedErrorCondition.getCondition(), otherErrorCondition.getCondition());
    assertNotEquals(description, otherDescription);
    assertNotEquals(populatedErrorCondition.getDescription(), otherErrorCondition.getDescription());
    assertNotEquals(populatedErrorCondition, otherErrorCondition);

    transport.setCondition(otherErrorCondition);
    assertNotNull("Expected an ErrorCondition to be returned", transport.getCondition());
    assertEquals("Unexpected ErrorCondition returned", otherErrorCondition, transport.getCondition());

    // Try setting again with an empty condition object, expect to get a null back per historic behaviour.
    transport.setCondition(emptyErrorCondition);
    assertNull("Expected null ErrorCondition given historic behaviour", transport.getCondition());
}
 
Example 9
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorConditionAfterTransportClosed() {
    Symbol condition = Symbol.getSymbol("some-error");
    String description = "some-error-description";
    ErrorCondition origErrorCondition = new ErrorCondition();
    origErrorCondition.setCondition(condition);
    origErrorCondition.setDescription(description);
    assertNotNull("Expected a Condition", origErrorCondition.getCondition());

    // Set an error condition, then call 'closed' specifying an error.
    // Expect the original condition which was set to remain.
    TransportImpl transport = new TransportImpl();

    transport.setCondition(origErrorCondition);
    transport.closed(new TransportException("my-ignored-exception"));

    assertNotNull("Expected an ErrorCondition to be returned", transport.getCondition());
    assertEquals("Unexpected ErrorCondition returned", origErrorCondition, transport.getCondition());

    // ---------------------------------------------------------------- //

    // Set an error condition, then call 'closed' without an error.
    // Expect the original condition which was set to remain.
    transport = new TransportImpl();

    transport.setCondition(origErrorCondition);
    transport.closed(null);

    assertNotNull("Expected an ErrorCondition to be returned", transport.getCondition());
    assertEquals("Unexpected ErrorCondition returned", origErrorCondition, transport.getCondition());

    // ---------------------------------------------------------------- //

    // Without having set an error condition, call 'closed' specifying an error.
    // Expect a condition to be set.
    transport = new TransportImpl();
    transport.closed(new TransportException(description));

    assertNotNull("Expected an ErrorCondition to be returned", transport.getCondition());
    assertEquals("Unexpected condition returned", ConnectionError.FRAMING_ERROR, transport.getCondition().getCondition());
    assertEquals("Unexpected description returned", "org.apache.qpid.proton.engine.TransportException: " + description, transport.getCondition().getDescription());

    // ---------------------------------------------------------------- //

    // Without having set an error condition, call 'closed' without an error.
    // Expect a condition to be set.
    transport = new TransportImpl();

    transport.closed(null);

    assertNotNull("Expected an ErrorCondition to be returned", transport.getCondition());
    assertEquals("Unexpected ErrorCondition returned", ConnectionError.FRAMING_ERROR, transport.getCondition().getCondition());
    assertEquals("Unexpected description returned", "connection aborted", transport.getCondition().getDescription());
}
 
Example 10
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCloseFrameErrorAfterDecodeError() {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);

    assertEvents(collector, Event.Type.CONNECTION_INIT, Event.Type.CONNECTION_BOUND, Event.Type.CONNECTION_LOCAL_OPEN, Event.Type.TRANSPORT);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();

    // Provide the bytes for Open, but omit the mandatory container-id to provoke a decode error.
    byte[] bytes = new byte[] {  0x00, 0x00, 0x00, 0x0C, // Frame size = 12 bytes.
                                 0x02, 0x00, 0x00, 0x00, // DOFF, TYPE, 2x CHANNEL
                                 0x00, 0x53, 0x10, 0x45};// Described-type, ulong type, open descriptor, list0.

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    assertEvents(collector, Event.Type.TRANSPORT_ERROR, Event.Type.TRANSPORT_TAIL_CLOSED);

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
    FrameBody frameBody = transport.writes.get(1);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription("The container-id field cannot be omitted");

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example 11
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidBeginProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 1, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();

    // Send the necessary response to open
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
    FrameBody frameBody = transport.writes.get(1);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example 12
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidFlowProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    Session session = connection.session();
    session.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 2, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();


    // Send the necessary response to Open/Begin
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
    FrameBody frameBody = transport.writes.get(2);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example 13
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidTransferProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    Session session = connection.session();
    session.open();

    String linkName = "myReceiver";
    Receiver receiver = session.receiver(linkName);
    receiver.flow(5);
    receiver.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Flow);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();

    // Send the necessary response to Open/Begin/Attach
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.SENDER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    FrameBody frameBody = transport.writes.get(4);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}
 
Example 14
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doInvalidDispositionProvokesDecodeErrorTestImpl(byte[] bytes, String description) {
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();

    Collector collector = Collector.Factory.create();
    connection.collect(collector);

    transport.bind(connection);
    connection.open();

    Session session = connection.session();
    session.open();

    String linkName = "mySender";
    Sender sender = session.sender(linkName);
    sender.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);

    // Provide the response bytes for the header
    transport.tail().put(AmqpHeader.HEADER);
    transport.process();

    // Send the necessary response to Open/Begin/Attach plus some credit
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.SENDER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    int credit = 1;
    Flow flow = new Flow();
    flow.setHandle(UnsignedInteger.ZERO);
    flow.setDeliveryCount(UnsignedInteger.ZERO);
    flow.setNextIncomingId(UnsignedInteger.ONE);
    flow.setNextOutgoingId(UnsignedInteger.ZERO);
    flow.setIncomingWindow(UnsignedInteger.valueOf(1024));
    flow.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    flow.setDrain(true);
    flow.setLinkCredit(UnsignedInteger.valueOf(credit));
    transport.handleFrame(new TransportFrame(0, flow, null));

    sendMessage(sender, "tag1", "content1");

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Transfer);

    int capacity = transport.capacity();
    assertTrue("Unexpected transport capacity: " + capacity, capacity > bytes.length);

    transport.tail().put(bytes);
    transport.process();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    FrameBody frameBody = transport.writes.get(4);
    assertTrue("Unexpected frame type", frameBody instanceof Close);

    // Expect the close frame generated to contain a decode error condition referencing the missing container-id.
    ErrorCondition expectedCondition = new ErrorCondition();
    expectedCondition.setCondition(AmqpError.DECODE_ERROR);
    expectedCondition.setDescription(description);

    assertEquals("Unexpected condition", expectedCondition, ((Close) frameBody).getError());
}