KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > my > ImportFeedAction


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

25
26 package com.j2biz.blogunity.web.actions.my;
27
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.apache.commons.fileupload.DiskFileUpload;
39 import org.apache.commons.fileupload.FileItem;
40 import org.apache.commons.fileupload.FileUpload;
41 import org.apache.commons.fileupload.FileUploadException;
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import com.j2biz.blogunity.BlogunityManager;
47 import com.j2biz.blogunity.dao.BlogDAO;
48 import com.j2biz.blogunity.dao.CategoryDAO;
49 import com.j2biz.blogunity.dao.EntryDAO;
50 import com.j2biz.blogunity.exception.BlogunityException;
51 import com.j2biz.blogunity.i18n.I18N;
52 import com.j2biz.blogunity.i18n.I18NStatusFactory;
53 import com.j2biz.blogunity.pojo.Blog;
54 import com.j2biz.blogunity.pojo.Category;
55 import com.j2biz.blogunity.pojo.Entry;
56 import com.j2biz.blogunity.web.ActionResultFactory;
57 import com.j2biz.blogunity.web.FormError;
58 import com.j2biz.blogunity.web.FormErrorList;
59 import com.j2biz.blogunity.web.IActionResult;
60 import com.sun.syndication.feed.synd.SyndCategory;
61 import com.sun.syndication.feed.synd.SyndEntry;
62 import com.sun.syndication.feed.synd.SyndFeed;
63 import com.sun.syndication.io.SyndFeedInput;
64
65 /**
66  * @author michelson
67  * @version $$
68  * @since 0.1
69  *
70  *
71  */

72 public class ImportFeedAction extends MyAbstractAction {
73     /**
74      * Logger for this class
75      */

76     private static final Log log = LogFactory.getLog(ImportFeedAction.class);
77
78     private static final IActionResult MEMBERS_LIST_FORWARD = ActionResultFactory
79             .buildForward("/jsp/my/listBlogFeeds.jsp");
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
85      * javax.servlet.http.HttpServletResponse)
86      */

