Search in sources :

Example 1 with ObjectToMimeContentConverter

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

the class MessageConverter_Internal_to_v0_10 method convertToStoredMessage.

private StoredMessage<MessageMetaData_0_10> convertToStoredMessage(final InternalMessage serverMsg, final NamedAddressSpace addressSpace) {
    Object messageBody = serverMsg.getMessageBody();
    ObjectToMimeContentConverter converter = MimeContentConverterRegistry.getBestFitObjectToMimeContentConverter(messageBody);
    final byte[] messageContent = converter == null ? new byte[] {} : converter.toMimeContent(messageBody);
    String mimeType = converter == null ? null : converter.getMimeType();
    mimeType = improveMimeType(serverMsg, mimeType);
    final MessageMetaData_0_10 messageMetaData_0_10 = convertMetaData(serverMsg, addressSpace, mimeType, messageContent.length);
    final int metadataSize = messageMetaData_0_10.getStorableSize();
    return new StoredMessage<MessageMetaData_0_10>() {

        @Override
        public MessageMetaData_0_10 getMetaData() {
            return messageMetaData_0_10;
        }

        @Override
        public long getMessageNumber() {
            return serverMsg.getMessageNumber();
        }

        @Override
        public QpidByteBuffer getContent(final int offset, final int length) {
            return QpidByteBuffer.wrap(messageContent, offset, length);
        }

        @Override
        public int getContentSize() {
            return messageContent.length;
        }

        @Override
        public int getMetadataSize() {
            return metadataSize;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isInMemory() {
            return true;
        }

        @Override
        public boolean flowToDisk() {
            return false;
        }

        @Override
        public void reallocate() {
        }
    };
}
Also used : ObjectToMimeContentConverter(org.apache.qpid.server.message.mimecontentconverter.ObjectToMimeContentConverter) StoredMessage(org.apache.qpid.server.store.StoredMessage)

Example 2 with ObjectToMimeContentConverter

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

the class MessageConverter_from_1_0 method getAmqp0xConvertedContentAndMimeType.

public static ConvertedContentAndMimeType getAmqp0xConvertedContentAndMimeType(final Message_1_0 serverMsg) {
    Object bodyObject = convertBodyToObject(serverMsg);
    ObjectToMimeContentConverter converter = getBestFitObjectToMimeContentConverter(bodyObject);
    ContentHint contentHint = getAmqp0xTypeHint(serverMsg);
    Class<?> typeHint = contentHint.getContentClass();
    if (typeHint == null && bodyObject == null) {
        typeHint = Void.class;
    }
    if (converter == null) {
        converter = getBestFitObjectToMimeContentConverter(bodyObject, typeHint);
        if (converter == null) {
            throw new MessageConversionException(String.format("Could not convert message from 1.0 to 0-x because conversion of content failed." + " Could not find mime type converter for the content '%s'.", bodyObject == null ? null : bodyObject.getClass().getSimpleName()));
        }
    }
    final byte[] messageContent = converter.toMimeContent(bodyObject);
    String mimeType = converter.getMimeType();
    if (bodyObject instanceof byte[]) {
        if (Serializable.class == typeHint) {
            mimeType = "application/java-object-stream";
        } else if (String.class == typeHint) {
            mimeType = "text/plain";
        } else if ((Map.class == typeHint || List.class == typeHint) && contentHint.getContentType() != null) {
            mimeType = contentHint.getContentType();
        }
    }
    return new ConvertedContentAndMimeType(messageContent, mimeType);
}
Also used : MimeContentConverterRegistry.getBestFitObjectToMimeContentConverter(org.apache.qpid.server.message.mimecontentconverter.MimeContentConverterRegistry.getBestFitObjectToMimeContentConverter) ObjectToMimeContentConverter(org.apache.qpid.server.message.mimecontentconverter.ObjectToMimeContentConverter) MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with ObjectToMimeContentConverter

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

the class MessageConverter_Internal_to_v0_8 method convertToStoredMessage.

private StoredMessage<MessageMetaData> convertToStoredMessage(final InternalMessage serverMsg, final NamedAddressSpace addressSpace) {
    Object messageBody = serverMsg.getMessageBody();
    ObjectToMimeContentConverter converter = MimeContentConverterRegistry.getBestFitObjectToMimeContentConverter(messageBody);
    final byte[] messageContent;
    try {
        messageContent = converter == null ? new byte[] {} : converter.toMimeContent(messageBody);
    } catch (IllegalArgumentException e) {
        throw new MessageConversionException("Could not convert message from Internal to 0-8 because" + " conversion of message content failed.", e);
    }
    String mimeType = converter == null ? null : converter.getMimeType();
    mimeType = improveMimeType(serverMsg, mimeType);
    final MessageMetaData messageMetaData_0_8 = convertMetaData(serverMsg, addressSpace, mimeType, messageContent.length);
    final int metadataSize = messageMetaData_0_8.getStorableSize();
    return new StoredMessage<MessageMetaData>() {

        @Override
        public MessageMetaData getMetaData() {
            return messageMetaData_0_8;
        }

        @Override
        public long getMessageNumber() {
            return serverMsg.getMessageNumber();
        }

        @Override
        public QpidByteBuffer getContent(final int offset, final int length) {
            return QpidByteBuffer.wrap(messageContent, offset, length);
        }

        @Override
        public int getContentSize() {
            return messageContent.length;
        }

        @Override
        public int getMetadataSize() {
            return metadataSize;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isInMemory() {
            return true;
        }

        @Override
        public boolean flowToDisk() {
            return false;
        }

        @Override
        public void reallocate() {
        }
    };
}
Also used : ObjectToMimeContentConverter(org.apache.qpid.server.message.mimecontentconverter.ObjectToMimeContentConverter) MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) StoredMessage(org.apache.qpid.server.store.StoredMessage)

Aggregations

ObjectToMimeContentConverter (org.apache.qpid.server.message.mimecontentconverter.ObjectToMimeContentConverter)3 MessageConversionException (org.apache.qpid.server.protocol.converter.MessageConversionException)2 StoredMessage (org.apache.qpid.server.store.StoredMessage)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 MimeContentConverterRegistry.getBestFitObjectToMimeContentConverter (org.apache.qpid.server.message.mimecontentconverter.MimeContentConverterRegistry.getBestFitObjectToMimeContentConverter)1