Skip to content

Commit baac76e

Browse files
committed
SPR-5249: Atom and RSS View
1 parent c920c93 commit baac76e

File tree

9 files changed

+487
-1
lines changed

9 files changed

+487
-1
lines changed

org.springframework.testsuite/ivy.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<dependencies>
2525
<dependency org="com.ibm.websphere" name="com.springsource.com.ibm.websphere.uow" rev="6.0.2.17" conf="test->compile"/>
2626
<dependency org="com.opensymphony.quartz" name="com.springsource.org.quartz" rev="1.6.2" conf="test->compile"/>
27+
<dependency org="com.sun.syndication" name="com.springsource.com.sun.syndication" rev="0.9.0" conf="test->compile"/>
2728
<dependency org="edu.emory.mathcs.backport" name="com.springsource.edu.emory.mathcs.backport" rev="3.0.0" conf="test->compile"/>
2829
<dependency org="javax.el" name="com.springsource.javax.el" rev="2.1.0" conf="test->compile"/>
2930
<dependency org="javax.faces" name="com.springsource.javax.faces" rev="1.2.0.08" conf="test->compile"/>
@@ -74,7 +75,8 @@
7475
<!-- test dependencies -->
7576
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.4.0" conf="test->runtime" />
7677
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="test->compile"/>
77-
78+
<dependency org="org.custommonkey.xmlunit" name="com.springsource.org.custommonkey.xmlunit" rev="1.2.0" conf="test->compile"/>
79+
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->compile"/>
7880
</dependencies>
7981

