Home » activemq-parent-5.3.1-source-release » org.apache » activemq » web » view » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.web.view;
   18   
   19   import java.io.IOException;
   20   import java.io.PrintWriter;
   21   import java.util.Enumeration;
   22   
   23   import javax.jms.JMSException;
   24   import javax.jms.Message;
   25   import javax.jms.QueueBrowser;
   26   import javax.servlet.ServletException;
   27   import javax.servlet.http.HttpServletRequest;
   28   import javax.servlet.http.HttpServletResponse;
   29   
   30   /**
   31    * A simple rendering of the contents of a queue appear as a list of message
   32    * elements which just contain an ID attribute.
   33    * 
   34    * @version $Revision: $
   35    */
   36   public class SimpleMessageRenderer implements MessageRenderer {
   37   
   38       private String contentType = "text/xml";
   39       private int maxMessages;
   40   
   41       public void renderMessages(HttpServletRequest request, HttpServletResponse response, QueueBrowser browser) throws IOException, JMSException, ServletException {
   42           // lets use XML by default
   43           response.setContentType(getContentType());
   44           PrintWriter writer = response.getWriter();
   45           printHeader(writer, browser, request);
   46   
   47           Enumeration iter = browser.getEnumeration();
   48           for (int counter = 0; iter.hasMoreElements() && (maxMessages <= 0 || counter < maxMessages); counter++) {
   49               Message message = (Message)iter.nextElement();
   50               renderMessage(writer, request, response, browser, message);
   51           }
   52   
   53           printFooter(writer, browser, request);
   54       }
   55   
   56       public void renderMessage(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, QueueBrowser browser, Message message) throws JMSException, ServletException {
   57           // lets just write the message IDs for now
   58           writer.print("<message id='");
   59           writer.print(message.getJMSMessageID());
   60           writer.println("'/>");
   61       }
   62   
   63       // Properties
   64       // -------------------------------------------------------------------------
   65       public int getMaxMessages() {
   66           return maxMessages;
   67       }
   68   
   69       public void setMaxMessages(int maxMessages) {
   70           this.maxMessages = maxMessages;
   71       }
   72   
   73       public String getContentType() {
   74           return contentType;
   75       }
   76   
   77       public void setContentType(String contentType) {
   78           this.contentType = contentType;
   79       }
   80   
   81       // Implementation methods
   82       // -------------------------------------------------------------------------
   83   
   84       protected void printHeader(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException, ServletException {
   85           writer.println("");
   86           writer.print("<messages queue='");
   87           writer.print(browser.getQueue());
   88           writer.print("'");
   89           String selector = browser.getMessageSelector();
   90           if (selector != null) {
   91               writer.print(" selector='");
   92               writer.print(selector);
   93               writer.print("'");
   94           }
   95           writer.println(">");
   96       }
   97   
   98       protected void printFooter(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException, ServletException {
   99           writer.println("</messages>");
  100       }
  101   
  102   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » web » view » [javadoc | source]