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.Date;
   22   import java.util.List;
   23   
   24   import javax.jms.JMSException;
   25   import javax.jms.Message;
   26   import javax.jms.QueueBrowser;
   27   import javax.jms.TextMessage;
   28   import javax.servlet.ServletException;
   29   import javax.servlet.http.HttpServletRequest;
   30   import javax.servlet.http.HttpServletResponse;
   31   
   32   import com.sun.syndication.feed.synd.SyndContent;
   33   import com.sun.syndication.feed.synd.SyndContentImpl;
   34   import com.sun.syndication.feed.synd.SyndEntry;
   35   import com.sun.syndication.feed.synd.SyndEntryImpl;
   36   import com.sun.syndication.feed.synd.SyndFeed;
   37   import com.sun.syndication.feed.synd.SyndFeedImpl;
   38   import com.sun.syndication.io.FeedException;
   39   import com.sun.syndication.io.SyndFeedOutput;
   40   
   41   /**
   42    * This renderer uses XStream to render messages on a queue as full XML elements
   43    * 
   44    * @version $Revision: $
   45    */
   46   public class RssMessageRenderer extends SimpleMessageRenderer {
   47   
   48       // private String feedType = "atom_0.3";
   49       private String feedType = "rss_2.0";
   50       private SyndFeed feed;
   51       private String description = "This feed is auto-generated by Apache ActiveMQ";
   52       private String entryContentType = "text/plain";
   53   
   54       public void renderMessage(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, QueueBrowser browser, Message message) throws JMSException {
   55           SyndFeed feed = getFeed(browser, request);
   56   
   57           List<SyndEntry> entries = feed.getEntries();
   58           SyndEntry entry = createEntry(browser, message, request);
   59           SyndContent description = createEntryContent(browser, message, request);
   60           entry.setDescription(description);
   61           entries.add(entry);
   62       }
   63   
   64       // Properties
   65       // -------------------------------------------------------------------------
   66       public String getDescription() {
   67           return description;
   68       }
   69   
   70       public void setDescription(String feedDescription) {
   71           this.description = feedDescription;
   72       }
   73   
   74       public String getFeedType() {
   75           return feedType;
   76       }
   77   
   78       public void setFeedType(String feedType) {
   79           this.feedType = feedType;
   80       }
   81   
   82       public String getEntryContentType() {
   83           return entryContentType;
   84       }
   85   
   86       public void setEntryContentType(String entryContentType) {
   87           this.entryContentType = entryContentType;
   88       }
   89   
   90       // Implementation methods
   91       // -------------------------------------------------------------------------
   92   
   93       protected void printFooter(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException, ServletException {
   94           // now lets actually write out the content
   95           SyndFeed feed = getFeed(browser, request);
   96           SyndFeedOutput output = new SyndFeedOutput();
   97           try {
   98               output.output(feed, writer);
   99           } catch (FeedException e) {
  100               throw new ServletException(e);
  101           }
  102       }
  103   
  104       protected void printHeader(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException {
  105       }
  106   
  107       public SyndFeed getFeed(QueueBrowser browser, HttpServletRequest request) throws JMSException {
  108           if (feed == null) {
  109               feed = createFeed(browser, request);
  110           }
  111           return feed;
  112       }
  113   
  114       protected SyndEntry createEntry(QueueBrowser browser, Message message, HttpServletRequest request) throws JMSException {
  115           SyndEntry entry = new SyndEntryImpl();
  116           String title = message.getJMSMessageID();
  117           entry.setTitle(title);
  118           String link = request.getRequestURI() + "?msgId=" + title;
  119           entry.setLink(link);
  120           entry.setPublishedDate(new Date());
  121           return entry;
  122       }
  123   
  124       protected SyndContent createEntryContent(QueueBrowser browser, Message message, HttpServletRequest request) throws JMSException {
  125           SyndContent description = new SyndContentImpl();
  126           description.setType(entryContentType);
  127   
  128           if (message instanceof TextMessage) {
  129               String text = ((TextMessage)message).getText();
  130               description.setValue(text);
  131           }
  132           return description;
  133       }
  134   
  135       protected SyndFeed createFeed(QueueBrowser browser, HttpServletRequest request) throws JMSException {
  136           SyndFeed feed = new SyndFeedImpl();
  137           feed.setFeedType(feedType);
  138   
  139           String title = browser.getQueue().toString();
  140           String selector = browser.getMessageSelector();
  141           if (selector != null) {
  142               title += " with selector: " + selector;
  143           }
  144           feed.setTitle(title);
  145           feed.setLink(request.getRequestURI());
  146           feed.setDescription(getDescription());
  147           return feed;
  148       }
  149   
  150   }

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