1 25 26 package com.j2biz.blogunity.web.actions.my; 27 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 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 72 public class ImportFeedAction extends MyAbstractAction { 73 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 87 public IActionResult execute(HttpServletRequest request, HttpServletResponse 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 upload.setRepositoryPath(BlogunityManager.getSystemConfiguration().getTempDir()); 108 109 List 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 String feedType; 119 Blog blog = null; 120 SyndFeed feed = null; 121 Iterator iter = items.iterator(); 122 while (iter.hasNext()) { 123 FileItem item = (FileItem) iter.next(); 124 125 if (item.isFormField()) { 126 String name = item.getFieldName(); 127 String value = item.getString(); 128 129 if (name.equals("blogid") && StringUtils.isNotEmpty(value)) { 130 try { 131 blog = blogDAO.getBlogByID(Long.parseLong(value)); 132 } catch (Exception e1) { 133 blog = null; 134 } 135 } 136 137 } else { 138 139 String fieldName = item.getFieldName(); 140 String fileName = item.getName(); 141 String 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 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 (uploadedStream)); 169 if (log.isDebugEnabled()) { 170 log.debug("Building complete... Found Feed-Type = " 171 + feed.getFeedType()); 172 } 173 174 } catch (Exception 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 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 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 categories = feed.getCategories(); 207 for (Iterator 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 entries = feed.getEntries(); 220 for (Iterator i = entries.iterator(); i.hasNext();) { 221 SyndEntry e = (SyndEntry) i.next(); 222 223 Entry entry = new Entry(); 224 entry.setAuthor(user); 225 Date t = e.getPublishedDate(); 226 if (t == null) t = new Date (); 227 entry.setCreateTime(t); 228 229 String content = e.getDescription().getValue(); 230 String 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 entryCategories = e.getCategories(); 239 for (Iterator ix = entryCategories.iterator(); ix.hasNext();) { 240 SyndCategory c = (SyndCategory) ix.next(); 241 String 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 |