Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@253 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
9 changed files with 487 additions and 1 deletions
@ -0,0 +1,89 @@ |
|||||||
|
/* |
||||||
|
* Copyright ${YEAR} the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.web.servlet.view.feed; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import com.sun.syndication.feed.atom.Content; |
||||||
|
import com.sun.syndication.feed.atom.Entry; |
||||||
|
import com.sun.syndication.feed.atom.Feed; |
||||||
|
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; |
||||||
|
import static org.custommonkey.xmlunit.XMLUnit.setIgnoreWhitespace; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest; |
||||||
|
import org.springframework.mock.web.MockHttpServletResponse; |
||||||
|
|
||||||
|
public class AtomFeedViewTest { |
||||||
|
|
||||||
|
private AbstractAtomFeedView view; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void createView() throws Exception { |
||||||
|
view = new MyAtomFeedView(); |
||||||
|
setIgnoreWhitespace(true); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void render() throws Exception { |
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
||||||
|
|
||||||
|
Map<String, String> model = new HashMap<String, String>(); |
||||||
|
model.put("1", "This is entry 1"); |
||||||
|
model.put("2", "This is entry 2"); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
assertEquals("Invalid content-type", "application/atom+xml", response.getContentType()); |
||||||
|
String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>Test Feed</title>" + |
||||||
|
"<entry><title>2</title><summary>This is entry 2</summary></entry>" + |
||||||
|
"<entry><title>1</title><summary>This is entry 1</summary></entry>" + "</feed>"; |
||||||
|
assertXMLEqual(expected, response.getContentAsString()); |
||||||
|
} |
||||||
|
|
||||||
|
private static class MyAtomFeedView extends AbstractAtomFeedView { |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void buildFeedMetadata(Map model, Feed feed, HttpServletRequest request) { |
||||||
|
feed.setTitle("Test Feed"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected List<Entry> buildFeedEntries(Map model, HttpServletRequest request, HttpServletResponse response) |
||||||
|
throws Exception { |
||||||
|
List<Entry> entries = new ArrayList<Entry>(); |
||||||
|
for (Iterator iterator = model.keySet().iterator(); iterator.hasNext();) { |
||||||
|
String name = (String) iterator.next(); |
||||||
|
Entry entry = new Entry(); |
||||||
|
entry.setTitle(name); |
||||||
|
Content content = new Content(); |
||||||
|
content.setValue((String) model.get(name)); |
||||||
|
entry.setSummary(content); |
||||||
|
entries.add(entry); |
||||||
|
} |
||||||
|
return entries; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,93 @@ |
|||||||
|
/* |
||||||
|
* Copyright ${YEAR} the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.web.servlet.view.feed; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import com.sun.syndication.feed.rss.Channel; |
||||||
|
import com.sun.syndication.feed.rss.Description; |
||||||
|
import com.sun.syndication.feed.rss.Item; |
||||||
|
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; |
||||||
|
import static org.custommonkey.xmlunit.XMLUnit.setIgnoreWhitespace; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest; |
||||||
|
import org.springframework.mock.web.MockHttpServletResponse; |
||||||
|
|
||||||
|
public class RssFeedViewTest { |
||||||
|
|
||||||
|
private AbstractRssFeedView view; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void createView() throws Exception { |
||||||
|
view = new MyRssFeedView(); |
||||||
|
setIgnoreWhitespace(true); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void render() throws Exception { |
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
||||||
|
|
||||||
|
Map<String, String> model = new HashMap<String, String>(); |
||||||
|
model.put("1", "This is entry 1"); |
||||||
|
model.put("2", "This is entry 2"); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
assertEquals("Invalid content-type", "application/rss+xml", response.getContentType()); |
||||||
|
String expected = "<rss version=\"2.0\">" + |
||||||
|
"<channel><title>Test Feed</title><link>http://example.com</link><description>Test feed description</description>" + |
||||||
|
"<item><title>2</title><description>This is entry 2</description></item>" + |
||||||
|
"<item><title>1</title><description>This is entry 1</description></item>" + "</channel></rss>"; |
||||||
|
assertXMLEqual(expected, response.getContentAsString()); |
||||||
|
} |
||||||
|
|
||||||
|
private static class MyRssFeedView extends AbstractRssFeedView { |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void buildFeedMetadata(Map model, Channel channel, HttpServletRequest request) { |
||||||
|
channel.setTitle("Test Feed"); |
||||||
|
channel.setDescription("Test feed description"); |
||||||
|
channel.setLink("http://example.com"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected List<Item> buildFeedItems(Map model, HttpServletRequest request, HttpServletResponse response) |
||||||
|
throws Exception { |
||||||
|
List<Item> items = new ArrayList<Item>(); |
||||||
|
for (Iterator iterator = model.keySet().iterator(); iterator.hasNext();) { |
||||||
|
String name = (String) iterator.next(); |
||||||
|
Item item = new Item(); |
||||||
|
item.setTitle(name); |
||||||
|
Description description = new Description(); |
||||||
|
description.setValue((String) model.get(name)); |
||||||
|
item.setDescription(description); |
||||||
|
items.add(item); |
||||||
|
} |
||||||
|
return items; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,105 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2008 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.web.servlet.view.feed; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import com.sun.syndication.feed.WireFeed; |
||||||
|
import com.sun.syndication.feed.atom.Entry; |
||||||
|
import com.sun.syndication.feed.atom.Feed; |
||||||
|
|
||||||
|
/** |
||||||
|
* Abstract superclass for Atom Feed views, using java.net's <a href="https://rome.dev.java.net/">ROME</a> package. |
||||||
|
* Application-specific view classes will extend this class. The view will be held in the subclass itself, not in a |
||||||
|
* template. |
||||||
|
* |
||||||
|
* <p/>Main entry points are the {@link #buildFeedMetadata(Map, WireFeed, HttpServletRequest)} and |
||||||
|
* {@link #buildFeedEntries(Map, HttpServletRequest, HttpServletResponse)}. |
||||||
|
* |
||||||
|
* @author Jettro Coenradie |
||||||
|
* @author Sergio Bossa |
||||||
|
* @author Arjen Poutsma |
||||||
|
* @since 3.0 |
||||||
|
* @see #buildFeedMetadata(Map, WireFeed, HttpServletRequest) |
||||||
|
* @see #buildFeedEntries(Map, HttpServletRequest, HttpServletResponse) |
||||||
|
*/ |
||||||
|
public abstract class AbstractAtomFeedView extends AbstractFeedView<Feed> { |
||||||
|
|
||||||
|
public static final String DEFAULT_FEED_TYPE = "atom_1.0"; |
||||||
|
|
||||||
|
private String feedType = DEFAULT_FEED_TYPE; |
||||||
|
|
||||||
|
/** Sets the appropriate content type: "application/atom+xml". */ |
||||||
|
public AbstractAtomFeedView() { |
||||||
|
setContentType("application/atom+xml"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the Rome feed type to use. |
||||||
|
* <p/> |
||||||
|
* Defaults to Atom 1.0. |
||||||
|
* |
||||||
|
* @see Feed#setFeedType(String) |
||||||
|
* @see #DEFAULT_FEED_TYPE |
||||||
|
*/ |
||||||
|
public void setFeedType(String feedType) { |
||||||
|
this.feedType = feedType; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new feed to hold the entries. |
||||||
|
* <p/> |
||||||
|
* By default returns an Atom 1.0 feed, but the subclass can specify any Feed. |
||||||
|
* |
||||||
|
* @return the newly created Feed instance |
||||||
|
* @see #setFeedType(String) |
||||||
|
* @see com.sun.syndication.feed.atom.Feed#Feed(String) |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
protected Feed newFeed() { |
||||||
|
return new Feed(feedType); |
||||||
|
} |
||||||
|
|
||||||
|
/** Invokes {@link #buildFeedEntries(Map, HttpServletRequest, HttpServletResponse)} to get a list of feed entries. */ |
||||||
|
@Override |
||||||
|
protected final void buildFeedEntries(Map model, |
||||||
|
Feed feed, |
||||||
|
HttpServletRequest request, |
||||||
|
HttpServletResponse response) throws Exception { |
||||||
|
List<Entry> entries = buildFeedEntries(model, request, response); |
||||||
|
feed.setEntries(entries); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Subclasses must implement this method to build feed entries, given the model. |
||||||
|
* <p/> |
||||||
|
* Note that the passed-in HTTP response is just supposed to be used for setting cookies or other HTTP headers. The |
||||||
|
* built feed itself will automatically get written to the response after this method returns. |
||||||
|
* |
||||||
|
* @param model the model Map |
||||||
|
* @param request in case we need locale etc. Shouldn't look at attributes. |
||||||
|
* @param response in case we need to set cookies. Shouldn't write to it. |
||||||
|
* @return the feed entries to be added to the feed |
||||||
|
* @throws Exception any exception that occured during document building |
||||||
|
* @see Entry |
||||||
|
*/ |
||||||
|
protected abstract List<Entry> buildFeedEntries(Map model, HttpServletRequest request, HttpServletResponse response) |
||||||
|
throws Exception; |
||||||
|
} |
||||||
@ -0,0 +1,101 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2008 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.web.servlet.view.feed; |
||||||
|
|
||||||
|
import java.io.OutputStreamWriter; |
||||||
|
import java.util.Map; |
||||||
|
import javax.servlet.ServletOutputStream; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import com.sun.syndication.feed.WireFeed; |
||||||
|
import com.sun.syndication.io.WireFeedOutput; |
||||||
|
|
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
import org.springframework.web.servlet.view.AbstractView; |
||||||
|
|
||||||
|
/** |
||||||
|
* Abstract base class for Atom and RSS Feed views, using java.net's <a href="https://rome.dev.java.net/">ROME</a> |
||||||
|
* package. TypApplication-specific view classes will typically extends either {@link AbstractRssFeedView} or {@link |
||||||
|
* AbstractAtomFeedView}, not this class. |
||||||
|
* |
||||||
|
* @author Jettro Coenradie |
||||||
|
* @author Sergio Bossa |
||||||
|
* @author Arjen Poutsma |
||||||
|
* @see AbstractRssFeedView |
||||||
|
* @see AbstractAtomFeedView |
||||||
|
* @since 3.0 |
||||||
|
*/ |
||||||
|
public abstract class AbstractFeedView<T extends WireFeed> extends AbstractView { |
||||||
|
|
||||||
|
@Override |
||||||
|
protected final void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) |
||||||
|
throws Exception { |
||||||
|
T wireFeed = newFeed(); |
||||||
|
buildFeedMetadata(model, wireFeed, request); |
||||||
|
buildFeedEntries(model, wireFeed, request, response); |
||||||
|
|
||||||
|
response.setContentType(getContentType()); |
||||||
|
if (!StringUtils.hasText(wireFeed.getEncoding())) { |
||||||
|
wireFeed.setEncoding("UTF-8"); |
||||||
|
} |
||||||
|
|
||||||
|
WireFeedOutput feedOutput = new WireFeedOutput(); |
||||||
|
ServletOutputStream out = response.getOutputStream(); |
||||||
|
feedOutput.output(wireFeed, new OutputStreamWriter(out, wireFeed.getEncoding())); |
||||||
|
out.flush(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new feed to hold the entries. |
||||||
|
* |
||||||
|
* @return the newly created Feed instance |
||||||
|
*/ |
||||||
|
protected abstract T newFeed(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Populate the feed metadata (title, link, description, etc.). |
||||||
|
* <p/> |
||||||
|
* Default is an empty implementation. Subclasses can override this method to add meta fields such as title, link |
||||||
|
* description, etc. |
||||||
|
* |
||||||
|
* @param model the model, in case meta information must be populated from it |
||||||
|
* @param feed the feed being populated |
||||||
|
* @param request in case we need locale etc. Shouldn't look at attributes. |
||||||
|
*/ |
||||||
|
protected void buildFeedMetadata(Map model, T feed, HttpServletRequest request) { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Subclasses must implement this method to build feed entries, given the model. |
||||||
|
* <p/> |
||||||
|
* Note that the passed-in HTTP response is just supposed to be used for setting cookies or other HTTP headers. The |
||||||
|
* built feed itself will automatically get written to the response after this method returns. |
||||||
|
* |
||||||
|
* @param model the model Map |
||||||
|
* @param feed the feed to add entries to |
||||||
|
* @param request in case we need locale etc. Shouldn't look at attributes. |
||||||
|
* @param response in case we need to set cookies. Shouldn't write to it. |
||||||
|
* @throws Exception any exception that occured during building |
||||||
|
*/ |
||||||
|
protected abstract void buildFeedEntries(Map model, |
||||||
|
T feed, |
||||||
|
HttpServletRequest request, |
||||||
|
HttpServletResponse response) throws Exception; |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,88 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2008 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.web.servlet.view.feed; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import com.sun.syndication.feed.WireFeed; |
||||||
|
import com.sun.syndication.feed.rss.Channel; |
||||||
|
import com.sun.syndication.feed.rss.Item; |
||||||
|
|
||||||
|
/** |
||||||
|
* Abstract superclass for RSS Feed views, using java.net's <a href="https://rome.dev.java.net/">ROME</a> package. |
||||||
|
* Application-specific view classes will extend this class. The view will be held in the subclass itself, not in a |
||||||
|
* template. |
||||||
|
* |
||||||
|
* <p/>Main entry points are the {@link #buildFeedMetadata(Map, WireFeed , HttpServletRequest)} and {@link |
||||||
|
* #buildFeedItems(Map, HttpServletRequest, HttpServletResponse)}. |
||||||
|
* |
||||||
|
* @author Jettro Coenradie |
||||||
|
* @author Sergio Bossa |
||||||
|
* @author Arjen Poutsma |
||||||
|
* @see #buildFeedMetadata(Map, WireFeed , HttpServletRequest) |
||||||
|
* @see #buildFeedItems(Map, HttpServletRequest, HttpServletResponse) |
||||||
|
* @since 3.0 |
||||||
|
*/ |
||||||
|
public abstract class AbstractRssFeedView extends AbstractFeedView<Channel> { |
||||||
|
|
||||||
|
/** Sets the appropriate content type: "application/rss+xml". */ |
||||||
|
protected AbstractRssFeedView() { |
||||||
|
setContentType("application/rss+xml"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new channel to hold the entries. |
||||||
|
* |
||||||
|
* <p/>By default returns an RSS 2.0 channel, but the subclass can specify any channel. |
||||||
|
* |
||||||
|
* @return the newly created Feed instance |
||||||
|
* @see Channel#Channel(String) |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
protected Channel newFeed() { |
||||||
|
return new Channel("rss_2.0"); |
||||||
|
} |
||||||
|
|
||||||
|
/** Invokes {@link #buildFeedItems(Map, HttpServletRequest, HttpServletResponse)} to get a list of feed items. */ |
||||||
|
@Override |
||||||
|
protected final void buildFeedEntries(Map model, |
||||||
|
Channel channel, |
||||||
|
HttpServletRequest request, |
||||||
|
HttpServletResponse response) throws Exception { |
||||||
|
List<Item> items = buildFeedItems(model, request, response); |
||||||
|
channel.setItems(items); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Subclasses must implement this method to build feed items, given the model. |
||||||
|
* |
||||||
|
* <p/>Note that the passed-in HTTP response is just supposed to be used for setting cookies or other HTTP headers. The |
||||||
|
* built feed itself will automatically get written to the response after this method returns. |
||||||
|
* |
||||||
|
* @param model the model Map |
||||||
|
* @param request in case we need locale etc. Shouldn't look at attributes. |
||||||
|
* @param response in case we need to set cookies. Shouldn't write to it. |
||||||
|
* @return the feed items to be added to the feed |
||||||
|
* @throws Exception any exception that occured during document building |
||||||
|
* @see Item |
||||||
|
*/ |
||||||
|
protected abstract List<Item> buildFeedItems(Map model, HttpServletRequest request, HttpServletResponse response) |
||||||
|
throws Exception; |
||||||
|
} |
||||||
@ -0,0 +1,5 @@ |
|||||||
|
<html> |
||||||
|
<body> |
||||||
|
Support classes for feed generation, providing View implementations for Atom and RSS. |
||||||
|
</body> |
||||||
|
</html> |
||||||
Loading…
Reference in new issue