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    
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.TextMessage;
029    
030    /**
031     * Allow the user to browse a message on a queue by its ID
032     * 
033     * @version $Revision: 754973 $
034     */
035    public class MessageQuery extends QueueBrowseQuery {
036    
037        private String id;
038        private Message message;
039    
040        public MessageQuery(BrokerFacade brokerFacade, SessionPool sessionPool) throws JMSException {
041            super(brokerFacade, sessionPool);
042        }
043    
044        public String getId() {
045            return id;
046        }
047    
048        public void setId(String id) {
049            this.id = id;
050        }
051    
052        public void setMessage(Message message) {
053            this.message = message;
054        }
055    
056        public Message getMessage() throws JMSException {
057            if (message == null) {
058                if (id != null) {
059                    QueueBrowser tempBrowser=getBrowser();
060                    Enumeration iter = tempBrowser.getEnumeration();
061                    while (iter.hasMoreElements()) {
062                        Message item = (Message) iter.nextElement();
063                        if (id.equals(item.getJMSMessageID())) {
064                            message = item;
065                            break;
066                        }
067                    }
068                    tempBrowser.close();
069                }
070    
071            }
072            return message;
073        }
074    
075        public Object getBody() throws JMSException {
076            Message message = getMessage();
077            if (message instanceof TextMessage) {
078                return ((TextMessage) message).getText();
079            }
080            if (message instanceof ObjectMessage) {
081                try {
082                    return ((ObjectMessage) message).getObject();
083                } catch (JMSException e) {
084                    //message could not be parsed, make the reason available
085                    return e;
086                }
087            }
088            if (message instanceof MapMessage) {
089                return createMapBody((MapMessage) message);
090            }
091            return null;
092        }
093    
094        public Map<String, Object> getPropertiesMap() throws JMSException {
095            Map<String, Object> answer = new HashMap<String, Object>();
096            Message aMessage = getMessage();
097            Enumeration iter = aMessage.getPropertyNames();
098            while (iter.hasMoreElements()) {
099                String name = (String) iter.nextElement();
100                Object value = aMessage.getObjectProperty(name);
101                if (value != null) {
102                    answer.put(name, value);
103                }
104            }
105            return answer;
106        }
107    
108        protected Map<String, Object> createMapBody(MapMessage mapMessage) throws JMSException {
109            Map<String, Object> answer = new HashMap<String, Object>();
110            Enumeration iter = mapMessage.getMapNames();
111            while (iter.hasMoreElements()) {
112                String name = (String) iter.nextElement();
113                Object value = mapMessage.getObject(name);
114                if (value != null) {
115                    answer.put(name, value);
116                }
117            }
118            return answer;
119        }
120    }