001/*
002 * Copyright 2002-2018 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.atom.Entry;
026import com.rometools.rome.feed.atom.Feed;
027
028/**
029 * Abstract superclass for Atom Feed views, using the
030 * <a href="https://github.com/rometools/rome">ROME</a> package.
031 *
032 * <p>><b>NOTE: As of Spring 4.1, this is based on the {@code com.rometools}
033 * variant of ROME, version 1.5. Please upgrade your build dependency.</b>
034 *
035 * <p>Application-specific view classes will extend this class.
036 * The view will be held in the subclass itself, not in a template.
037 * Main entry points are the {@link #buildFeedMetadata} and {@link #buildFeedEntries}.
038 *
039 * <p>Thanks to Jettro Coenradie and Sergio Bossa for the original feed view prototype!
040 *
041 * @author Arjen Poutsma
042 * @author Juergen Hoeller
043 * @since 3.0
044 * @see #buildFeedMetadata
045 * @see #buildFeedEntries
046 * @see <a href="https://www.atomenabled.org/developers/syndication/">Atom Syndication Format</a>
047 */
048public abstract class AbstractAtomFeedView extends AbstractFeedView<Feed> {
049
050        /**
051         * The default feed type used.
052         */
053        public static final String DEFAULT_FEED_TYPE = "atom_1.0";
054
055        private String feedType = DEFAULT_FEED_TYPE;
056
057
058        public AbstractAtomFeedView() {
059                setContentType("application/atom+xml");
060        }
061
062        /**
063         * Set the Rome feed type to use.
064         * <p>Defaults to Atom 1.0.
065         * @see Feed#setFeedType(String)
066         * @see #DEFAULT_FEED_TYPE
067         */
068        public void setFeedType(String feedType) {
069                this.feedType = feedType;
070        }
071
072        /**
073         * Create a new Feed instance to hold the entries.
074         * <p>By default returns an Atom 1.0 feed, but the subclass can specify any Feed.
075         * @see #setFeedType(String)
076         */
077        @Override
078        protected Feed newFeed() {
079                return new Feed(this.feedType);
080        }
081
082        /**
083         * Invokes {@link #buildFeedEntries(Map, HttpServletRequest, HttpServletResponse)}
084         * to get a list of feed entries.
085         */
086        @Override
087        protected final void buildFeedEntries(Map<String, Object> model, Feed feed,
088                        HttpServletRequest request, HttpServletResponse response) throws Exception {
089
090                List<Entry> entries = buildFeedEntries(model, request, response);
091                feed.setEntries(entries);
092        }
093
094        /**
095         * Subclasses must implement this method to build feed entries, given the model.
096         * <p>Note that the passed-in HTTP response is just supposed to be used for
097         * setting cookies or other HTTP headers. The built feed itself will automatically
098         * get written to the response after this method returns.
099         * @param model the model Map
100         * @param request in case we need locale etc. Shouldn't look at attributes.
101         * @param response in case we need to set cookies. Shouldn't write to it.
102         * @return the feed entries to be added to the feed
103         * @throws Exception any exception that occurred during document building
104         * @see Entry
105         */
106        protected abstract List<Entry> buildFeedEntries(
107                        Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
108                        throws Exception;
109
110}