1 25 26 package com.j2biz.blogunity.web.actions.my; 27 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileOutputStream ; 31 import java.io.FileWriter ; 32 import java.io.IOException ; 33 import java.text.DateFormat ; 34 import java.text.SimpleDateFormat ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.zip.GZIPOutputStream ; 39 import java.util.zip.ZipEntry ; 40 import java.util.zip.ZipOutputStream ; 41 42 import javax.servlet.ServletOutputStream ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 46 import org.apache.commons.lang.StringUtils; 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 import com.j2biz.blogunity.BlogunityManager; 51 import com.j2biz.blogunity.dao.BlogDAO; 52 import com.j2biz.blogunity.exception.BlogunityException; 53 import com.j2biz.blogunity.i18n.I18N; 54 import com.j2biz.blogunity.i18n.I18NStatusFactory; 55 import com.j2biz.blogunity.pojo.Blog; 56 import com.j2biz.blogunity.pojo.Category; 57 import com.j2biz.blogunity.pojo.Entry; 58 import com.j2biz.blogunity.web.IActionResult; 59 import com.sun.syndication.feed.synd.SyndCategory; 60 import com.sun.syndication.feed.synd.SyndCategoryImpl; 61 import com.sun.syndication.feed.synd.SyndContent; 62 import com.sun.syndication.feed.synd.SyndContentImpl; 63 import com.sun.syndication.feed.synd.SyndEntry; 64 import com.sun.syndication.feed.synd.SyndEntryImpl; 65 import com.sun.syndication.feed.synd.SyndFeed; 66 import com.sun.syndication.feed.synd.SyndFeedImpl; 67 import com.sun.syndication.io.SyndFeedOutput; 68 69 76 public class ExportFeedAction extends MyAbstractAction { 77 80 private static final Log log = LogFactory.getLog(ExportFeedAction.class); 81 82 private static final String MIME_TYPE = "application/xml; charset=UTF-8"; 83 84 private static final String DATE_FORMAT = "yyyy-MM-dd"; 85 86 92 public IActionResult execute(HttpServletRequest request, HttpServletResponse response) 93 throws BlogunityException { 94 95 String blogid = request.getParameter("blogid"); 96 if (StringUtils.isEmpty(blogid)) { throw new BlogunityException(I18NStatusFactory.create( 97 I18N.ERRORS.ID_NOT_SETTED, "Blog")); } 98 99 String feedType = request.getParameter("type"); 100 if (StringUtils.isEmpty(feedType)) 101 throw new BlogunityException(I18NStatusFactory 102 .create(I18N.ERRORS.FEED_TYPE_NOT_SETTED)); 103 104 String compression = request.getParameter("compression"); 105 if (StringUtils.isEmpty(compression)) { 106 compression = "none"; 107 } 108 109 Blog blog = null; 110 try { 111 blog = (new BlogDAO()).getBlogByID(Long.parseLong(blogid)); 112 } catch (Exception e1) { 113 blog = null; 114 } 115 if (blog == null) 116 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND)); 117 118 if (blog.getFounder().getId().longValue() != user.getId().longValue() 119 && !user.isAdministrator()) 120 throw new BlogunityException(I18NStatusFactory 121 .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION)); 122 123 try { 124 SyndFeed feed = getFeed(request, blog); 125 feed.setFeedType(feedType); 126 response.setContentType(MIME_TYPE); 127 128 SyndFeedOutput output = new SyndFeedOutput(); 129 130 String filename = blog.getUrlName(); 131 if (compression.equals("zip")) { 132 filename += "-" + feedType + ".zip"; 133 } else if (compression.equals("gzip")) { 134 filename += "-" + feedType + ".gz"; 135 } else { 136 filename += "-" + feedType + ".xml"; 137 } 138 139 response.setHeader("Content-disposition", "attachment; filename=" + filename); 140 response.setContentType("application/Octet-stream"); 141 142 if (compression.equals("zip") || compression.equals("gzip")) { 143 File temp = writeFeedToTempFile(feed, blog); 144 File compressedFile; 145 146 if (compression.equals("zip")) { 147 compressedFile = zipTempFile(temp); 148 } else { 149 compressedFile = gzipTempFile(temp); 150 } 151 152 removeTempFile(temp); 153 154 ServletOutputStream op = response.getOutputStream(); 155 FileInputStream in = new FileInputStream (compressedFile); 156 157 int length = 0; 158 byte[] buf = new byte[4096]; 159 while ((in != null) && ((length = in.read(buf)) != -1)) { 160 op.write(buf, 0, length); 162 } 163 in.close(); 164 165 removeTempFile(compressedFile); 167 168 } else { 169 output.output(feed, response.getWriter()); 170 } 171 172 } catch (Exception ex) { 173 log.error("Error generating blog!", ex); 174 throw new BlogunityException(I18NStatusFactory.create( 175 I18N.ERRORS.COULD_NOT_GENERATE_FEED, ex)); 176 } 177 178 return null; 180 } 181 182 private synchronized File zipTempFile(File tempFile) throws BlogunityException { 183 try { 184 185 File zippedFile = new File (BlogunityManager.getSystemConfiguration().getTempDir(), 186 tempFile.getName() + ".zip"); 187 188 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (zippedFile)); 189 190 byte[] readBuffer = new byte[2156]; 191 int bytesIn = 0; 192 193 FileInputStream fis = new FileInputStream (tempFile); 194 ZipEntry anEntry = new ZipEntry (tempFile.getName()); 195 196 zos.putNextEntry(anEntry); 198 while ((bytesIn = fis.read(readBuffer)) != -1) { 200 zos.write(readBuffer, 0, bytesIn); 201 } 202 fis.close(); 204 zos.close(); 205 206 return zippedFile; 207 208 } catch (Exception e) { 209 210 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FEED_ZIP_FAILED, e)); 211 212 } 213 } 214 215 private synchronized File gzipTempFile(File tempFile) throws BlogunityException { 216 try { 217 218 File gzippedFile = new File (BlogunityManager.getSystemConfiguration().getTempDir(), 219 tempFile.getName() + ".gz"); 220 221 GZIPOutputStream zos = new GZIPOutputStream (new FileOutputStream (gzippedFile)); 222 223 byte[] readBuffer = new byte[2156]; 224 int bytesIn = 0; 225 226 FileInputStream fis = new FileInputStream (tempFile); 227 228 while ((bytesIn = fis.read(readBuffer)) != -1) { 230 zos.write(readBuffer, 0, bytesIn); 231 } 232 fis.close(); 234 zos.close(); 235 236 return gzippedFile; 237 238 } catch (Exception e) { 239 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FEED_GZIP_FAILED, e)); 240 } 241 } 242 243 private synchronized File writeFeedToTempFile(SyndFeed feed, Blog b) throws BlogunityException { 244 String tempDir = BlogunityManager.getSystemConfiguration().getTempDir(); 245 File tempFile = new File (tempDir, b.getUrlName() + "_" + System.currentTimeMillis() 246 + ".xml"); 247 248 if (!tempFile.exists()) { 249 boolean result; 250 try { 251 result = tempFile.createNewFile(); 252 } catch (IOException e) { 253 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE_DIRECTORY, 254 tempFile.getAbsolutePath(), e)); 255 256 } 257 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 258 I18N.ERRORS.CREATE_DIRECTORY, tempFile.getAbsolutePath())); } 259 } else { 260 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE_DIRECTORY, 261 tempFile.getAbsolutePath())); 262 } 263 264 try { 265 FileWriter writer = new FileWriter (tempFile); 266 SyndFeedOutput output = new SyndFeedOutput(); 267 output.output(feed, writer); 268 writer.close(); 269 270 } catch (Exception e) { 271 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.WRITE_DIRECTORY, 272 tempFile.getAbsolutePath(), e)); 273 274 } 275 276 return tempFile; 277 } 278 279 private synchronized void removeTempFile(File tempFile) throws BlogunityException { 280 boolean result = tempFile.delete(); 281 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 282 I18N.ERRORS.DELETE_DIRECTORY, tempFile.getAbsolutePath())); } 283 284 } 285 286 private SyndFeed getFeed(HttpServletRequest request, Blog b) throws BlogunityException { 287 288 DateFormat dateParser = new SimpleDateFormat (DATE_FORMAT); 289 290 SyndFeed feed = new SyndFeedImpl(); 291 292 String blogUrl = BlogunityManager.getBase() + "/blogs/" + b.getUrlName(); 293 294 feed.setTitle(b.getUrlName()); 295 feed.setLink(blogUrl); 296 feed.setDescription((b.getDescription() != null) ? b.getDescription() : ""); 297 feed.setCopyright("Copyright by " + b.getFounder().getNickname()); 298 feed.setAuthor(b.getFounder().getNickname()); 299 feed.setPublishedDate(b.getLastModified()); 300 301 List categories = new ArrayList (); 303 for (Iterator i = b.getCategories().iterator(); i.hasNext();) { 304 Category cat = (Category) i.next(); 305 SyndCategory c = new SyndCategoryImpl(); 306 c.setName(cat.getName()); 307 categories.add(c); 308 309 } 310 feed.setCategories(categories); 311 312 List syndEntries = new ArrayList (); 314 315 for (Iterator i = b.getEntries().iterator(); i.hasNext();) { 316 Entry entry = (Entry ) i.next(); 317 318 SyndEntry e = new SyndEntryImpl(); 319 e.setTitle(entry.getTitle()); 320 321 e.setLink(blogUrl + entry.getPermalink()); 322 e.setPublishedDate(entry.getCreateTime()); 323 324 SyndContent description = new SyndContentImpl(); 325 description.setType("text/html"); 326 String value = ""; 327 if (StringUtils.isNotEmpty(entry.getExcerpt())) value = entry.getExcerpt() + "<br/>" 328 + entry.getBody(); 329 else 330 value = entry.getBody(); 331 332 description.setValue(value); 333 e.setDescription(description); 334 syndEntries.add(e); 335 336 } 337 338 feed.setEntries(syndEntries); 339 340 return feed; 341 } 342 } | Popular Tags |