87     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
88             throws BlogunityException {
89
90         boolean isMultipart = FileUpload.isMultipartContent(request);
91
92         if (!isMultipart) {
93
94         throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_MULTIPART));
95
96         }
97
98         FormErrorList errors = new FormErrorList();
99         DiskFileUpload upload = new DiskFileUpload();
100         BlogDAO blogDAO = new BlogDAO();
101         CategoryDAO categoryDAO = new CategoryDAO();
102
103         // TODO this parameters should be specified in global
104
// configuration-settings
105
// upload.setSizeThreshold(yourMaxMemorySize);
106
// upload.setSizeMax(yourMaxRequestSize);
107
upload.setRepositoryPath(BlogunityManager.getSystemConfiguration().getTempDir());
108
109         List JavaDoc /* FileItem */items;
110         try {
111             items = upload.parseRequest(request);
112         } catch (FileUploadException e) {
113             throw new BlogunityException(I18NStatusFactory.create(
114                     I18N.ERRORS.UNABLE_TO_PARSE_UPLOAD, e));
115         }
116
117         // Process the uploaded items
118
String JavaDoc feedType;
119         Blog blog = null;
120         SyndFeed feed = null;
121         Iterator JavaDoc iter = items.iterator();
122         while (iter.hasNext()) {
123             FileItem item = (FileItem) iter.next();
124
125             if (item.isFormField()) {
126                 String JavaDoc name = item.getFieldName();
127                 String JavaDoc value = item.getString();
128
129                 if (name.equals("blogid") && StringUtils.isNotEmpty(value)) {
130                     try {
131                         blog = blogDAO.getBlogByID(Long.parseLong(value));
132                     } catch (Exception JavaDoc e1) {
133                         blog = null;
134                     }
135                 }
136
137             } else {
138
139                 String JavaDoc fieldName = item.getFieldName();
140                 String JavaDoc fileName = item.getName();
141                 String JavaDoc contentType = item.getContentType();
142                 boolean isInMemory = item.isInMemory();
143                 long sizeInBytes = item.getSize();
144
145                 if (StringUtils.isEmpty(fileName)) {
146                     errors.add(new FormError("import", "XML-File is not specified!"));
147                 } else if (!contentType.equals("text/xml")) {
148                     errors.add(new FormError("import", "Uploaded file is not a valid XML-Feed!"));
149                 }
150
151                 if (errors.size() == 0) {
152
153                     if (fieldName.equals("feedFile")) {
154                         if (log.isDebugEnabled()) {
155                             log.debug("Parsing uploaded file: name=" + fileName + ", contentType="
156                                     + contentType + ", size=" + sizeInBytes);
157                         }
158
159                         SyndFeedInput input = new SyndFeedInput();
160                         InputStream JavaDoc uploadedStream = null;
161                         try {
162                             uploadedStream = item.getInputStream();
163
164                             if (log.isDebugEnabled()) {
165                                 log.debug("Building Feed-Instance...");
166                             }
167
168                             feed = input.build(new InputStreamReader JavaDoc(uploadedStream));
169                             if (log.isDebugEnabled()) {
170                                 log.debug("Building complete... Found Feed-Type = "
171                                         + feed.getFeedType());
172                             }
173
174                         } catch (Exception JavaDoc e) {
175                             throw new BlogunityException(I18NStatusFactory.create(
176                                     I18N.ERRORS.UNABLE_TO_PARSE_XML, e));
177                         } finally {
178                             try {
179                                 if (uploadedStream != null) uploadedStream.close();
180                             } catch (IOException JavaDoc e1) {
181                                 ;
182                             }
183                         }
184
185                     }
186
187                 }
188
189             }
190         }
191
192         if (blog == null) { throw new BlogunityException(I18NStatusFactory.create(
193                 I18N.ERRORS.NOT_FOUND, "Blog")); }
194         if (blog.getFounder().getId().longValue() != user.getId().longValue())
195                 throw new BlogunityException(I18NStatusFactory
196                         .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION));
197
198         // return, if there is errors
199
if (errors.size() > 0) {
200             request.setAttribute("errors", errors);
201             request.setAttribute("blog", blog);
202             return MEMBERS_LIST_FORWARD;
203         }
204
205         if (feed != null) {
206             List JavaDoc categories = feed.getCategories();
207             for (Iterator JavaDoc i = categories.iterator(); i.hasNext();) {
208                 SyndCategory c = (SyndCategory) i.next();
209
210                 Category exists = categoryDAO.getGlobalCategoryByName(c.getName());
211                 if (exists == null) {
212                     Category cat = new Category(c.getName(), Category.LOCAL);
213                     cat.addBlog(blog);
214                     blog.addCategory(cat);
215                     categoryDAO.createCategory(cat);
216                 }
217             }
218
219             List JavaDoc entries = feed.getEntries();
220             for (Iterator JavaDoc i = entries.iterator(); i.hasNext();) {
221                 SyndEntry e = (SyndEntry) i.next();
222
223                 Entry entry = new Entry();
224                 entry.setAuthor(user);
225                 Date JavaDoc t = e.getPublishedDate();
226                 if (t == null) t = new Date JavaDoc();
227                 entry.setCreateTime(t);
228
229                 String JavaDoc content = e.getDescription().getValue();
230                 String JavaDoc title = e.getTitle();
231
232                 entry.setRawBody(content);
233                 entry.setBody(content);
234                 entry.setRawTitle(title);
235                 entry.setTitle(title);
236                 entry.setBlog(blog);
237
238                 List JavaDoc entryCategories = e.getCategories();
239                 for (Iterator JavaDoc ix = entryCategories.iterator(); ix.hasNext();) {
240                     SyndCategory c = (SyndCategory) ix.next();
241                     String JavaDoc name = c.getName();
242                     Category _c = categoryDAO.getGlobalCategoryByName(name);
243
244                     if (_c == null) {
245                         _c = new Category(c.getName(), Category.LOCAL);
246                         _c.addBlog(blog);
247                         _c.addEntry(entry);
248                         categoryDAO.createCategory(_c);
249                         blog.addCategory(_c);
250                     }
251
252                     if (_c != null) {
253                         entry.addCategory(_c);
254                     }
255
256                 }
257
258                 (new EntryDAO()).createEntry(entry);
259             }
260         }
261
262         blogDAO.updateBlog(blog);
263
264         request.setAttribute("blog", blog);
265
266         return MEMBERS_LIST_FORWARD;
267     }
268 }
Popular Tags