001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.web;
018    
019    import java.util.Enumeration;
020    import java.util.HashMap;
021    import java.util.Map;
022    import javax.jms.BytesMessage;
023    import javax.jms.JMSException;
024    import javax.jms.MapMessage;
025    import javax.jms.Message;
026    import javax.jms.ObjectMessage;
027    import javax.jms.QueueBrowser;
028    import javax.jms.StreamMessage;
029    import javax.jms.TextMessage;
030    
031    /**
032     * Allow the user to browse a message on a queue by its ID
033     * 
034     * 
035     */
036    public class MessageQuery extends QueueBrowseQuery {
037    
038        private String id;
039        private Message message;
040    
041        public MessageQuery(BrokerFacade brokerFacade, SessionPool sessionPool) throws JMSException {
042            super(brokerFacade, sessionPool);
043        }
044    
045        public String getId() {
046            return id;
047        }
048    
049        public void setId(String id) {
050            this.id = id;
051        }
052    
053        public void setMessage(Message message) {
054            this.message = message;
055        }
056    
057        public Message getMessage() throws JMSException {
058            if (message == null) {
059                if (id != null) {
060                    QueueBrowser tempBrowser=getBrowser();
061                    Enumeration iter = tempBrowser.getEnumeration();
062                    while (iter.hasMoreElements()) {
063                        Message item = (Message) iter.nextElement();
064                        if (id.equals(item.getJMSMessageID())) {
065                            message = item;
066                            break;
067                        }
068                    }
069                    tempBrowser.close();
070                }
071    
072            }
073            return message;
074        }
075    
076        public Object getBody() throws JMSException {
077            Message message = getMessage();
078            if (message instanceof TextMessage) {
079                return ((TextMessage) message).getText();
080            }
081            if (message instanceof ObjectMessage) {
082                try {
083                    return ((ObjectMessage) message).getObject();
084                } catch (JMSException e) {
085                    //message could not be parsed, make the reason available
086                    return e;
087                }
088            }
089            if (message instanceof MapMessage) {
090                return createMapBody((MapMessage) message);
091            }
092            if (message instanceof BytesMessage) {
093                BytesMessage msg = (BytesMessage) message;
094                int len = (int) msg.getBodyLength();
095                if (len > -1) {
096                    byte[] data = new byte[len];
097                    msg.readBytes(data);
098                    return new String(data);
099                } else {
100                    return "";
101                }
102            }
103            if (message instanceof StreamMessage) {
104                return "StreamMessage is not viewable";
105            }
106    
107            // unknown message type
108            if (message != null) {
109                return "Unknown message type [" + message.getClass().getName() + "] " + message;
110            }
111    
112            return null;
113        }
114    
115        public Map<String, Object> getPropertiesMap() throws JMSException {
116            Map<String, Object> answer = new HashMap<String, Object>();
117            Message aMessage = getMessage();
118            Enumeration iter = aMessage.getPropertyNames();
119            while (iter.hasMoreElements()) {
120                String name = (String) iter.nextElement();
121                Object value = aMessage.getObjectProperty(name);
122                if (value != null) {
123                    answer.put(name, value);
124                }
125            }
126            return answer;
127        }
128    
129        protected Map<String, Object> createMapBody(MapMessage mapMessage) throws JMSException {
130            Map<String, Object> answer = new HashMap<String, Object>();
131            Enumeration iter = mapMessage.getMapNames();
132            while (iter.hasMoreElements()) {
133                String name = (String) iter.nextElement();
134                Object value = mapMessage.getObject(name);
135                if (value != null) {
136                    answer.put(name, value);
137                }
138            }
139            return answer;
140        }
141    }