KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > view > RssMessageRenderer


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 com.sun.syndication.feed.synd.SyndContent;
20 import com.sun.syndication.feed.synd.SyndContentImpl;
21 import com.sun.syndication.feed.synd.SyndEntry;
22 import com.sun.syndication.feed.synd.SyndEntryImpl;
23 import com.sun.syndication.feed.synd.SyndFeed;
24 import com.sun.syndication.feed.synd.SyndFeedImpl;
25 import com.sun.syndication.io.FeedException;
26 import com.sun.syndication.io.SyndFeedOutput;
27
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Message JavaDoc;
30 import javax.jms.QueueBrowser JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import java.io.IOException JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.util.List JavaDoc;
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 JavaDoc feedType = "rss_2.0";
50     private SyndFeed feed;
51     private String JavaDoc description = "This feed is auto-generated by Apache ActiveMQ";
52     private String JavaDoc entryContentType = "text/plain";
53
54     public void renderMessage(PrintWriter JavaDoc writer, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
55             QueueBrowser JavaDoc browser, Message JavaDoc message) throws JMSException JavaDoc {
56         SyndFeed feed = getFeed(browser, request);
57
58         List JavaDoc entries = feed.getEntries();
59         SyndEntry entry = createEntry(browser, message, request);
60         SyndContent description = createEntryContent(browser, message, request);
61         entry.setDescription(description);
62         entries.add(entry);
63     }
64
65     // Properties
66
// -------------------------------------------------------------------------
67
public String JavaDoc getDescription() {
68         return description;
69     }
70
71     public void setDescription(String JavaDoc feedDescription) {
72         this.description = feedDescription;
73     }
74
75     public String JavaDoc getFeedType() {
76         return feedType;
77     }
78
79     public void setFeedType(String JavaDoc feedType) {
80         this.feedType = feedType;
81     }
82
83     public String JavaDoc getEntryContentType() {
84         return entryContentType;
85     }
86
87     public void setEntryContentType(String JavaDoc entryContentType) {
88         this.entryContentType = entryContentType;
89     }
90
91     // Implementation methods
92
// -------------------------------------------------------------------------
93

94     protected void printFooter(PrintWriter JavaDoc writer, QueueBrowser JavaDoc browser, HttpServletRequest JavaDoc request)
95             throws IOException JavaDoc, JMSException JavaDoc, ServletException JavaDoc {
96         // now lets actually write out the content
97
SyndFeed feed = getFeed(browser, request);
98         SyndFeedOutput output = new SyndFeedOutput();
99         try {
100             output.output(feed, writer);
101         }
102         catch (FeedException e) {
103             throw new ServletException JavaDoc(e);
104         }
105     }
106
107     protected void printHeader(PrintWriter JavaDoc writer, QueueBrowser JavaDoc browser, HttpServletRequest JavaDoc request)
108             throws IOException JavaDoc, JMSException JavaDoc {
109     }
110
111     public SyndFeed getFeed(QueueBrowser JavaDoc browser, HttpServletRequest JavaDoc request) throws JMSException JavaDoc {
112         if (feed == null) {
113             feed = createFeed(browser, request);
114         }
115         return feed;
116     }
117
118     protected SyndEntry createEntry(QueueBrowser JavaDoc browser, Message JavaDoc message, HttpServletRequest JavaDoc request) throws JMSException JavaDoc {
119         SyndEntry entry = new SyndEntryImpl();
120         String JavaDoc title = message.getJMSMessageID();
121         entry.setTitle(title);
122         String JavaDoc link = request.getRequestURI() + "/" + title;
123         entry.setLink(link);
124         entry.setPublishedDate(new Date JavaDoc());
125         return entry;
126     }
127
128     protected SyndContent createEntryContent(QueueBrowser JavaDoc browser, Message JavaDoc message, HttpServletRequest JavaDoc request) throws JMSException JavaDoc {
129         SyndContent description = new SyndContentImpl();
130         description.setType(entryContentType);
131
132         if (message instanceof TextMessage JavaDoc) {
133             String JavaDoc text = ((TextMessage JavaDoc) message).getText();
134             description.setValue(text);
135         }
136         return description;
137     }
138
139     protected SyndFeed createFeed(QueueBrowser JavaDoc browser, HttpServletRequest JavaDoc request) throws JMSException JavaDoc {
140         SyndFeed feed = new SyndFeedImpl();
141         feed.setFeedType(feedType);
142
143         String JavaDoc title = browser.getQueue().toString();
144         String JavaDoc selector = browser.getMessageSelector();
145         if (selector != null) {
146             title += " with selector: " + selector;
147         }
148         feed.setTitle(title);
149         feed.setLink(request.getRequestURI());
150         feed.setDescription(getDescription());
151         return feed;
152     }
153
154 }
155
Popular Tags