KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DownloadSingleLogfileAction.java,v 1.3 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.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30
31 import javax.servlet.ServletOutputStream JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
49  * @author michelson
50  * @version $$
51  * @since 0.1
52  *
53  *
54  */

55 public class DownloadSingleLogfileAction extends MyAbstractAction {
56     /**
57      * Logger for this class
58      */

59     private static final Log log = LogFactory.getLog(DownloadSingleLogfileAction.class);
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
65      * javax.servlet.http.HttpServletResponse)
66      */

67     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68             throws BlogunityException {
69
70         String JavaDoc blogid = request.getParameter("blogId");
71         if (StringUtils.isEmpty(blogid)) { throw new BlogunityException(I18NStatusFactory.create(
72                 I18N.ERRORS.ID_NOT_SETTED, "Blog")); }
73
74         String JavaDoc 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 JavaDoc 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 JavaDoc blogsDir = config.getBlogsDirectory();
97             File JavaDoc blogLogsDir = new File JavaDoc(blogsDir, blog.getUrlName() + "/logs");
98
99             File JavaDoc logFile = new File JavaDoc(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             // return compressed file to user
108
ServletOutputStream JavaDoc op = response.getOutputStream();
109             FileInputStream JavaDoc in = new FileInputStream JavaDoc(logFile);
110
111             int length = 0;
112             byte[] buf = new byte[4096];
113             while ((in != null) && ((length = in.read(buf)) != -1)) {
114                 // the data has already been read into buf
115
op.write(buf, 0, length);
116             }
117             in.close();
118
119         } catch (Exception JavaDoc ex) {
120             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UNKNOWN, ex));
121         }
122
123         // return null because we have no forward here
124
return IActionResult.NULL_RESULT;
125     }
126
127 }
Popular Tags