1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.samples.servlet;
18
19 import com.sun.syndication.feed.synd.*;
20 import com.sun.syndication.io.FeedException;
21 import com.sun.syndication.io.SyndFeedOutput;
22
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import java.io.IOException;
27 import java.text.DateFormat;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 /***
34 * Sample Servlet that serves a feed created with Rome.
35 * <p>
36 * The feed type is determined by the 'type' request parameter, if the parameter is missing it defaults
37 * to the 'default.feed.type' servlet init parameter, if the init parameter is missing it defaults to 'atom_0.3'
38 * <p>
39 * @author Alejandro Abdelnur
40 *
41 */
42 public class FeedServlet extends HttpServlet {
43 private static final String DEFAULT_FEED_TYPE = "default.feed.type";
44 private static final String FEED_TYPE = "type";
45 private static final String MIME_TYPE = "application/xml; charset=UTF-8";
46 private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
47
48 private static final String DATE_FORMAT = "yyyy-MM-dd";
49
50 private String _defaultFeedType;
51
52 public void init() {
53 _defaultFeedType = getServletConfig().getInitParameter(DEFAULT_FEED_TYPE);
54 _defaultFeedType = (_defaultFeedType!=null) ? _defaultFeedType : "atom_0.3";
55 }
56
57 public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException {
58 try {
59 SyndFeed feed = getFeed(req);
60
61 String feedType = req.getParameter(FEED_TYPE);
62 feedType = (feedType!=null) ? feedType : _defaultFeedType;
63 feed.setFeedType(feedType);
64
65 res.setContentType(MIME_TYPE);
66 SyndFeedOutput output = new SyndFeedOutput();
67 output.output(feed,res.getWriter());
68 }
69 catch (FeedException ex) {
70 String msg = COULD_NOT_GENERATE_FEED_ERROR;
71 log(msg,ex);
72 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg);
73 }
74 }
75
76 protected SyndFeed getFeed(HttpServletRequest req) throws IOException,FeedException {
77 DateFormat dateParser = new SimpleDateFormat(DATE_FORMAT);
78
79 SyndFeed feed = new SyndFeedImpl();
80
81 feed.setTitle("Sample Feed (created with Rome)");
82 feed.setLink("http://rome.dev.java.net");
83 feed.setDescription("This feed has been created using Rome (Java syndication utilities");
84
85 List entries = new ArrayList();
86 SyndEntry entry;
87 SyndContent description;
88
89 entry = new SyndEntryImpl();
90 entry.setTitle("Rome v0.1");
91 entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
92 try {
93 entry.setPublishedDate(dateParser.parse("2004-06-08"));
94 }
95 catch (ParseException ex) {
96
97 }
98 description = new SyndContentImpl();
99 description.setType("text/plain");
100 description.setValue("Initial release of Rome");
101 entry.setDescription(description);
102 entries.add(entry);
103
104 entry = new SyndEntryImpl();
105 entry.setTitle("Rome v0.2");
106 entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome02");
107 try {
108 entry.setPublishedDate(dateParser.parse("2004-06-16"));
109 }
110 catch (ParseException ex) {
111
112 }
113 description = new SyndContentImpl();
114 description.setType("text/plain");
115 description.setValue("Bug fixes, minor API changes and some new features"+
116 "<p>For details check the <a href=\"http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV02\">Changes Log for 0.2</a></p>");
117 entry.setDescription(description);
118 entries.add(entry);
119
120 entry = new SyndEntryImpl();
121 entry.setTitle("Rome v0.3");
122 entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome03");
123 try {
124 entry.setPublishedDate(dateParser.parse("2004-07-27"));
125 }
126 catch (ParseException ex) {
127
128 }
129 description = new SyndContentImpl();
130 description.setType("text/html");
131 description.setValue("<p>Bug fixes, API changes, some new features and some Unit testing</p>"+
132 "<p>For details check the <a href=\"http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV03\">Changes Log for 0.3</a></p>");
133 entry.setDescription(description);
134 entries.add(entry);
135
136 entry = new SyndEntryImpl();
137 entry.setTitle("Rome v0.4");
138 entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome04");
139 try {
140 entry.setPublishedDate(dateParser.parse("2004-09-24"));
141 }
142 catch (ParseException ex) {
143
144 }
145 description = new SyndContentImpl();
146 description.setType("text/html");
147 description.setValue("<p>Bug fixes, API changes, some new features, Unit testing completed</p>"+
148 "<p>For details check the <a href=\"http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV04\">Changes Log for 0.4</a></p>");
149 entry.setDescription(description);
150 entries.add(entry);
151
152 feed.setEntries(entries);
153
154 return feed;
155 }
156
157 }