Search in sources :

Example 6 with AMQMessageHeader

use of org.apache.qpid.server.message.AMQMessageHeader in project qpid-broker-j by apache.

the class PropertyConverter_Internal_to_v1_0Test method testDurableTrueConversion.

public void testDurableTrueConversion() {
    final AMQMessageHeader header = mock(AMQMessageHeader.class);
    InternalMessage originalMessage = createTestMessage(header, null, true);
    Message_1_0 convertedMessage = _messageConverter.convert(originalMessage, _addressSpace);
    assertTrue("Unexpected persistence of message", convertedMessage.isPersistent());
    assertTrue("Unexpected persistence of meta data", convertedMessage.getStoredMessage().getMetaData().isPersistent());
}
Also used : InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) AMQMessageHeader(org.apache.qpid.server.message.AMQMessageHeader)

Example 7 with AMQMessageHeader

use of org.apache.qpid.server.message.AMQMessageHeader in project qpid-broker-j by apache.

the class PropertyConverter_Internal_to_v1_0Test method testHeadersConversion.

public void testHeadersConversion() {
    final Map<String, Object> properties = new HashMap<>();
    properties.put("testProperty1", "testProperty1Value");
    properties.put("intProperty", 1);
    final AMQMessageHeader header = mock(AMQMessageHeader.class);
    when(header.getHeaderNames()).thenReturn(properties.keySet());
    doAnswer(invocation -> {
        final String originalArgument = (String) (invocation.getArguments())[0];
        return properties.get(originalArgument);
    }).when(header).getHeader(any(String.class));
    InternalMessage originalMessage = createTestMessage(header);
    final Message_1_0 convertedMessage = _messageConverter.convert(originalMessage, _addressSpace);
    Map<String, Object> convertedHeaders = convertedMessage.getApplicationPropertiesSection().getValue();
    assertEquals("Unexpected application properties", properties, new HashMap<>(convertedHeaders));
}
Also used : InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) HashMap(java.util.HashMap) AMQMessageHeader(org.apache.qpid.server.message.AMQMessageHeader)

Example 8 with AMQMessageHeader

use of org.apache.qpid.server.message.AMQMessageHeader in project qpid-broker-j by apache.

the class PropertyConverter_Internal_to_v1_0Test method testMessageIdStringConversion.

public void testMessageIdStringConversion() {
    final String messageId = "testMessageId";
    final AMQMessageHeader header = mock(AMQMessageHeader.class);
    when(header.getMessageId()).thenReturn(messageId);
    InternalMessage originalMessage = createTestMessage(header);
    Message_1_0 convertedMessage = _messageConverter.convert(originalMessage, _addressSpace);
    Object convertedMessageId = MessageConverter_from_1_0.getMessageId(convertedMessage);
    assertEquals("Unexpected messageId", messageId, convertedMessageId);
}
Also used : InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) AMQMessageHeader(org.apache.qpid.server.message.AMQMessageHeader)

Example 9 with AMQMessageHeader

use of org.apache.qpid.server.message.AMQMessageHeader in project qpid-broker-j by apache.

the class AbstractVirtualHost method publishMessage.

