Search in sources :

Example 1 with StreamMessage

use of javax.jms.StreamMessage in project camel by apache.

the class JmsBinding method extractBodyFromJms.

/**
     * Extracts the body from the JMS message
     *
     * @param exchange the exchange
     * @param message  the message to extract its body
     * @return the body, can be <tt>null</tt>
     */
public Object extractBodyFromJms(Exchange exchange, Message message) {
    try {
        // if we are configured to not map the jms message then return it as body
        if (!mapJmsMessage) {
            LOG.trace("Option map JMS message is false so using JMS message as body: {}", message);
            return message;
        }
        if (message instanceof ObjectMessage) {
            LOG.trace("Extracting body as a ObjectMessage from JMS message: {}", message);
            ObjectMessage objectMessage = (ObjectMessage) message;
            Object payload = objectMessage.getObject();
            if (payload instanceof DefaultExchangeHolder) {
                DefaultExchangeHolder holder = (DefaultExchangeHolder) payload;
                DefaultExchangeHolder.unmarshal(exchange, holder);
                return exchange.getIn().getBody();
            } else {
                return objectMessage.getObject();
            }
        } else if (message instanceof TextMessage) {
            LOG.trace("Extracting body as a TextMessage from JMS message: {}", message);
            TextMessage textMessage = (TextMessage) message;
            return textMessage.getText();
        } else if (message instanceof MapMessage) {
            LOG.trace("Extracting body as a MapMessage from JMS message: {}", message);
            return createMapFromMapMessage((MapMessage) message);
        } else if (message instanceof BytesMessage) {
            LOG.trace("Extracting body as a BytesMessage from JMS message: {}", message);
            return createByteArrayFromBytesMessage((BytesMessage) message);
        } else if (message instanceof StreamMessage) {
            LOG.trace("Extracting body as a StreamMessage from JMS message: {}", message);
            return message;
        } else {
            return null;
        }
    } catch (JMSException e) {
        throw new RuntimeCamelException("Failed to extract body due to: " + e + ". Message: " + message, e);
    }
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TextMessage(javax.jms.TextMessage)

Example 2 with StreamMessage

use of javax.jms.StreamMessage in project ACS by ACS-Community.

the class DefaultPublisherImpl method createStreamMessage.

/**
   * Method createStreamMessage
   *
   *
   * @return StreamMessage
   *
   * @throws JMSException
   *
   */
public StreamMessage createStreamMessage() throws JMSException {
    if (closed) {
        throw (new JMSException("Publisher object has been closed"));
    }
    StreamMessage message = null;
    message = session.createStreamMessage();
    return message;
}
Also used : JMSException(javax.jms.JMSException) StreamMessage(javax.jms.StreamMessage)

Example 3 with StreamMessage

use of javax.jms.StreamMessage in project camel by apache.

the class JmsBinding method makeJmsMessage.

/**
     * Creates a JMS message from the Camel exchange and message
     *
     * @param exchange the current exchange
     * @param camelMessage the body to make a javax.jms.Message as
     * @param session the JMS session used to create the message
     * @param cause optional exception occurred that should be sent as reply instead of a regular body
     * @return a newly created JMS Message instance containing the
     * @throws JMSException if the message could not be created
     */
public Message makeJmsMessage(Exchange exchange, org.apache.camel.Message camelMessage, Session session, Exception cause) throws JMSException {
    Message answer = null;
    boolean alwaysCopy = endpoint != null && endpoint.getConfiguration().isAlwaysCopyMessage();
    boolean force = endpoint != null && endpoint.getConfiguration().isForceSendOriginalMessage();
    if (!alwaysCopy && camelMessage instanceof JmsMessage) {
        JmsMessage jmsMessage = (JmsMessage) camelMessage;
        if (!jmsMessage.shouldCreateNewMessage() || force) {
            answer = jmsMessage.getJmsMessage();
            if (!force) {
                // answer must match endpoint type
                JmsMessageType type = endpoint != null ? endpoint.getConfiguration().getJmsMessageType() : null;
                if (type != null && answer != null) {
                    if (type == JmsMessageType.Text) {
                        answer = answer instanceof TextMessage ? answer : null;
                    } else if (type == JmsMessageType.Bytes) {
                        answer = answer instanceof BytesMessage ? answer : null;
                    } else if (type == JmsMessageType.Map) {
                        answer = answer instanceof MapMessage ? answer : null;
                    } else if (type == JmsMessageType.Object) {
                        answer = answer instanceof ObjectMessage ? answer : null;
                    } else if (type == JmsMessageType.Stream) {
                        answer = answer instanceof StreamMessage ? answer : null;
                    }
                }
            }
        }
    }
    if (answer == null) {
        if (cause != null) {
            // an exception occurred so send it as response
            LOG.debug("Will create JmsMessage with caused exception: {}", cause);
            // create jms message containing the caused exception
            answer = createJmsMessage(cause, session);
        } else {
            ObjectHelper.notNull(camelMessage, "message");
            // create regular jms message using the camel message body
            answer = createJmsMessage(exchange, camelMessage, session, exchange.getContext());
            appendJmsProperties(answer, exchange, camelMessage);
        }
    }
    if (answer != null && messageCreatedStrategy != null) {
        messageCreatedStrategy.onMessageCreated(answer, session, exchange, null);
    }
    return answer;
}
Also used : MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) TextMessage(javax.jms.TextMessage)

