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.web.IActionResult; 47 48 55 public class DownloadSingleLogfileAction extends MyAbstractAction { 56 59 private static final Log log = LogFactory.getLog(DownloadSingleLogfileAction.class); 60 61 67 public IActionResult execute(HttpServletRequest request, HttpServletResponse response) 68 throws BlogunityException { 69 70 String blogid = request.getParameter("blogId"); 71 if (StringUtils.isEmpty(blogid)) { throw new BlogunityException(I18NStatusFactory.create( 72 I18N.ERRORS.ID_NOT_SETTED, "Blog")); } 73 74 String file = request.getParameter("file"); 75 if (StringUtils.isEmpty(file)) { throw new BlogunityException(I18NStatusFactory.create( 76 I18N.ERRORS.NOT_FOUND, "Logfile")); } 77 78 BlogDAO blogDAO = new BlogDAO(); 79 Blog blog = null; 80 try { 81 blog = blogDAO.getBlogByID(Long.parseLong(blogid)); 82 } catch (Exception e1) { 83 blog = null; 84 } 85 if (blog == null) 86 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND)); 87 88 if (blog.getFounder().getId().longValue() != user.getId().longValue() 89 && !user.isAdministrator()) 90 throw new BlogunityException(I18NStatusFactory 91 .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION)); 92 93 try { 94 95 SystemConfiguration config = BlogunityManager.getSystemConfiguration(); 96 File blogsDir = config.getBlogsDirectory(); 97 File blogLogsDir = new File (blogsDir, blog.getUrlName() + "/logs"); 98 99 File logFile = new File (blogLogsDir, file); 100 101 if (!logFile.exists() || !logFile.canRead()) { throw new BlogunityException( 102 I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND, "Logfile")); } 103 104 response.setHeader("Content-disposition", "attachment; filename=" + file); 105 response.setContentType("application/Octet-stream"); 106 107 ServletOutputStream op = response.getOutputStream(); 109 FileInputStream in = new FileInputStream (logFile); 110 111 int length = 0; 112 byte[] buf = new byte[4096]; 113 while ((in != null) && ((length = in.read(buf)) != -1)) { 114 op.write(buf, 0, length); 116 } 117 in.close(); 118 119 } catch (Exception ex) { 120 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UNKNOWN, ex)); 121 } 122 123 return IActionResult.NULL_RESULT; 125 } 126 127 } | Popular Tags |