Example usage for javax.jms MapMessage getMapNames

List of usage examples for javax.jms MapMessage getMapNames

Introduction

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

Prototype


Enumeration getMapNames() throws JMSException;

Source Link

Document

Returns an Enumeration of all the names in the MapMessage object.

Usage

From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java

private boolean verify(Message message, MsgCheck check) {
    String sVal = "";

    if (check.getField().equals(MESSAGECONTENTFIELD)) {
        try {//from   w  w  w.j av  a  2s  . com
            if (message instanceof TextMessage) {
                sVal = ((TextMessage) message).getText();
            } else if (message instanceof MapMessage) {
                MapMessage mm = (MapMessage) message;
                ObjectMapper mapper = new ObjectMapper();
                ObjectNode root = mapper.createObjectNode();

                @SuppressWarnings("unchecked")
                Enumeration<String> e = mm.getMapNames();
                while (e.hasMoreElements()) {
                    String field = e.nextElement();
                    root.set(field, mapper.convertValue(mm.getObject(field), JsonNode.class));
                }
                sVal = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
            } else if (message instanceof BytesMessage) {
                BytesMessage bm = (BytesMessage) message;
                bm.reset();
                byte[] bytes = new byte[(int) bm.getBodyLength()];
                if (bm.readBytes(bytes) == bm.getBodyLength()) {
                    sVal = new String(bytes);
                }
            }
        } catch (JMSException e) {
            return false;
        } catch (JsonProcessingException e) {
            return false;
        }
    } else {
        Enumeration<String> propNames = null;
        try {
            propNames = message.getPropertyNames();
            while (propNames.hasMoreElements()) {
                String propertyName = propNames.nextElement();
                if (propertyName.equals(check.getField())) {
                    if (message.getObjectProperty(propertyName) != null) {
                        sVal = message.getObjectProperty(propertyName).toString();
                        break;
                    }
                }
            }
        } catch (JMSException e) {
            return false;
        }
    }

    String eVal = "";
    if (check.getExpectedValue() != null) {
        eVal = check.getExpectedValue();
    }
    if (Pattern.compile(eVal).matcher(sVal).find()) {
        return true;
    }
    return false;
}

From source file:org.apache.camel.component.jms.JmsBinding.java

/**
 * Extracts a {@link Map} from a {@link MapMessage}
 *//*from  w w  w.ja  va 2 s .  co  m*/
public Map<String, Object> createMapFromMapMessage(MapMessage message) throws JMSException {
    Map<String, Object> answer = new HashMap<String, Object>();
    Enumeration names = message.getMapNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement().toString();
        Object value = message.getObject(name);
        answer.put(name, value);
    }
    return answer;
}

From source file:org.apache.jmeter.protocol.jms.sampler.SubscriberSampler.java

private void extractContent(StringBuilder buffer, StringBuilder propBuffer, Message msg, boolean isLast) {
    if (msg != null) {
        try {/*  w w  w.j a  va 2s. com*/
            if (msg instanceof TextMessage) {
                buffer.append(((TextMessage) msg).getText());
            } else if (msg instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) msg;
                if (objectMessage.getObject() != null) {
                    buffer.append(objectMessage.getObject().getClass());
                } else {
                    buffer.append("object is null");
                }
            } else if (msg instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) msg;
                buffer.append(bytesMessage.getBodyLength() + " bytes received in BytesMessage");
            } else if (msg instanceof MapMessage) {
                MapMessage mapm = (MapMessage) msg;
                @SuppressWarnings("unchecked") // MapNames are Strings
                Enumeration<String> enumb = mapm.getMapNames();
                while (enumb.hasMoreElements()) {
                    String name = enumb.nextElement();
                    Object obj = mapm.getObject(name);
                    buffer.append(name);
                    buffer.append(",");
                    buffer.append(obj.getClass().getCanonicalName());
                    buffer.append(",");
                    buffer.append(obj);
                    buffer.append("\n");
                }
            }
            Utils.messageProperties(propBuffer, msg);
            if (!isLast && !StringUtils.isEmpty(separator)) {
                propBuffer.append(separator);
                buffer.append(separator);
            }
        } catch (JMSException e) {
            log.error(e.getMessage());
        }
    }
}

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

