AbstractRSSView.java :  » Project-Management » EmForce » ru » emdev » EmForge » rss » Java Open Source

Java Open Source » Project Management » EmForce 
EmForce » ru » emdev » EmForge » rss » AbstractRSSView.java
package ru.emdev.EmForge.rss;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.AbstractView;

import ru.emdev.EmForge.EmForgeContext;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.feed.synd.SyndImage;
import com.sun.syndication.feed.synd.SyndImageImpl;
import com.sun.syndication.io.SyndFeedOutput;

/** Abstract RSS View
 * 
 * This class is used as base class for all RSS View in EmForge Project
 * @note Initially I wrote this code based on zabada's recipes project example
 * @note But I decided to use Rome instead of RSSLibJ since it is more up-to-dated project 
 */
public abstract class AbstractRSSView extends AbstractView {
  /// feed description
  protected String   m_description;
  /// feed link
  protected String   m_link;
  /// feed title
  protected String   m_title;
  /// feed's image path
  protected String   m_imagePath;

  protected EmForgeContext
            m_appContext;
  
  public void setDescription(String description) {
    m_description = description;
  }
  
  public void setLink(String link) {
    m_link = link;
  }

  public void setTitle(String title) {
    m_title = title;
  }
  
  public void setImagePath(String imagePath) {
    m_imagePath = imagePath;
  }

  public void setAppContext(EmForgeContext i_appContext) {
    m_appContext = i_appContext;
  }
  
  public AbstractRSSView() {
    setContentType("text/xml; charset=UTF-8");
  }

    /**
     * Renders the view given the specified model.
     */
    protected final void renderMergedOutputModel(Map i_model, 
                           HttpServletRequest i_request, 
                           HttpServletResponse o_response) throws Exception {
      SyndFeed feed = new SyndFeedImpl();
      
        buildRSSFeed(i_model, feed, i_request, o_response);

        feed.setFeedType("rss_2.0");
        
        feed.setDescription(m_description);
        feed.setLink(m_link);
        feed.setTitle(m_title);
        
        SyndImage image = new SyndImageImpl();
        image.setLink(m_link);
        image.setTitle(m_title);
        image.setUrl(m_imagePath);
        image.setDescription(m_description);
        feed.setImage(image);
        
        o_response.setContentType(getContentType());
        o_response.setCharacterEncoding("UTF-8");
        
        SyndFeedOutput syndOut = new SyndFeedOutput();
        syndOut.output(feed, o_response.getWriter());
  }

  /**
   * Subclasses must implement this method to create an RSS Feed
   * given the model.
   * @param model the model Map
   * @param feed the RSS Feed
   * @param request in case we need locale etc. Shouldn't look at attributes.
   * @param response in case we need to set cookies. Shouldn't write to it.
   */
  protected abstract void buildRSSFeed(Map i_model, 
                      SyndFeed i_feed, 
                      HttpServletRequest i_request, 
                      HttpServletResponse o_response) throws Exception;
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.