Example 4 with StreamMessage

use of javax.jms.StreamMessage in project camel by apache.

the class JmsBinding method extractBodyFromJms.

/**
     * Extracts the body from the JMS message
     *
     * @param exchange the exchange
     * @param message  the message to extract its body
     * @return the body, can be <tt>null</tt>
     */
public Object extractBodyFromJms(Exchange exchange, Message message) {
    try {
        // based on message type
        if (endpoint != null && endpoint.getMessageConverter() != null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Extracting body using a custom MessageConverter: {} from JMS message: {}", endpoint.getMessageConverter(), message);
            }
            return endpoint.getMessageConverter().fromMessage(message);
        }
        // if we are configured to not map the jms message then return it as body
        if (endpoint != null && !endpoint.getConfiguration().isMapJmsMessage()) {
            LOG.trace("Option map JMS message is false so using JMS message as body: {}", message);
            return message;
        }
        if (message instanceof ObjectMessage) {
            LOG.trace("Extracting body as a ObjectMessage from JMS message: {}", message);
            ObjectMessage objectMessage = (ObjectMessage) message;
            Object payload = objectMessage.getObject();
            if (payload instanceof DefaultExchangeHolder) {
                DefaultExchangeHolder holder = (DefaultExchangeHolder) payload;
                DefaultExchangeHolder.unmarshal(exchange, holder);
                return exchange.getIn().getBody();
            } else {
                return objectMessage.getObject();
            }
        } else if (message instanceof TextMessage) {
            LOG.trace("Extracting body as a TextMessage from JMS message: {}", message);
            TextMessage textMessage = (TextMessage) message;
            return textMessage.getText();
        } else if (message instanceof MapMessage) {
            LOG.trace("Extracting body as a MapMessage from JMS message: {}", message);
            return createMapFromMapMessage((MapMessage) message);
        } else if (message instanceof BytesMessage) {
            LOG.trace("Extracting body as a BytesMessage from JMS message: {}", message);
            return createByteArrayFromBytesMessage((BytesMessage) message);
        } else if (message instanceof StreamMessage) {
            LOG.trace("Extracting body as a StreamMessage from JMS message: {}", message);
            return message;
        } else {
            return null;
        }
    } catch (JMSException e) {
        throw new RuntimeCamelException("Failed to extract body due to: " + e + ". Message: " + message, e);
    }
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) Object(org.apache.camel.component.jms.JmsMessageType.Object) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TextMessage(javax.jms.TextMessage)

Example 5 with StreamMessage

use of javax.jms.StreamMessage in project ats-framework by Axway.

the class JmsMessageVerification method getBodyHash.

private byte[] getBodyHash(final String algorithm) {
    isNotNull();
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(algorithm);
    } catch (final NoSuchAlgorithmException e) {
        throw new JmsMessageException("Failed to load " + algorithm + " algorithm: " + e);
    }
    try {
        if (actualMessage instanceof TextMessage) {
            digest.update(((TextMessage) actualMessage).getText().getBytes());
        } else if (actualMessage instanceof BytesMessage) {
            final BytesMessage m = (BytesMessage) actualMessage;
            final byte[] tmp = new byte[2048];
            int r;
            while ((r = m.readBytes(tmp)) >= 0) {
                if (r != 0) {
                    digest.update(tmp, 0, r);
                }
            }
        } else if (actualMessage instanceof StreamMessage) {
            final StreamMessage m = (StreamMessage) actualMessage;
            final byte[] tmp = new byte[2048];
            int r;
            while ((r = m.readBytes(tmp)) >= 0) {
                if (r != 0) {
                    digest.update(tmp, 0, r);
                }
            }
        } else {
            throw new JmsMessageException("Cannot determind content hash for message type : " + actualMessage.getClass());
        }
    } catch (final JMSException e) {
        throw new JmsMessageException("Failed to determine message " + algorithm + " hash", e);
    }
    return digest.digest();
}
Also used : JmsMessageException(com.axway.ats.action.exceptions.JmsMessageException) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) TextMessage(javax.jms.TextMessage)

Aggregations

StreamMessage (javax.jms.StreamMessage)5 BytesMessage (javax.jms.BytesMessage)4 JMSException (javax.jms.JMSException)4 TextMessage (javax.jms.TextMessage)4 MapMessage (javax.jms.MapMessage)3 ObjectMessage (javax.jms.ObjectMessage)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 DefaultExchangeHolder (org.apache.camel.impl.DefaultExchangeHolder)2 JmsMessageException (com.axway.ats.action.exceptions.JmsMessageException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Message (javax.jms.Message)1 Object (org.apache.camel.component.jms.JmsMessageType.Object)1