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