001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.view.feed;
018
019import java.util.List;
020import java.util.Map;
021import javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpServletResponse;
023
024import com.rometools.rome.feed.rss.Channel;
025import com.rometools.rome.feed.rss.Item;
026
027import org.springframework.http.MediaType;
028
029/**
030 * Abstract superclass for RSS Feed views, using the
031 * <a href="https://github.com/rometools/rome">ROME</a> package.
032 *
033 * <p>><b>NOTE: As of Spring 4.1, this is based on the {@code com.rometools}
034 * variant of ROME, version 1.5. Please upgrade your build dependency.</b>
035 *
036 * <p>Application-specific view classes will extend this class.
037 * The view will be held in the subclass itself, not in a template.
038 * Main entry points are the {@link #buildFeedMetadata} and {@link #buildFeedItems}.
039 *
040 * <p>Thanks to Jettro Coenradie and Sergio Bossa for the original feed view prototype!
041 *
042 * @author Arjen Poutsma
043 * @author Juergen Hoeller
044 * @since 3.0
045 * @see #buildFeedMetadata
046 * @see #buildFeedItems
047 */
048public abstract class AbstractRssFeedView extends AbstractFeedView<Channel> {
049
050        public AbstractRssFeedView() {
051                setContentType(MediaType.APPLICATION_RSS_XML_VALUE);
052        }
053
054
055        /**
056         * Create a new Channel instance to hold the entries.
057         * <p>By default returns an RSS 2.0 channel, but the subclass can specify any channel.
058         */
059        @Override
060        protected Channel newFeed() {
061                return new Channel("rss_2.0");
062        }
063
064        /**
065         * Invokes {@link #buildFeedItems(Map, HttpServletRequest, HttpServletResponse)}
066         * to get a list of feed items.
067         */
068        @Override
069        protected final void buildFeedEntries(Map<String, Object> model, Channel channel,
070                        HttpServletRequest request, HttpServletResponse response) throws Exception {
071
072                List<Item> items = buildFeedItems(model, request, response);
073                channel.setItems(items);
074        }
075
076        /**
077         * Subclasses must implement this method to build feed items, given the model.
078         * <p>Note that the passed-in HTTP response is just supposed to be used for
079         * setting cookies or other HTTP headers. The built feed itself will automatically
080         * get written to the response after this method returns.
081         * @param model the model Map
082         * @param request  in case we need locale etc. Shouldn't look at attributes.
083         * @param response in case we need to set cookies. Shouldn't write to it.
084         * @return the feed items to be added to the feed
085         * @throws Exception any exception that occurred during document building
086         * @see Item
087         */
088        protected abstract List<Item> buildFeedItems(
089                        Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
090                        throws Exception;
091
092}