|
| 1 | +/* |
| 2 | + * Copyright 2008 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.servlet.view.feed; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import javax.servlet.http.HttpServletRequest; |
| 22 | +import javax.servlet.http.HttpServletResponse; |
| 23 | + |
| 24 | +import com.sun.syndication.feed.WireFeed; |
| 25 | +import com.sun.syndication.feed.atom.Entry; |
| 26 | +import com.sun.syndication.feed.atom.Feed; |
| 27 | + |
| 28 | +/** |
| 29 | + * Abstract superclass for Atom Feed views, using java.net's <a href="https://rome.dev.java.net/">ROME</a> package. |
| 30 | + * Application-specific view classes will extend this class. The view will be held in the subclass itself, not in a |
| 31 | + * template. |
| 32 | + * |
| 33 | + * <p/>Main entry points are the {@link #buildFeedMetadata(Map, WireFeed, HttpServletRequest)} and |
| 34 | + * {@link #buildFeedEntries(Map, HttpServletRequest, HttpServletResponse)}. |
| 35 | + * |
| 36 | + * @author Jettro Coenradie |
| 37 | + * @author Sergio Bossa |
| 38 | + * @author Arjen Poutsma |
| 39 | + * @since 3.0 |
| 40 | + * @see #buildFeedMetadata(Map, WireFeed, HttpServletRequest) |
| 41 | + * @see #buildFeedEntries(Map, HttpServletRequest, HttpServletResponse) |
| 42 | + */ |
| 43 | +public abstract class AbstractAtomFeedView extends AbstractFeedView<Feed> { |
| 44 | + |
| 45 | + public static final String DEFAULT_FEED_TYPE = "atom_1.0"; |
| 46 | + |
| 47 | + private String feedType = DEFAULT_FEED_TYPE; |
| 48 | + |
| 49 | + /** Sets the appropriate content type: "application/atom+xml". */ |
| 50 | + public AbstractAtomFeedView() { |
| 51 | + setContentType("application/atom+xml"); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the Rome feed type to use. |
| 56 | + * <p/> |
| 57 | + * Defaults to Atom 1.0. |
| 58 | + * |
| 59 | + * @see Feed#setFeedType(String) |
| 60 | + * @see #DEFAULT_FEED_TYPE |
| 61 | + */ |
| 62 | + public void setFeedType(String feedType) { |
| 63 | + this.feedType = feedType; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Create a new feed to hold the entries. |
| 68 | + * <p/> |
| 69 | + * By default returns an Atom 1.0 feed, but the subclass can specify any Feed. |
| 70 | + * |
| 71 | + * @return the newly created Feed instance |
| 72 | + * @see #setFeedType(String) |
| 73 | + * @see com.sun.syndication.feed.atom.Feed#Feed(String) |
| 74 | + */ |
| 75 | + @Override |
| 76 | + protected Feed newFeed() { |
| 77 | + return new Feed(feedType); |
| 78 | + } |
| 79 | + |
| 80 | + /** Invokes {@link #buildFeedEntries(Map, HttpServletRequest, HttpServletResponse)} to get a list of feed entries. */ |
| 81 | + @Override |
| 82 | + protected final void buildFeedEntries(Map model, |
| 83 | + Feed feed, |
| 84 | + HttpServletRequest request, |
| 85 | + HttpServletResponse response) throws Exception { |
| 86 | + List<Entry> entries = buildFeedEntries(model, request, response); |
| 87 | + feed.setEntries(entries); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Subclasses must implement this method to build feed entries, given the model. |
| 92 | + * <p/> |
| 93 | + * Note that the passed-in HTTP response is just supposed to be used for setting cookies or other HTTP headers. The |
| 94 | + * built feed itself will automatically get written to the response after this method returns. |
| 95 | + * |
| 96 | + * @param model the model Map |
| 97 | + * @param request in case we need locale etc. Shouldn't look at attributes. |
| 98 | + * @param response in case we need to set cookies. Shouldn't write to it. |
| 99 | + * @return the feed entries to be added to the feed |
| 100 | + * @throws Exception any exception that occured during document building |
| 101 | + * @see Entry |
| 102 | + */ |
| 103 | + protected abstract List<Entry> buildFeedEntries(Map model, HttpServletRequest request, HttpServletResponse response) |
| 104 | + throws Exception; |
| 105 | +} |
0 commit comments