Example usage for javax.jms MapMessage getBytes

List of usage examples for javax.jms MapMessage getBytes

Introduction

In this page you can find the example usage for javax.jms MapMessage getBytes.

Prototype


byte[] getBytes(String name) throws JMSException;

Source Link

Document

Returns the byte array value with the specified name.

Usage

From source file:eu.domibus.submission.transformer.impl.JMSMessageTransformer.java

/**
 * Transforms {@link javax.jms.MapMessage} to {@link eu.domibus.submission.Submission}
 *
 * @param messageIn the message ({@link javax.jms.MapMessage}) to be tranformed
 * @return the result of the transformation as {@link eu.domibus.submission.Submission}
 *///from   w w  w. ja  va2 s  .  c  om
@Override
public Submission transformToSubmission(final MapMessage messageIn) {

    final Submission target = new Submission();

    try {

        target.setAction(messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_ACTION));
        target.setService(messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_SERVICE));
        target.setServiceType(
                messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_SERVICE_TYPE));
        target.setConversationId(
                messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_CONVERSATION_ID));
        final String fromPartyID = messageIn
                .getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_FROM_PARTY_ID);
        final String fromPartyType = messageIn
                .getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_FROM_PARTY_TYPE);
        target.addFromParty(fromPartyID, fromPartyType);
        target.setFromRole(
                messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_FROM_ROLE));
        final String toPartyID = messageIn
                .getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TO_PARTY_ID);
        final String toPartyType = messageIn
                .getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TO_PARTY_TYPE);
        target.addToParty(toPartyID, toPartyType);
        target.setToRole(messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TO_ROLE));
        target.addMessageProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_ORIGINAL_SENDER,
                messageIn.getStringProperty(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_ORIGINAL_SENDER));
        target.addMessageProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_FINAL_RECIPIENT,
                messageIn.getStringProperty(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_FINAL_RECIPIENT));
        target.setRefToMessageId(
                messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_REF_TO_MESSAGE_ID));
        target.setAgreementRef(
                messageIn.getStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_AGREEMENT_REF));
        final int numPayloads = messageIn
                .getIntProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TOTAL_NUMBER_OF_PAYLOADS);

        for (int i = 1; i <= numPayloads; i++) {
            final String propPayload = String.valueOf(MessageFormat
                    .format(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_NAME_FORMAT, i));

            final String bodyloadFileName = JMSMessageTransformer.BODYLOAD_FILE_NAME_FORMAT;

            final String contentId;
            final String mimeType;
            String description = null;
            final byte[] payloadData;
            payloadData = messageIn.getBytes(propPayload);
            final String payMimeTypeProp = String.valueOf(MessageFormat
                    .format(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_MIME_TYPE_FORMAT, i));
            mimeType = messageIn.getStringProperty(payMimeTypeProp);
            final String payDescrip = String.valueOf(MessageFormat
                    .format(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_DESCRIPTION_FORMAT, i));

            if (messageIn.getStringProperty(payDescrip) != null) {
                description = messageIn.getStringProperty(payDescrip);
            }

            final String payContID = String.valueOf(MessageFormat
                    .format(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_MIME_CONTENT_ID_FORMAT, i));

            contentId = messageIn.getStringProperty(payContID);

            final Properties partProperties = new Properties();
            if (mimeType != null && !mimeType.trim().equals("")) {
                partProperties.setProperty(Property.MIME_TYPE, mimeType);
            }

            target.addPayload(contentId, payloadData, partProperties, i == 1, description, null);
        }

    } catch (final JMSException ex) {
        JMSMessageTransformer.LOG.error("Error while getting properties from MapMessage", ex);
        throw new RuntimeException(ex);
    }

    return target;

}

From source file:org.mule.transport.jms.JmsMessageUtilsTestCase.java

/**
 * Tests that is able to convert a Map which only contains simple values into a
 * MapMessage./* www  .j a  va  2 s. c  o m*/
 */
@Test
public void testConvertsValidMapWithSimpleValuesToMapMessage() throws JMSException {
    Session session = mock(Session.class);
    when(session.createMapMessage()).thenReturn(new ActiveMQMapMessage());

    // Creates a test Map with data
    Map data = new HashMap();
    data.put("value1", new Float(4));
    data.put("value2", new byte[] { 1, 2, 3 });
    data.put("value3", "value3");
    data.put("value4", new Double(67.9));
    data.put("value5", true);
    data.put("value6", null);

    Message message = JmsMessageUtils.toMessage(data, session);
    assertTrue(message instanceof MapMessage);

    MapMessage mapMessage = (MapMessage) message;
    assertEquals(new Float(4), mapMessage.getFloat("value1"), 0);
    assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, mapMessage.getBytes("value2")));
    assertEquals("value3", mapMessage.getString("value3"));
    assertEquals(new Double(67.9), mapMessage.getDouble("value4"), 0);
    assertTrue(mapMessage.getBoolean("value5"));
    assertNull(mapMessage.getObject("value6"));
}