1 package com.atlassian.xwork.results;
2
3 import com.opensymphony.webwork.ServletActionContext;
4 import com.opensymphony.webwork.dispatcher.WebWorkResultSupport;
5 import com.opensymphony.xwork.ActionInvocation;
6 import com.sun.syndication.feed.WireFeed;
7 import com.sun.syndication.feed.synd.SyndFeed;
8 import com.sun.syndication.io.WireFeedOutput;
9 import org.apache.log4j.Logger;
10
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServletResponse;
13 import java.io.IOException;
14
15 public class RssResult extends WebWorkResultSupport {
16 private static final Logger log = Logger.getLogger(RssResult.class);
17 private static final String DEFAULT_DEFAULT_ENCODING = "UTF-8";
18
19 public static final String RSS = "rss";
20 public static final String RSS1 = "rss1";
21 public static final String RSS2 = "rss2";
22 public static final String ATOM = "atom";
23
24 protected void doExecute(String finalDestination, ActionInvocation actionInvocation) throws Exception {
25
26 ServletActionContext.getRequest().getSession(false);
27 HttpServletResponse response = ServletActionContext.getResponse();
28
29 if (finalDestination.startsWith("rss"))
30 response.setContentType("application/rss+xml; charset=" + DEFAULT_DEFAULT_ENCODING);
31 else if (finalDestination.startsWith("atom"))
32 response.setContentType("application/atom+xml; charset=" + DEFAULT_DEFAULT_ENCODING);
33 else
34 response.setContentType("text/xml; charset=" + DEFAULT_DEFAULT_ENCODING);
35
36 SyndFeed feed = (SyndFeed) actionInvocation.getStack().findValue("syndFeed");
37 if (feed == null)
38 throw new ServletException("Unable to find feed for this action");
39
40 WireFeed outFeed = feed.createWireFeed(finalDestination);
41 outFeed.setEncoding(DEFAULT_DEFAULT_ENCODING);
42 new WireFeedOutput().output(outFeed, response.getWriter());
43 try {
44 response.flushBuffer();
45 } catch (IOException e) {
46 log.info("Client aborted (closed the connection) before the rss feed could be returned.");
47 if (log.isDebugEnabled()) {
48 log.debug("Error sending rss result to client", e);
49 }
50 }
51 }
52 }