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.view;
018    
019    import java.io.IOException;
020    import java.io.PrintWriter;
021    import java.util.Enumeration;
022    
023    import javax.jms.JMSException;
024    import javax.jms.Message;
025    import javax.jms.QueueBrowser;
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * A simple rendering of the contents of a queue appear as a list of message
032     * elements which just contain an ID attribute.
033     * 
034     * 
035     */
036    public class SimpleMessageRenderer implements MessageRenderer {
037    
038        private String contentType = "text/xml";
039        private int maxMessages;
040    
041        public void renderMessages(HttpServletRequest request, HttpServletResponse response, QueueBrowser browser) throws IOException, JMSException, ServletException {
042            // lets use XML by default
043            response.setContentType(getContentType());
044            PrintWriter writer = response.getWriter();
045            printHeader(writer, browser, request);
046    
047            Enumeration iter = browser.getEnumeration();
048            for (int counter = 0; iter.hasMoreElements() && (maxMessages <= 0 || counter < maxMessages); counter++) {
049                Message message = (Message)iter.nextElement();
050                renderMessage(writer, request, response, browser, message);
051            }
052    
053            printFooter(writer, browser, request);
054        }
055    
056        public void renderMessage(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, QueueBrowser browser, Message message) throws JMSException, ServletException {
057            // lets just write the message IDs for now
058            writer.print("<message id='");
059            writer.print(message.getJMSMessageID());
060            writer.println("'/>");
061        }
062    
063        // Properties
064        // -------------------------------------------------------------------------
065        public int getMaxMessages() {
066            return maxMessages;
067        }
068    
069        public void setMaxMessages(int maxMessages) {
070            this.maxMessages = maxMessages;
071        }
072    
073        public String getContentType() {
074            return contentType;
075        }
076    
077        public void setContentType(String contentType) {
078            this.contentType = contentType;
079        }
080    
081        // Implementation methods
082        // -------------------------------------------------------------------------
083    
084        protected void printHeader(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException, ServletException {
085            writer.println("");
086            writer.print("<messages queue='");
087            writer.print(browser.getQueue());
088            writer.print("'");
089            String selector = browser.getMessageSelector();
090            if (selector != null) {
091                writer.print(" selector='");
092                writer.print(selector);
093                writer.print("'");
094            }
095            writer.println(">");
096        }
097    
098        protected void printFooter(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException, ServletException {
099            writer.println("</messages>");
100        }
101    
102    }