public static Object toObject(Message source, String jmsSpec, String encoding)
        throws JMSException, IOException {
    if (source instanceof ObjectMessage) {
        return ((ObjectMessage) source).getObject();
    } else if (source instanceof MapMessage) {
        Map<String, Object> map = new HashMap<String, Object>();
        MapMessage m = (MapMessage) source;

        for (Enumeration<?> e = m.getMapNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object obj = m.getObject(name);
            map.put(name, obj);/*w  ww.  j  a  v a  2s  .c  om*/
        }

        return map;
    } else if (source instanceof TextMessage) {
        return ((TextMessage) source).getText();
    } else if (source instanceof BytesMessage) {
        return toByteArray(source, jmsSpec, encoding);
    } else if (source instanceof StreamMessage) {
        List<Object> result = new ArrayList<Object>();
        try {
            StreamMessage sMsg = (StreamMessage) source;
            Object obj;
            while ((obj = sMsg.readObject()) != null) {
                result.add(obj);
            }
        } catch (MessageEOFException eof) {
            // ignored
        } catch (Exception e) {
            throw new JMSException("Failed to extract information from JMS Stream Message: " + e);
        }
        return result;
    }

    // what else is there to do?
    return source;
}

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

@Test
public void testMapMessageWithNullValue() throws Exception {
    String[] keys = new String[] { "key", "null" };
    Iterator<String> keyIterator = IteratorUtils.arrayIterator(keys);
    Enumeration<String> keyEnumeration = new IteratorEnumeration(keyIterator);

    MapMessage mockMessage1 = mock(MapMessage.class);
    when(mockMessage1.getMapNames()).thenReturn(keyEnumeration);
    when(mockMessage1.getObject("key")).thenReturn("value");
    when(mockMessage1.getObject("null")).thenReturn(null);

    Object result = JmsMessageUtils.toObject(mockMessage1, JmsConstants.JMS_SPECIFICATION_11, ENCODING);
    assertNotNull(result);//from w ww  .j ava  2s .  c  om
    assertTrue(result instanceof Map);
    Map map = (Map) result;
    assertEquals("value", map.get("key"));
    assertNull(map.get("null"));
}

From source file:org.wso2.carbon.andes.core.internal.util.Utils.java

/**
 * Gets the message content as a string, after verifying its type
 *
 * @param message - JMS Message//w w  w . ja  v  a 2 s  . c  o  m
 * @return a string array of message content; a summary and the whole message
 * @throws JMSException
 */
public static String[] getMessageContentAsString(Message message) throws JMSException {

    String messageContent[] = new String[2];
    String summaryMsg = "";
    String wholeMsg = "";

    StringBuilder sb = new StringBuilder();
    if (message != null) {
        if (message instanceof TextMessage) {
            String textMessage = ((TextMessage) message).getText();
            if (StringUtils.isNotEmpty(textMessage)) {
                wholeMsg = StringEscapeUtils.escapeHtml(textMessage).trim();
                if (wholeMsg.length() >= CHARACTERS_TO_SHOW) {
                    summaryMsg = wholeMsg.substring(0, CHARACTERS_TO_SHOW);
                } else {
                    summaryMsg = wholeMsg;
                }
                if (wholeMsg.length() > MESSAGE_DISPLAY_LENGTH_MAX) {
                    wholeMsg = wholeMsg.substring(0, MESSAGE_DISPLAY_LENGTH_MAX - 3) + DISPLAY_CONTINUATION
                            + DISPLAY_LENGTH_EXCEEDED;
                }
            }
        } else if (message instanceof ObjectMessage) {
            wholeMsg = "This Operation is Not Supported!";
            summaryMsg = "Not Supported";

        } else if (message instanceof MapMessage) {
            MapMessage mapMessage = ((MapMessage) message);
            Enumeration mapEnu = mapMessage.getMapNames();
            while (mapEnu.hasMoreElements()) {
                String mapName = (String) mapEnu.nextElement();
                String mapVal = mapMessage.getObject(mapName).toString();
                wholeMsg = StringEscapeUtils
                        .escapeHtml(sb.append(mapName).append(": ").append(mapVal).append(", ").toString())
                        .trim();

            }
            if (wholeMsg.length() >= CHARACTERS_TO_SHOW) {
                summaryMsg = wholeMsg.substring(0, CHARACTERS_TO_SHOW);
            } else {
                summaryMsg = wholeMsg;
            }

        } else if (message instanceof StreamMessage) {
            ((StreamMessage) message).reset();
            wholeMsg = getContentFromStreamMessage((StreamMessage) message, sb).trim();
            if (wholeMsg.length() >= CHARACTERS_TO_SHOW) {
                summaryMsg = wholeMsg.substring(0, CHARACTERS_TO_SHOW);
            } else {
                summaryMsg = wholeMsg;
            }

        } else if (message instanceof BytesMessage) {
            ((BytesMessage) message).reset();
            long messageLength = ((BytesMessage) message).getBodyLength();
            byte[] byteMsgArr = new byte[(int) messageLength];

            int index = ((BytesMessage) message).readBytes(byteMsgArr);
            for (int i = 0; i < index; i++) {
                wholeMsg = sb.append(byteMsgArr[i]).append(" ").toString().trim();
            }

            if (wholeMsg.length() >= CHARACTERS_TO_SHOW) {
                summaryMsg = wholeMsg.substring(0, CHARACTERS_TO_SHOW);
            } else {
                summaryMsg = wholeMsg;
            }
        }
    }
    messageContent[0] = summaryMsg;
    messageContent[1] = wholeMsg;
    return messageContent;
}

