KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ExportBlogThemeAction.java,v 1.4 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.util.ResourceUtils;
47 import com.j2biz.blogunity.web.IActionResult;
48
49 /**
50  * @author michelson
51  * @version $$
52  * @since 0.1
53  *
54  *
55  */

56 public class ExportBlogThemeAction extends MyAbstractAction {
57     /**
58      * Logger for this class
59      */

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

68     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69             throws BlogunityException {
70
71         String JavaDoc blogid = request.getParameter("blogId");
72         if (StringUtils.isEmpty(blogid)) { throw new BlogunityException(I18NStatusFactory.create(
73                 I18N.ERRORS.ID_NOT_SETTED, "Blog")); }
74
75         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc tempDir = config.getTempDir();
108
109             File JavaDoc blogsDir = config.getBlogsDirectory();
110             File JavaDoc blogThemeDir = new File JavaDoc(blogsDir, blog.getUrlName() + "/theme");
111
112             // File compressedFile = zipThemeDirectory(blogThemeDir, blog);
113
File JavaDoc compressedFile = ResourceUtils.zipDirectory(blogThemeDir, blog.getUrlName()
114                     + "_theme");
115
116             // return compressed file to user
117
ServletOutputStream JavaDoc op = response.getOutputStream();
118             FileInputStream JavaDoc in = new FileInputStream JavaDoc(compressedFile);
119
120             int length = 0;
121             byte[] buf = new byte[4096];
122             while ((in != null) && ((length = in.read(buf)) != -1)) {
123                 // the data has already been read into buf
124
op.write(buf, 0, length);
125             }
126             in.close();
127
128             // and remove temporary file
129
// removeTempFile(compressedFile);
130
ResourceUtils.removeFile(compressedFile);
131
132         } catch (Exception JavaDoc 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 null because we have no forward here
139
return IActionResult.NULL_RESULT;
140     }
141
142 }
Popular Tags