@Override
public int publishMessage(@Param(name = "message") final ManageableMessage message) {
    final String address = message.getAddress();
    MessageDestination destination = address == null ? getDefaultDestination() : getAttainedMessageDestination(address);
    if (destination == null) {
        destination = getDefaultDestination();
    }
    final AMQMessageHeader header = new MessageHeaderImpl(message);
    Serializable body = null;
    Object messageContent = message.getContent();
    if (messageContent != null) {
        if (messageContent instanceof Map || messageContent instanceof List) {
            if (message.getMimeType() != null || message.getEncoding() != null) {
                throw new IllegalArgumentException("If the message content is provided as map or list, the mime type and encoding must be left unset");
            }
            body = (Serializable) messageContent;
        } else if (messageContent instanceof String) {
            String contentTransferEncoding = message.getContentTransferEncoding();
            if ("base64".equalsIgnoreCase(contentTransferEncoding)) {
                body = Strings.decodeBase64((String) messageContent);
            } else if (contentTransferEncoding == null || contentTransferEncoding.trim().equals("") || contentTransferEncoding.trim().equalsIgnoreCase("identity")) {
                String mimeType = message.getMimeType();
                if (mimeType != null && !(mimeType = mimeType.trim().toLowerCase()).equals("")) {
                    if (!(mimeType.startsWith("text/") || Arrays.asList("application/json", "application/xml").contains(mimeType))) {
                        throw new IllegalArgumentException(message.getMimeType() + " is invalid as a MIME type for this message. " + "Only MIME types of the text type can be used if a string is supplied as the content");
                    } else if (mimeType.matches(".*;\\s*charset\\s*=.*")) {
                        throw new IllegalArgumentException(message.getMimeType() + " is invalid as a MIME type for this message. " + "If a string is supplied as the content, the MIME type must not include a charset parameter");
                    }
                }
                body = (String) messageContent;
            } else {
                throw new IllegalArgumentException("contentTransferEncoding value '" + contentTransferEncoding + "' is invalid.  The only valid values are base64 and identity");
            }
        } else {
            throw new IllegalArgumentException("The message content (if present) can only be a string, map or list");
        }
    }
    InternalMessage internalMessage = InternalMessage.createMessage(getMessageStore(), header, body, message.isPersistent(), address);
    AutoCommitTransaction txn = new AutoCommitTransaction(getMessageStore());
    final InstanceProperties instanceProperties = new InstanceProperties() {

        @Override
        public Object getProperty(final Property prop) {
            switch(prop) {
                case EXPIRATION:
                    Date expiration = message.getExpiration();
                    return expiration == null ? 0 : expiration.getTime();
                case IMMEDIATE:
                    return false;
                case PERSISTENT:
                    return message.isPersistent();
                case MANDATORY:
                    return false;
                case REDELIVERED:
                    return false;
                default:
                    return null;
            }
        }
    };
    final RoutingResult<InternalMessage> result = destination.route(internalMessage, address, instanceProperties);
    return result.send(txn, null);
}
Also used : AutoCommitTransaction(org.apache.qpid.server.txn.AutoCommitTransaction) Serializable(java.io.Serializable) MessageDestination(org.apache.qpid.server.message.MessageDestination) InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) InstanceProperties(org.apache.qpid.server.message.InstanceProperties) AMQMessageHeader(org.apache.qpid.server.message.AMQMessageHeader) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Collections.newSetFromMap(java.util.Collections.newSetFromMap)

Example 10 with AMQMessageHeader

use of org.apache.qpid.server.message.AMQMessageHeader in project qpid-broker-j by apache.

the class DirectExchangeTest method createTestMessage.

private ServerMessage<?> createTestMessage(Map<String, Object> headerValues) {
    AMQMessageHeader header = mock(AMQMessageHeader.class);
    headerValues.forEach((key, value) -> when(header.getHeader(key)).thenReturn(value));
    @SuppressWarnings("unchecked") ServerMessage<?> message = mock(ServerMessage.class);
    when(message.isResourceAcceptable(any(TransactionLogResource.class))).thenReturn(true);
    when(message.getMessageHeader()).thenReturn(header);
    return message;
}
Also used : AMQMessageHeader(org.apache.qpid.server.message.AMQMessageHeader) TransactionLogResource(org.apache.qpid.server.store.TransactionLogResource)

Aggregations

AMQMessageHeader (org.apache.qpid.server.message.AMQMessageHeader)81 InternalMessage (org.apache.qpid.server.message.internal.InternalMessage)55 Matchers.anyString (org.mockito.Matchers.anyString)28 ServerMessage (org.apache.qpid.server.message.ServerMessage)16 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)15 TransactionLogResource (org.apache.qpid.server.store.TransactionLogResource)11 HashMap (java.util.HashMap)10 MessageConversionException (org.apache.qpid.server.protocol.converter.MessageConversionException)8 MessageReference (org.apache.qpid.server.message.MessageReference)5 Queue (org.apache.qpid.server.model.Queue)5 ReplyTo (org.apache.qpid.server.protocol.v0_10.transport.ReplyTo)5 Exchange (org.apache.qpid.server.model.Exchange)4 UUID (java.util.UUID)3 UnsignedLong (org.apache.qpid.server.protocol.v1_0.type.UnsignedLong)3 Date (java.util.Date)2 Set (java.util.Set)2 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)2 InternalMessageHeader (org.apache.qpid.server.message.internal.InternalMessageHeader)2 StoredMessage (org.apache.qpid.server.store.StoredMessage)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2