8082
</ivy-module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright ${YEAR} 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.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.Iterator;
22+
import java.util.List;
23+
import java.util.Map;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
import com.sun.syndication.feed.atom.Content;
28+
import com.sun.syndication.feed.atom.Entry;
29+
import com.sun.syndication.feed.atom.Feed;
30+
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
31+
import static org.custommonkey.xmlunit.XMLUnit.setIgnoreWhitespace;
32+
import static org.junit.Assert.assertEquals;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
36+
import org.springframework.mock.web.MockHttpServletRequest;
37+
import org.springframework.mock.web.MockHttpServletResponse;
38+
39+
public class AtomFeedViewTest {
40+
41+
private AbstractAtomFeedView view;
42+
43+
@Before
44+
public void createView() throws Exception {
45+
view = new MyAtomFeedView();
46+
setIgnoreWhitespace(true);
47+
}
48+
49+
@Test
50+
public void render() throws Exception {
51+
MockHttpServletRequest request = new MockHttpServletRequest();
52+
MockHttpServletResponse response = new MockHttpServletResponse();
53+
54+
Map<String, String> model = new HashMap<String, String>();
55+
model.put("1", "This is entry 1");
56+
model.put("2", "This is entry 2");
57+
58+
view.render(model, request, response);
59+
assertEquals("Invalid content-type", "application/atom+xml", response.getContentType());
60+
String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>Test Feed</title>" +
61+
"<entry><title>2</title><summary>This is entry 2</summary></entry>" +
62+
"<entry><title>1</title><summary>This is entry 1</summary></entry>" + "</feed>";
63+
assertXMLEqual(expected, response.getContentAsString());
64+
}
65+
66+
private static class MyAtomFeedView extends AbstractAtomFeedView {
67+
68+
@Override
69+
protected void buildFeedMetadata(Map model, Feed feed, HttpServletRequest request) {
70+
feed.setTitle("Test Feed");
71+
}
72+
73+
@Override
74+
protected List<Entry> buildFeedEntries(Map model, HttpServletRequest request, HttpServletResponse response)
75+
throws Exception {
76+
List<Entry> entries = new ArrayList<Entry>();
77+
for (Iterator iterator = model.keySet().iterator(); iterator.hasNext();) {
78+
String name = (String) iterator.next();
79+
Entry entry = new Entry();
80+
entry.setTitle(name);
81+
Content content = new Content();
82+
content.setValue((String) model.get(name));
83+
entry.setSummary(content);
84+
entries.add(entry);
85+
}
86+
return entries;
87+
}
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright ${YEAR} 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.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.Iterator;
22+
import java.util.List;
23+
import java.util.Map;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
import com.sun.syndication.feed.rss.Channel;
28+
import com.sun.syndication.feed.rss.Description;
29+
import com.sun.syndication.feed.rss.Item;
30+
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
31+
import static org.custommonkey.xmlunit.XMLUnit.setIgnoreWhitespace;
32+
import static org.junit.Assert.assertEquals;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
36+
import org.springframework.mock.web.MockHttpServletRequest;
37+
import org.springframework.mock.web.MockHttpServletResponse;
38+
39+
public class RssFeedViewTest {
40+
41+
private AbstractRssFeedView view;
42+
43+
@Before
44+
public void createView() throws Exception {
45+
view = new MyRssFeedView();
46+
setIgnoreWhitespace(true);
47+
48+
}
49+
50+
@Test
51+
public void render() throws Exception {
52+
MockHttpServletRequest request = new MockHttpServletRequest();
53+
MockHttpServletResponse response = new MockHttpServletResponse();
54+
55+
Map<String, String> model = new HashMap<String, String>();
56+
model.put("1", "This is entry 1");
57+
model.put("2", "This is entry 2");
58+
59+
view.render(model, request, response);
60+
assertEquals("Invalid content-type", "application/rss+xml", response.getContentType());
61+
String expected = "<rss version=\"2.0\">" +
62+
"<channel><title>Test Feed</title><link>http://example.com</link><description>Test feed description</description>" +
63+
"<item><title>2</title><description>This is entry 2</description></item>" +
64+
"<item><title>1</title><description>This is entry 1</description></item>" + "</channel></rss>";
65+
assertXMLEqual(expected, response.getContentAsString());
66+
}
67+
68+
private static class MyRssFeedView extends AbstractRssFeedView {
69+
70+
@Override
71+
protected void buildFeedMetadata(Map model, Channel channel, HttpServletRequest request) {
72+
channel.setTitle("Test Feed");
73+
channel.setDescription("Test feed description");
74+
channel.setLink("http://example.com");
75+
}
76+
77+
@Override
78+
protected List<Item> buildFeedItems(Map model, HttpServletRequest request, HttpServletResponse response)
79+
throws Exception {
80+
List<Item> items = new ArrayList<Item>();
81+
for (Iterator iterator = model.keySet().iterator(); iterator.hasNext();) {
82+
String name = (String) iterator.next();
83+
Item item = new Item();
84+
item.setTitle(name);
85+
Description description = new Description();
86+
description.setValue((String) model.get(name));
87+
item.setDescription(description);
88+
items.add(item);
89+
}
90+
return items;
91+
}
92+
}
93+
}

org.springframework.web.servlet/ivy.xml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<include file="${spring.build.dir}/common/default-ivy-configurations.xml"/>
1414
<conf name="commons-attributes" extends="runtime" description="JARs needed to run with Commons Attributes"/>
1515
<conf name="commons-fileupload" extends="runtime" description="JARs needed to run with Commons Fileupload"/>
16+
<conf name="feed" extends="runtime" description="JARs needed to create beans for Atom and RSS feeds"/>
1617
<conf name="freemarker" extends="runtime" description="JARs needed to create beans for Freemarker"/>
1718
<conf name="itext" extends="runtime" description="JARs needed to create beans for iText"/>
1819
<conf name="jasper-reports" extends="runtime" description="JARs needed to create beans for Jasper Reports"/>
@@ -28,6 +29,7 @@
2829
</publications>
2930

3031
<dependencies>
32+
<dependency org="com.sun.syndication" name="com.springsource.com.sun.syndication" rev="0.9.0" conf="optional, feed->compile"/>
3133
<dependency org="com.lowagie.text" name="com.springsource.com.lowagie.text" rev="2.0.8" conf="optional, itext->compile"/>
3234
<dependency org="org.freemarker" name="com.springsource.freemarker" rev="2.3.12" conf="optional, freemarker->compile"/>
3335
<dependency org="javax.servlet" name="com.springsource.javax.servlet" rev="2.4.0" conf="provided->compile"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)