1 25 26 package com.j2biz.blogunity.web.actions.my; 27 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 import com.j2biz.blogunity.BlogunityManager; 40 import com.j2biz.blogunity.dao.BlogDAO; 41 import com.j2biz.blogunity.exception.BlogunityException; 42 import com.j2biz.blogunity.i18n.I18N; 43 import com.j2biz.blogunity.i18n.I18NStatusFactory; 44 import com.j2biz.blogunity.pojo.Blog; 45 import com.j2biz.blogunity.pojo.SystemConfiguration; 46 import com.j2biz.blogunity.util.ResourceUtils; 47 import com.j2biz.blogunity.web.IActionResult; 48 49 56 public class ExportBlogThemeAction extends MyAbstractAction { 57 60 private static final Log log = LogFactory.getLog(ExportBlogThemeAction.class); 61 62 68 public IActionResult execute(HttpServletRequest request, HttpServletResponse response) 69 throws BlogunityException { 70 71 String blogid = request.getParameter("blogId"); 72 if (StringUtils.isEmpty(blogid)) { throw new BlogunityException(I18NStatusFactory.create( 73 I18N.ERRORS.ID_NOT_SETTED, "Blog")); } 74 75 String compression = request.getParameter("compression"); 76 if (StringUtils.isEmpty(compression)) { 77 compression = "zip"; 78 } 79 80 Blog blog = null; 81 try { 82 blog = (new BlogDAO()).getBlogByID(Long.parseLong(blogid)); 83 } catch (Exception e1) { 84 blog = null; 85 } 86 if (blog == null) 87 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND)); 88 89 if (blog.getFounder().getId().longValue() != user.getId().longValue() 90 && !user.isAdministrator()) 91 throw new BlogunityException(I18NStatusFactory 92 .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION)); 93 94 try { 95 96 String filename = blog.getUrlName() + "_theme"; 97 if (compression.equals("gzip")) { 98 filename += ".gz"; 99 } else { 100 filename += ".zip"; 101 } 102 103 response.setHeader("Content-disposition", "attachment; filename=" + filename); 104 response.setContentType("application/Octet-stream"); 105 106 SystemConfiguration config = BlogunityManager.getSystemConfiguration(); 107 String tempDir = config.getTempDir(); 108 109 File blogsDir = config.getBlogsDirectory(); 110 File blogThemeDir = new File (blogsDir, blog.getUrlName() + "/theme"); 111 112 File compressedFile = ResourceUtils.zipDirectory(blogThemeDir, blog.getUrlName() 114 + "_theme"); 115 116 ServletOutputStream op = response.getOutputStream(); 118 FileInputStream in = new FileInputStream (compressedFile); 119 120 int length = 0; 121 byte[] buf = new byte[4096]; 122 while ((in != null) && ((length = in.read(buf)) != -1)) { 123 op.write(buf, 0, length); 125 } 126 in.close(); 127 128 ResourceUtils.removeFile(compressedFile); 131 132 } catch (Exception ex) { 133 log.error("Error exporting blog-theme!", ex); 134 throw new BlogunityException(I18NStatusFactory.create( 135 I18N.ERRORS.UNABLE_TO_EXPORT_THEME, ex)); 136 } 137 138 return IActionResult.NULL_RESULT; 140 } 141 142 } | Popular Tags |