| 1 25 26 package com.j2biz.blogunity.web.actions.blog; 27 28 import java.text.DateFormat ; 29 import java.text.SimpleDateFormat ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.apache.commons.lang.StringUtils; 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 42 import com.j2biz.blogunity.BlogunityManager; 43 import com.j2biz.blogunity.dao.EntryDAO; 44 import com.j2biz.blogunity.exception.BlogunityException; 45 import com.j2biz.blogunity.i18n.I18N; 46 import com.j2biz.blogunity.i18n.I18NStatusFactory; 47 import com.j2biz.blogunity.pojo.Blog; 48 import com.j2biz.blogunity.pojo.Category; 49 import com.j2biz.blogunity.pojo.Entry; 50 import com.j2biz.blogunity.web.IActionResult; 51 import com.j2biz.blogunity.web.actions.AbstractAction; 52 import com.sun.syndication.feed.synd.SyndCategory; 53 import com.sun.syndication.feed.synd.SyndCategoryImpl; 54 import com.sun.syndication.feed.synd.SyndContent; 55 import com.sun.syndication.feed.synd.SyndContentImpl; 56 import com.sun.syndication.feed.synd.SyndEntry; 57 import com.sun.syndication.feed.synd.SyndEntryImpl; 58 import com.sun.syndication.feed.synd.SyndFeed; 59 import com.sun.syndication.feed.synd.SyndFeedImpl; 60 import com.sun.syndication.io.SyndFeedOutput; 61 62 public abstract class AbstractFeedAction extends AbstractAction { 63 64 67 private static final Log log = LogFactory.getLog(AtomFeedAction.class); 68 69 private Blog blog; 70 71 private String categoryId; 72 73 private static final String MIME_TYPE = "application/xml; charset=UTF-8"; 74 75 private static final String DATE_FORMAT = "yyyy-MM-dd"; 76 77 80 public AbstractFeedAction(Blog blog) { 81 this(blog, null); 82 } 83 84 public AbstractFeedAction(Blog blog, String categoryId) { 85 this.blog = blog; 86 this.categoryId = categoryId; 87 } 88 89 public abstract String getFeedtype(); 90 91 97 public IActionResult execute(HttpServletRequest request, HttpServletResponse response) 98 throws BlogunityException { 99 100 if (log.isDebugEnabled()) { 101 log.debug("Atom-Feed for blog '" + blog + " requested!"); 102 } 103 104 outputFeed(request, response, getFeedtype()); 105 106 return IActionResult.NULL_RESULT; 107 108 } 109 110 114 private void outputFeed(HttpServletRequest request, HttpServletResponse response, 115 String feedType) throws BlogunityException { 116 117 if (blog == null) { 118 BlogunityException ex = new BlogunityException(I18NStatusFactory.create( 120 I18N.ERRORS.NOT_FOUND, "blog")); 121 throw ex; 122 } 123 124 try { 125 SyndFeed feed = getFeed(request, blog); 126 feed.setFeedType(feedType); 127 128 response.setContentType(MIME_TYPE); 129 130 SyndFeedOutput output = new SyndFeedOutput(); 131 output.output(feed, response.getWriter()); 132 } catch (Exception ex) { 133 String msg = I18N.ERRORS.COULD_NOT_GENERATE_FEED; 134 log.error(msg, ex); 135 136 BlogunityException e = new BlogunityException(I18NStatusFactory.create( 138 I18N.ERRORS.COULD_NOT_GENERATE_FEED, ex)); 139 throw e; 140 141 } 142 143 } 144 145 private SyndFeed getFeed(HttpServletRequest request, Blog b) throws BlogunityException { 146 147 DateFormat dateParser = new SimpleDateFormat (DATE_FORMAT); 148 149 SyndFeed feed = new SyndFeedImpl(); 150 String blogUrl = BlogunityManager.getBase() + "/blogs/" + b.getUrlName(); 151 152 feed.setTitle(b.getFullName()); 153 feed.setLink(blogUrl); 154 feed.setDescription((b.getDescription() != null) ? b.getDescription() : ""); 155 feed.setCopyright("Copyright by " + b.getFounder().getNickname()); 156 feed.setAuthor(b.getFounder().getNickname()); 157 feed.setPublishedDate(b.getLastModified()); 158 159 List categories = new ArrayList (); 161 for (Iterator i = b.getCategories().iterator(); i.hasNext();) { 162 Category cat = (Category) i.next(); 163 SyndCategory c = new SyndCategoryImpl(); 164 c.setName(cat.getName()); 165 categories.add(c); 166 167 } 168 feed.setCategories(categories); 169 170 List syndEntries = new ArrayList (); 172 List entries; 173 EntryDAO dao = new EntryDAO(); 174 if (StringUtils.isNotEmpty(categoryId)) { 176 177 try { 178 Long catIdLong = new Long (categoryId); 179 entries = dao.getPaginatedEntriesByCategory(b.getId(), catIdLong, 0, 20); 180 } catch (NumberFormatException e) { 181 entries = dao.getPaginatedEntries(b.getUrlName(), 0, 20); 182 } 183 184 } else { 185 entries = dao.getPaginatedEntries(b.getUrlName(), 0, 20); 186 } 187 188 for (Iterator i = entries.iterator(); i.hasNext();) { 189 Entry entry = (Entry) i.next(); 190 191 SyndEntry e = new SyndEntryImpl(); 192 e.setTitle(entry.getTitle()); 193 194 e.setLink(blogUrl + entry.getPermalink()); 195 e.setPublishedDate(entry.getCreateTime()); 196 197 SyndContent description = new SyndContentImpl(); 198 description.setType("text/html"); 199 String value = ""; 200 if (StringUtils.isNotEmpty(entry.getExcerpt())) value = entry.getExcerpt() + "<br/>" 201 + entry.getBody(); 202 else 203 value = entry.getBody(); 204 205 description.setValue(value); 206 207 e.setDescription(description); 208 209 syndEntries.add(e); 210 211 } 212 213 feed.setEntries(syndEntries); 214 215 return feed; 216 } 217 218 } | Popular Tags |