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;
021
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025import com.rometools.rome.feed.rss.Channel;
026import com.rometools.rome.feed.rss.Item;
027
028import org.springframework.http.MediaType;
029
030/**
031 * Abstract superclass for RSS Feed views, using the
032 * <a href="https://github.com/rometools/rome">ROME</a> package.
033 *
034 * <p>><b>NOTE: As of Spring 4.1, this is based on the {@code com.rometools}
035 * variant of ROME, version 1.5. Please upgrade your build dependency.</b>
036 *
037 * <p>Application-specific view classes will extend this class.
038 * The view will be held in the subclass itself, not in a template.
039 * Main entry points are the {@link #buildFeedMetadata} and {@link #buildFeedItems}.
040 *
041 * <p>Thanks to Jettro Coenradie and Sergio Bossa for the original feed view prototype!
042 *
043 * @author Arjen Poutsma
044 * @author Juergen Hoeller
045 * @since 3.0
046 * @see #buildFeedMetadata
047 * @see #buildFeedItems
048 */
049public abstract class AbstractRssFeedView extends AbstractFeedView<Channel> {
050
051        public AbstractRssFeedView() {
052                setContentType(MediaType.APPLICATION_RSS_XML_VALUE);
053        }
054
055
056        /**
057         * Create a new Channel instance to hold the entries.
058         * <p>By default returns an RSS 2.0 channel, but the subclass can specify any channel.
059         */
060        @Override
061        protected Channel newFeed() {
062                return new Channel("rss_2.0");
063        }
064
065        /**
066         * Invokes {@link #buildFeedItems(Map, HttpServletRequest, HttpServletResponse)}
067         * to get a list of feed items.
068         */
069        @Override
070        protected final void buildFeedEntries(Map<String, Object> model, Channel channel,
071                        HttpServletRequest request, HttpServletResponse response) throws Exception {
072
073                List<Item> items = buildFeedItems(model, request, response);
074                channel.setItems(items);
075        }
076
077        /**
078         * Subclasses must implement this method to build feed items, given the model.
079         * <p>Note that the passed-in HTTP response is just supposed to be used for
080         * setting cookies or other HTTP headers. The built feed itself will automatically
081         * get written to the response after this method returns.
082         * @param model the model Map
083         * @param request  in case we need locale etc. Shouldn't look at attributes.
084         * @param response in case we need to set cookies. Shouldn't write to it.
085         * @return the feed items to be added to the feed
086         * @throws Exception any exception that occurred during document building
087         * @see Item
088         */
089        protected abstract List<Item> buildFeedItems(
090                        Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
091                        throws Exception;
092
093}