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.Date;
022 import java.util.List;
023
024 import javax.jms.JMSException;
025 import javax.jms.Message;
026 import javax.jms.QueueBrowser;
027 import javax.jms.TextMessage;
028 import javax.servlet.ServletException;
029 import javax.servlet.http.HttpServletRequest;
030 import javax.servlet.http.HttpServletResponse;
031
032 import com.sun.syndication.feed.synd.SyndContent;
033 import com.sun.syndication.feed.synd.SyndContentImpl;
034 import com.sun.syndication.feed.synd.SyndEntry;
035 import com.sun.syndication.feed.synd.SyndEntryImpl;
036 import com.sun.syndication.feed.synd.SyndFeed;
037 import com.sun.syndication.feed.synd.SyndFeedImpl;
038 import com.sun.syndication.io.FeedException;
039 import com.sun.syndication.io.SyndFeedOutput;
040
041 /**
042 * This renderer uses XStream to render messages on a queue as full XML elements
043 *
044 * @version $Revision: $
045 */
046 public class RssMessageRenderer extends SimpleMessageRenderer {
047
048 // private String feedType = "atom_0.3";
049 private String feedType = "rss_2.0";
050 private SyndFeed feed;
051 private String description = "This feed is auto-generated by Apache ActiveMQ";
052 private String entryContentType = "text/plain";
053
054 public void renderMessage(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, QueueBrowser browser, Message message) throws JMSException {
055 SyndFeed feed = getFeed(browser, request);
056
057 List<SyndEntry> entries = feed.getEntries();
058 SyndEntry entry = createEntry(browser, message, request);
059 SyndContent description = createEntryContent(browser, message, request);
060 entry.setDescription(description);
061 entries.add(entry);
062 }
063
064 // Properties
065 // -------------------------------------------------------------------------
066 public String getDescription() {
067 return description;
068 }
069
070 public void setDescription(String feedDescription) {
071 this.description = feedDescription;
072 }
073
074 public String getFeedType() {
075 return feedType;
076 }
077
078 public void setFeedType(String feedType) {
079 this.feedType = feedType;
080 }
081
082 public String getEntryContentType() {
083 return entryContentType;
084 }
085
086 public void setEntryContentType(String entryContentType) {
087 this.entryContentType = entryContentType;
088 }
089
090 // Implementation methods
091 // -------------------------------------------------------------------------
092
093 protected void printFooter(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException, ServletException {
094 // now lets actually write out the content
095 SyndFeed feed = getFeed(browser, request);
096 SyndFeedOutput output = new SyndFeedOutput();
097 try {
098 output.output(feed, writer);
099 } 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 }