From source file:org.wso2.carbon.apimgt.jms.listener.utils.JMSMessageListener.java

public void onMessage(Message message) {
    try {//from  ww w  .  j a  v a2 s . com
        if (message != null) {
            if (log.isDebugEnabled()) {
                log.debug("Event received in JMS Event Receiver - " + message);
            }

            if (message instanceof MapMessage) {
                MapMessage mapMessage = (MapMessage) message;
                Map<String, Object> map = new HashMap<String, Object>();
                Enumeration enumeration = mapMessage.getMapNames();
                while (enumeration.hasMoreElements()) {
                    String key = (String) enumeration.nextElement();
                    map.put(key, mapMessage.getObject(key));
                }

                if (map.get(APIConstants.THROTTLE_KEY) != null) {
                    /**
                     * This message contains throttle data in map which contains Keys
                     * throttleKey - Key of particular throttling level
                     * isThrottled - Whether message has throttled or not
                     * expiryTimeStamp - When the throttling time window will expires
                     */
                    handleThrottleUpdateMessage(map);
                } else if (map.get(APIConstants.BLOCKING_CONDITION_KEY) != null) {
                    /**
                     * This message contains blocking condition data
                     * blockingCondition - Blocking condition type
                     * conditionValue - blocking condition value
                     * state - State whether blocking condition is enabled or not
                     */
                    handleBlockingMessage(map);
                } else if (map.get(APIConstants.POLICY_TEMPLATE_KEY) != null) {
                    /**
                     * This message contains key template data
                     * keyTemplateValue - Value of key template
                     * keyTemplateState - whether key template active or not
                     */
                    handleKeyTemplateMessage(map);
                }

            } else {
                log.warn("Event dropped due to unsupported message type " + message.getClass());
            }
        } else {
            log.warn("Dropping the empty/null event received through jms receiver");
        }
    } catch (JMSException e) {
        log.error("JMSException occurred when processing the received message ", e);
    }
}

From source file:org.wso2.carbon.event.input.adapter.jms.internal.util.JMSMessageListener.java

