| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.web.view.impl; |
| 33 | |
|
| 34 | |
import com.sun.syndication.feed.synd.*; |
| 35 | |
import com.sun.syndication.io.FeedException; |
| 36 | |
import com.sun.syndication.io.SyndFeedOutput; |
| 37 | |
import net.sourceforge.pebble.Constants; |
| 38 | |
import net.sourceforge.pebble.api.decorator.ContentDecoratorContext; |
| 39 | |
import net.sourceforge.pebble.decorator.ContentDecoratorChain; |
| 40 | |
import net.sourceforge.pebble.domain.*; |
| 41 | |
import net.sourceforge.pebble.web.view.View; |
| 42 | |
|
| 43 | |
import javax.servlet.ServletContext; |
| 44 | |
import javax.servlet.ServletException; |
| 45 | |
import javax.servlet.http.HttpServletRequest; |
| 46 | |
import javax.servlet.http.HttpServletResponse; |
| 47 | |
import java.io.IOException; |
| 48 | |
import java.text.SimpleDateFormat; |
| 49 | |
import java.util.*; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public abstract class AbstractRomeFeedView extends View { |
| 57 | |
|
| 58 | |
private final FeedType feedType; |
| 59 | |
private final SimpleDateFormat idDateFormat; |
| 60 | |
|
| 61 | 12 | protected AbstractRomeFeedView(FeedType feedType) { |
| 62 | 12 | this.feedType = feedType; |
| 63 | 12 | this.idDateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| 64 | 12 | idDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 65 | 12 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
@SuppressWarnings("unchecked") |
| 71 | |
public void prepare() { |
| 72 | 0 | ContentDecoratorContext context = new ContentDecoratorContext(); |
| 73 | 0 | context.setView(ContentDecoratorContext.SUMMARY_VIEW); |
| 74 | 0 | context.setMedia(ContentDecoratorContext.NEWS_FEED); |
| 75 | |
|
| 76 | 0 | List<BlogEntry> blogEntries = (List<BlogEntry>) getModel().get(Constants.BLOG_ENTRIES); |
| 77 | 0 | ContentDecoratorChain.decorate(context, blogEntries); |
| 78 | 0 | getModel().put(Constants.BLOG_ENTRIES, blogEntries); |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public String getContentType() { |
| 87 | 0 | AbstractBlog blog = (AbstractBlog) getModel().get(Constants.BLOG_KEY); |
| 88 | 0 | return feedType.getContentType() + "; charset=" + blog.getCharacterEncoding(); |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public void dispatch(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException { |
| 100 | 4 | SyndFeed syndFeed = getFeed(); |
| 101 | 4 | syndFeed.setFeedType(getFeedType().getFeedType()); |
| 102 | |
|
| 103 | 4 | SyndFeedOutput output = new SyndFeedOutput(); |
| 104 | |
|
| 105 | |
try { |
| 106 | 4 | output.output(syndFeed, response.getWriter()); |
| 107 | 0 | } catch (IOException e) { |
| 108 | 0 | throw new ServletException("Error generating feed", e); |
| 109 | 0 | } catch (FeedException e) { |
| 110 | 0 | throw new ServletException("Error generating feed", e); |
| 111 | 4 | } |
| 112 | 4 | } |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
protected abstract SyndFeed getFeed(); |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public FeedType getFeedType() { |
| 127 | 20 | return feedType; |
| 128 | |
} |
| 129 | |
|
| 130 | |
protected SimpleDateFormat getIdDateFormat() { |
| 131 | 8 | return idDateFormat; |
| 132 | |
} |
| 133 | |
|
| 134 | |
protected String generateId(AbstractBlog blog, Date date, String contentId) { |
| 135 | 16 | StringBuilder id = new StringBuilder("tag:"); |
| 136 | 16 | id.append(blog.getDomainName()).append(","); |
| 137 | 16 | if (date != null) { |
| 138 | 8 | id.append(getIdDateFormat().format(date)); |
| 139 | |
} else { |
| 140 | 8 | id.append("0000-00-00"); |
| 141 | |
} |
| 142 | 16 | id.append(":").append(blog.getId()); |
| 143 | 16 | if (contentId != null) { |
| 144 | 8 | id.append("/").append(contentId); |
| 145 | |
} |
| 146 | 16 | return id.toString(); |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | 4 | public enum FeedType { |
| 153 | 4 | ATOM("atom_1.0", "atom.xml", "application/atom+xml"), |
| 154 | 4 | RSS("rss_2.0", "rss.xml", "application/xml"); |
| 155 | |
|
| 156 | |
private final String feedType; |
| 157 | |
private final String fileName; |
| 158 | |
private final String contentType; |
| 159 | |
|
| 160 | 8 | FeedType(String feedType, String fileName, String contentType) { |
| 161 | 8 | this.feedType = feedType; |
| 162 | 8 | this.fileName = fileName; |
| 163 | 8 | this.contentType = contentType; |
| 164 | 8 | } |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
public String getFeedType() { |
| 172 | 4 | return feedType; |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public String getFileName() { |
| 181 | 8 | return fileName; |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
public String getContentType() { |
| 190 | 8 | return contentType; |
| 191 | |
} |
| 192 | |
} |
| 193 | |
|
| 194 | |
} |