KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > blog > AbstractFeedAction


1 /*
2  * $Id: AbstractFeedAction.java,v 1.6 2005/01/17 21:35:46 michelson Exp $
3  *
4  * Copyright (c) 2005 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.blog;
27
28 import java.text.DateFormat JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
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     /**
65      * Logger for this class
66      */

67     private static final Log log = LogFactory.getLog(AtomFeedAction.class);
68
69     private Blog blog;
70
71     private String JavaDoc categoryId;
72
73     private static final String JavaDoc MIME_TYPE = "application/xml; charset=UTF-8";
74
75     private static final String JavaDoc DATE_FORMAT = "yyyy-MM-dd";
76
77     /**
78      *
79      */

80     public AbstractFeedAction(Blog blog) {
81         this(blog, null);
82     }
83
84     public AbstractFeedAction(Blog blog, String JavaDoc categoryId) {
85         this.blog = blog;
86         this.categoryId = categoryId;
87     }
88
89     public abstract String JavaDoc getFeedtype();
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
95      * javax.servlet.http.HttpServletResponse)
96      */

97     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc 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     /**
111      * @throws ServletException
112      *
113      */

114     private void outputFeed(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
115             String JavaDoc feedType) throws BlogunityException {
116
117         if (blog == null) {
118             // re-pack exception into user-friendly one
119
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 JavaDoc ex) {
133             String JavaDoc msg = I18N.ERRORS.COULD_NOT_GENERATE_FEED;
134             log.error(msg, ex);
135
136             // re-pack exception into user-friendly one
137
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 JavaDoc request, Blog b) throws BlogunityException {
146
147         DateFormat JavaDoc dateParser = new SimpleDateFormat JavaDoc(DATE_FORMAT);
148
149         SyndFeed feed = new SyndFeedImpl();
150         String JavaDoc 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         // fill categories
160
List JavaDoc categories = new ArrayList JavaDoc();
161         for (Iterator JavaDoc 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         // fill entries
171
List JavaDoc syndEntries = new ArrayList JavaDoc();
172         List JavaDoc entries;
173         EntryDAO dao = new EntryDAO();
174         // get last 20 entries
175
if (StringUtils.isNotEmpty(categoryId)) {
176             
177             try {
178                 Long JavaDoc catIdLong = new Long JavaDoc(categoryId);
179                 entries = dao.getPaginatedEntriesByCategory(b.getId(), catIdLong, 0, 20);
180             } catch (NumberFormatException JavaDoc 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 JavaDoc 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 JavaDoc 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