001: package ru.emdev.EmForge.rss;
002:
003: import java.util.Map;
004:
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletResponse;
007:
008: import org.springframework.web.servlet.view.AbstractView;
009:
010: import ru.emdev.EmForge.EmForgeContext;
011:
012: import com.sun.syndication.feed.synd.SyndFeed;
013: import com.sun.syndication.feed.synd.SyndFeedImpl;
014: import com.sun.syndication.feed.synd.SyndImage;
015: import com.sun.syndication.feed.synd.SyndImageImpl;
016: import com.sun.syndication.io.SyndFeedOutput;
017:
018: /** Abstract RSS View
019: *
020: * This class is used as base class for all RSS View in EmForge Project
021: * @note Initially I wrote this code based on zabada's recipes project example
022: * @note But I decided to use Rome instead of RSSLibJ since it is more up-to-dated project
023: */
024: public abstract class AbstractRSSView extends AbstractView {
025: /// feed description
026: protected String m_description;
027: /// feed link
028: protected String m_link;
029: /// feed title
030: protected String m_title;
031: /// feed's image path
032: protected String m_imagePath;
033:
034: protected EmForgeContext m_appContext;
035:
036: public void setDescription(String description) {
037: m_description = description;
038: }
039:
040: public void setLink(String link) {
041: m_link = link;
042: }
043:
044: public void setTitle(String title) {
045: m_title = title;
046: }
047:
048: public void setImagePath(String imagePath) {
049: m_imagePath = imagePath;
050: }
051:
052: public void setAppContext(EmForgeContext i_appContext) {
053: m_appContext = i_appContext;
054: }
055:
056: public AbstractRSSView() {
057: setContentType("text/xml; charset=UTF-8");
058: }
059:
060: /**
061: * Renders the view given the specified model.
062: */
063: protected final void renderMergedOutputModel(Map i_model,
064: HttpServletRequest i_request, HttpServletResponse o_response)
065: throws Exception {
066: SyndFeed feed = new SyndFeedImpl();
067:
068: buildRSSFeed(i_model, feed, i_request, o_response);
069:
070: feed.setFeedType("rss_2.0");
071:
072: feed.setDescription(m_description);
073: feed.setLink(m_link);
074: feed.setTitle(m_title);
075:
076: SyndImage image = new SyndImageImpl();
077: image.setLink(m_link);
078: image.setTitle(m_title);
079: image.setUrl(m_imagePath);
080: image.setDescription(m_description);
081: feed.setImage(image);
082:
083: o_response.setContentType(getContentType());
084: o_response.setCharacterEncoding("UTF-8");
085:
086: SyndFeedOutput syndOut = new SyndFeedOutput();
087: syndOut.output(feed, o_response.getWriter());
088: }
089:
090: /**
091: * Subclasses must implement this method to create an RSS Feed
092: * given the model.
093: * @param model the model Map
094: * @param feed the RSS Feed
095: * @param request in case we need locale etc. Shouldn't look at attributes.
096: * @param response in case we need to set cookies. Shouldn't write to it.
097: */
098: protected abstract void buildRSSFeed(Map i_model, SyndFeed i_feed,
099: HttpServletRequest i_request, HttpServletResponse o_response)
100: throws Exception;
101: }
|