KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EditThemeFileExecAction.java,v 1.9 2005/01/17 21:35:44 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.BufferedWriter JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileWriter JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36
37 import com.j2biz.blogunity.BlogunityManager;
38 import com.j2biz.blogunity.dao.BlogDAO;
39 import com.j2biz.blogunity.exception.BlogunityException;
40 import com.j2biz.blogunity.i18n.I18N;
41 import com.j2biz.blogunity.i18n.I18NStatusFactory;
42 import com.j2biz.blogunity.pojo.Blog;
43 import com.j2biz.blogunity.web.ActionResultFactory;
44 import com.j2biz.blogunity.web.IActionResult;
45
46 /**
47  * @author michelson
48  * @version $$
49  * @since 0.1
50  *
51  *
52  */

53 public class EditThemeFileExecAction extends MyAbstractAction {
54
55     private static final IActionResult THEME_FILE_FORM_FORWARD = ActionResultFactory
56             .buildForward("/jsp/my/editThemeFileForm.jsp");
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
62      * javax.servlet.http.HttpServletResponse)
63      */

64     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
65             throws BlogunityException {
66
67         if (!(BlogunityManager.getSystemConfiguration().isThemeEditingAllowed() || user
68                 .isAdministrator())) { throw new BlogunityException(I18NStatusFactory
69                 .create(I18N.ERRORS.THEME_EDITING_NOT_ALLOWED)); }
70
71         String JavaDoc blogId = request.getParameter("id");
72         String JavaDoc fileName = request.getParameter("file");
73         String JavaDoc fileContent = request.getParameter("fileContent");
74
75         if (StringUtils.isEmpty(blogId)) { throw new BlogunityException(I18NStatusFactory.create(
76                 I18N.ERRORS.ID_NOT_SETTED, "Blog")); }
77
78         if (StringUtils.isEmpty(fileName)) { throw new BlogunityException(I18NStatusFactory
79                 .create(I18N.ERRORS.THEME_FILE_NOT_SETTED)); }
80
81         Blog blog = (new BlogDAO()).getBlogByID(new Long JavaDoc(blogId));
82
83         // BUGFIX [BGU-18]
84
if (user.getId().longValue() != blog.getFounder().getId().longValue()
85                 && !user.isAdministrator()) { throw new BlogunityException(I18NStatusFactory
86                 .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION)); }
87
88         File JavaDoc blogsDir = BlogunityManager.getSystemConfiguration().getBlogsDirectory();
89         File JavaDoc themeFile = new File JavaDoc(blogsDir, blog.getUrlName() + "/theme/" + fileName);
90
91         if (!themeFile.exists() || !themeFile.isFile() || !themeFile.canRead()
92                 || !themeFile.canWrite()) {
93
94         throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.THEME_FILE_READ_ERROR,
95                 themeFile.getName()));
96
97         }
98
99         try {
100             String JavaDoc line = null;
101             BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(themeFile));
102             writer.write(fileContent);
103             writer.close();
104         } catch (Exception JavaDoc e) {
105
106             throw new BlogunityException(I18NStatusFactory.create(
107                     I18N.ERRORS.THEME_FILE_WRITE_ERROR, themeFile.getName(), e));
108         }
109
110         return navigationStack.pop();
111     }
112 }
Popular Tags