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 */
017package org.apache.activemq.web.view;
018
019import java.io.IOException;
020import java.io.PrintWriter;
021import java.util.Date;
022import java.util.List;
023
024import javax.jms.JMSException;
025import javax.jms.Message;
026import javax.jms.QueueBrowser;
027import javax.jms.TextMessage;
028import javax.servlet.ServletException;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031
032import com.rometools.rome.feed.synd.SyndContent;
033import com.rometools.rome.feed.synd.SyndContentImpl;
034import com.rometools.rome.feed.synd.SyndEntry;
035import com.rometools.rome.feed.synd.SyndEntryImpl;
036import com.rometools.rome.feed.synd.SyndFeed;
037import com.rometools.rome.feed.synd.SyndFeedImpl;
038import com.rometools.rome.io.FeedException;
039import com.rometools.rome.io.SyndFeedOutput;
040
041/**
042 * This renderer uses XStream to render messages on a queue as full XML elements
043 * 
044 * 
045 */
046public 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        List<SyndEntry> entries = feed.getEntries();
057        SyndEntry entry = createEntry(browser, message, request);
058        SyndContent description = createEntryContent(browser, message, request);
059        entry.setDescription(description);
060        entries.add(entry);
061    }
062
063    // Properties
064    // -------------------------------------------------------------------------
065    public String getDescription() {
066        return description;
067    }
068
069    public void setDescription(String feedDescription) {
070        this.description = feedDescription;
071    }
072
073    public String getFeedType() {
074        return feedType;
075    }
076
077    public void setFeedType(String feedType) {
078        this.feedType = feedType;
079    }
080
081    public String getEntryContentType() {
082        return entryContentType;
083    }
084
085    public void setEntryContentType(String entryContentType) {
086        this.entryContentType = entryContentType;
087    }
088
089    // Implementation methods
090    // -------------------------------------------------------------------------
091
092    protected void printFooter(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException, ServletException {
093        // now lets actually write out the content
094        SyndFeed feed = getFeed(browser, request);
095        SyndFeedOutput output = new SyndFeedOutput();
096        try {
097            output.output(feed, writer);
098        } catch (FeedException e) {
099            throw new ServletException(e);
100        }
101    }
102
103    protected void printHeader(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) throws IOException, JMSException {
104    }
105
106    public SyndFeed getFeed(QueueBrowser browser, HttpServletRequest request) throws JMSException {
107        if (feed == null) {
108            feed = createFeed(browser, request);
109        }
110        return feed;
111    }
112
113    protected SyndEntry createEntry(QueueBrowser browser, Message message, HttpServletRequest request) throws JMSException {
114        SyndEntry entry = new SyndEntryImpl();
115        String title = message.getJMSMessageID();
116        entry.setTitle(title);
117        String link = request.getRequestURI() + "?msgId=" + title;
118        entry.setLink(link);
119        entry.setPublishedDate(new Date(message.getJMSTimestamp()));
120        return entry;
121    }
122
123    protected SyndContent createEntryContent(QueueBrowser browser, Message message, HttpServletRequest request) throws JMSException {
124        SyndContent description = new SyndContentImpl();
125        description.setType(entryContentType);
126
127        if (message instanceof TextMessage) {
128            String text = ((TextMessage)message).getText();
129            description.setValue(text);
130        }
131        return description;
132    }
133
134    protected SyndFeed createFeed(QueueBrowser browser, HttpServletRequest request) throws JMSException {
135        SyndFeed feed = new SyndFeedImpl();
136        feed.setFeedType(feedType);
137
138        String title = browser.getQueue().toString();
139        String selector = browser.getMessageSelector();
140        if (selector != null) {
141            title += " with selector: " + selector;
142        }
143        feed.setTitle(title);
144        feed.setLink(request.getRequestURI());
145        feed.setDescription(getDescription());
146        return feed;
147    }
148
149}