public void onMessage(Message message) {
    try {//from   w  w w  .  j  av  a 2s  .  c om
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
        if (message != null) {

            if (log.isDebugEnabled()) {
                log.debug("Event received in JMS Event Adaptor - " + message);
            }

            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                // Send the text of the message. Conversion to any type (XML,JSON) should
                // not happen here since this message will be built later.
                try {
                    String msgText = textMessage.getText();
                    eventAdaptorListener.onEvent(msgText);
                } catch (JMSException e) {
                    if (log.isErrorEnabled()) {
                        log.error("Failed to get text from " + textMessage, e);
                    }
                } catch (InputEventAdapterRuntimeException e) {
                    if (log.isErrorEnabled()) {
                        log.error(e);
                    }
                }
            } else if (message instanceof MapMessage) {
                MapMessage mapMessage = (MapMessage) message;
                Map event = new HashMap();
                try {
                    Enumeration names = mapMessage.getMapNames();
                    Object name;
                    while (names.hasMoreElements()) {
                        name = names.nextElement();
                        event.put(name, mapMessage.getObject((String) name));
                    }
                    eventAdaptorListener.onEvent(event);

                } catch (JMSException e) {
                    log.error("Can not read the map message ", e);
                } catch (InputEventAdapterRuntimeException e) {
                    log.error("Can not send the message to broker ", e);
                }
            } else if (message instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) message;
                byte[] bytes;
                bytes = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(bytes);
                eventAdaptorListener.onEvent(new String(bytes, "UTF-8"));
            } else {
                log.warn("Event dropped due to unsupported message type");
            }
        } else {
            log.warn("Dropping the empty/null event received through jms adaptor");
        }
    } catch (JMSException e) {
        log.error(e);
    } catch (UnsupportedEncodingException e) {
        log.error(e);
    } catch (Throwable t) {
        log.error(t);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}

From source file:org.wso2.carbon.event.input.adaptor.jms.internal.util.JMSMessageListener.java

public void onMessage(Message message) {
    try {//w  w  w.  j av  a 2 s  . c o m
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
        if (message != null) {

            if (log.isDebugEnabled()) {
                log.debug(message);
            }

            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                // Send the text of the message. Conversion to any type (XML,JSON) should
                // not happen here since this message will be built later.
                try {
                    String msgText = textMessage.getText();
                    eventAdaptorListener.onEventCall(msgText);
                } catch (JMSException e) {
                    if (log.isErrorEnabled()) {
                        log.error("Failed to get text from " + textMessage, e);
                    }
                } catch (InputEventAdaptorEventProcessingException e) {
                    if (log.isErrorEnabled()) {
                        log.error(e);
                    }
                }
            } else if (message instanceof MapMessage) {
                MapMessage mapMessage = (MapMessage) message;
                Map event = new HashMap();
                try {
                    Enumeration names = mapMessage.getMapNames();
                    Object name;
                    while (names.hasMoreElements()) {
                        name = names.nextElement();
                        event.put(name, mapMessage.getObject((String) name));
                    }
                    eventAdaptorListener.onEventCall(event);

                } catch (JMSException e) {
                    log.error("Can not read the map message ", e);
                } catch (InputEventAdaptorEventProcessingException e) {
                    log.error("Can not send the message to broker ", e);
                }
            } else if (message instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) message;
                byte[] bytes;
                bytes = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(bytes);
                eventAdaptorListener.onEventCall(new String(bytes, "UTF-8"));
            } else {
                log.warn("Event dropped due to unsupported message type");
            }
        } else {
            log.warn("Dropping the empty/null event received through jms adaptor");
        }
    } catch (JMSException e) {
        log.error(e);
    } catch (UnsupportedEncodingException e) {
        log.error(e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.JMSInjectHandler.java

/**
 * //from w ww  . j  ava  2s.  co m
 * @param message
 *            JMSMap message
 * @return XML representation of JMS Map message
 */
public static OMElement convertJMSMapToXML(MapMessage message) {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace jmsMapNS = OMAbstractFactory.getOMFactory().createOMNamespace(JMSConstants.JMS_MAP_NS, "");
    OMElement jmsMap = fac.createOMElement(JMSConstants.JMS_MAP_ELEMENT_NAME, jmsMapNS);
    try {
        Enumeration names = message.getMapNames();
        while (names.hasMoreElements()) {
            String nextName = names.nextElement().toString();
            String nextVal = message.getString(nextName);
            OMElement next = fac.createOMElement(nextName.replace(" ", ""), jmsMapNS);
            next.setText(nextVal);
            jmsMap.addChild(next);
        }
    } catch (JMSException e) {
        log.error("Error while processing the JMS Map Message. " + e.getMessage());
    }
    return jmsMap;
}