首页 > 代码库 > spring使用rssfeed

spring使用rssfeed

spring使用rssfeed

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import mypackage.rss.view.RssFeedView;

/**
 * 栏目稿件RSS订阅请求控制器类。
 *
 */
@Controller
@RequestMapping({"/rss/c/{columnId}"})
public class ColumnStoriesRssController {

    @RequestMapping("")
    public ModelAndView feed() {
        ModelAndView modelAndView = new ModelAndView();
        // TODO 从本地缓存获取稿件数据

        modelAndView.addObject(RssFeedView.ITEMS_KEY, null);
        modelAndView.setView(new RssFeedView());
        return modelAndView;
    }
}

 RSS Feed 视图类

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

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

import org.jdom2.Element;
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;

import mypackage.domain.model.NrCompoStory;
import mypackage.domain.model.NrGalleryStory;
import mypackage.domain.model.NrStory;
import mypackage.domain.model.NrVideoStory;
import mypackage.framework.infras.exception.UnexpectedException;
import com.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Item;

/**
 * RSS Feed 视图类。
 *
 */
public class RssFeedView extends AbstractRssFeedView {

    /**
     * RSS稿件列表KEY
     */
    public static final String ITEMS_KEY = "RSS_ITEMS";

    /**
     * {@inheritDoc}
     */
    @Override
    protected Channel newFeed() {
        Channel channel = new Channel("rss_2.0");
        channel.setTitle("${title}");
        channel.setLink("${link}");
        channel.setEncoding("UTF-8");
        channel.setDescription("${description}");
        return channel;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<Item> buildFeedItems(Map<String, Object> model,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        @SuppressWarnings("unchecked")
        List<NrStory> stories = (List<NrStory>)model.get(ITEMS_KEY);
        List<Item> result = new ArrayList<Item>();
        if (stories != null) {
            for (NrStory story : stories) {
                result.add(createItem(story));
            }
        }

        return result;
    }

    /**
     * 通过稿件创建RSS Item
     *
     * @param story 稿件
     * @return RSS Item
     */
    private Item createItem(NrStory story) {
        Item result = null;
        if (story instanceof NrCompoStory) {
            result = createCompoItem((NrCompoStory)story);
        } else if (story instanceof NrGalleryStory) {
            result = createGalleryItem((NrGalleryStory)story);
        } else if (story instanceof NrVideoStory) {
            result = createVideoItem((NrVideoStory)story);
        } else {
            throw new UnexpectedException("unknow story type.(" + story.getClass().getName() + ")");
        }

        return result;
    }

    /**
     * 通过图文稿件创建RSS Item
     *
     * @param story 稿件
     * @return RSS Item
     */
    private Item createCompoItem(NrCompoStory story) {
        Item result = new Item();
        result.setTitle("");
        result.setLink("");
        result.setDescription(null);
        result.setPubDate(null);
        Element element = null;


//        <videoItems>
//        <videoItem>
//        (see detail in next page)
//        </videoItem>
//        …...
//        </videoItems>
        result.setForeignMarkup(Arrays.asList(element));

        return result;
    }

    /**
     * 通过图集稿件创建RSS Item
     *
     * @param story 稿件
     * @return RSS Item
     */
    private Item createGalleryItem(NrGalleryStory story) {


        return null;
    }

    /**
     * 通过视频稿件创建RSS Item
     *
     * @param story 稿件
     * @return RSS Item
     */
    private Item createVideoItem(NrVideoStory story) {


        return null;
    }
}

 

spring使用rssfeed