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.io.OutputStreamWriter;
020import java.util.Map;
021import javax.servlet.ServletOutputStream;
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025import com.rometools.rome.feed.WireFeed;
026import com.rometools.rome.io.WireFeedOutput;
027
028import org.springframework.util.StringUtils;
029import org.springframework.web.servlet.view.AbstractView;
030
031/**
032 * Abstract base class for Atom and RSS Feed views, using the
033 * <a href="https://github.com/rometools/rome">ROME</a> package.
034 *
035 * <p>><b>NOTE: As of Spring 4.1, this is based on the {@code com.rometools}
036 * variant of ROME, version 1.5. Please upgrade your build dependency.</b>
037 *
038 * <p>Application-specific view classes will typically extend from either
039 * {@link AbstractRssFeedView} or {@link AbstractAtomFeedView} instead of from this class.
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 AbstractRssFeedView
047 * @see AbstractAtomFeedView
048 */
049public abstract class AbstractFeedView<T extends WireFeed> extends AbstractView {
050
051        @Override
052        protected final void renderMergedOutputModel(
053                        Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
054                        throws Exception {
055
056                T wireFeed = newFeed();
057                buildFeedMetadata(model, wireFeed, request);
058                buildFeedEntries(model, wireFeed, request, response);
059
060                setResponseContentType(request, response);
061                if (!StringUtils.hasText(wireFeed.getEncoding())) {
062                        wireFeed.setEncoding("UTF-8");
063                }
064
065                WireFeedOutput feedOutput = new WireFeedOutput();
066                ServletOutputStream out = response.getOutputStream();
067                feedOutput.output(wireFeed, new OutputStreamWriter(out, wireFeed.getEncoding()));
068                out.flush();
069        }
070
071        /**
072         * Create a new feed to hold the entries.
073         * @return the newly created Feed instance
074         */
075        protected abstract T newFeed();
076
077        /**
078         * Populate the feed metadata (title, link, description, etc.).
079         * <p>Default is an empty implementation. Subclasses can override this method
080         * to add meta fields such as title, link description, etc.
081         * @param model the model, in case meta information must be populated from it
082         * @param feed the feed being populated
083         * @param request in case we need locale etc. Shouldn't look at attributes.
084         */
085        protected void buildFeedMetadata(Map<String, Object> model, T feed, HttpServletRequest request) {
086        }
087
088        /**
089         * Subclasses must implement this method to build feed entries, given the model.
090         * <p>Note that the passed-in HTTP response is just supposed to be used for
091         * setting cookies or other HTTP headers. The built feed itself will automatically
092         * get written to the response after this method returns.
093         * @param model the model Map
094         * @param feed the feed to add entries to
095         * @param request in case we need locale etc. Shouldn't look at attributes.
096         * @param response in case we need to set cookies. Shouldn't write to it.
097         * @throws Exception any exception that occurred during building
098         */
099        protected abstract void buildFeedEntries(Map<String, Object> model, T feed,
100                        HttpServletRequest request, HttpServletResponse response) throws Exception;
101
102}