1 25 26 package com.j2biz.blogunity.web.actions.my; 27 28 import java.io.BufferedReader ; 29 import java.io.File ; 30 import java.io.FileReader ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 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 53 public class EditThemeFileFormAction extends MyAbstractAction { 54 55 private static final IActionResult THEME_FILE_FORM_FORWARD = ActionResultFactory 56 .buildForward("/jsp/my/editThemeFileForm.jsp"); 57 58 64 public IActionResult execute(HttpServletRequest request, HttpServletResponse 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 blogId = request.getParameter("id"); 72 String fileName = request.getParameter("file"); 73 74 if (StringUtils.isEmpty(blogId)) { throw new BlogunityException(I18NStatusFactory.create( 75 I18N.ERRORS.ID_NOT_SETTED, "Blog")); 76 77 } 78 79 if (StringUtils.isEmpty(fileName)) { throw new BlogunityException(I18NStatusFactory 80 .create(I18N.ERRORS.THEME_FILE_NOT_SETTED)); } 81 82 if (!(fileName.endsWith(".vm") || fileName.endsWith(".css"))) { throw new BlogunityException( 83 I18NStatusFactory.create(I18N.ERRORS.THEME_FILE_READ_ERROR, fileName)); } 84 85 Blog blog = (new BlogDAO()).getBlogByID(new Long (blogId)); 86 87 if (user.getId().longValue() != blog.getFounder().getId().longValue() 89 && !user.isAdministrator()) { throw new BlogunityException(I18NStatusFactory 90 .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION)); } 91 92 File blogsDir = BlogunityManager.getSystemConfiguration().getBlogsDirectory(); 93 File themeFile = new File (blogsDir, blog.getUrlName() + "/theme/" + fileName); 94 95 if (!themeFile.exists() || !themeFile.isFile() || !themeFile.canRead() 96 || !themeFile.canWrite()) { throw new BlogunityException(I18NStatusFactory.create( 97 I18N.ERRORS.THEME_FILE_READ_ERROR, themeFile.getAbsolutePath())); } 98 99 StringBuffer themeFileContent = new StringBuffer (); 100 try { 101 String line = null; 102 BufferedReader reader = new BufferedReader (new FileReader (themeFile)); 103 while ((line = reader.readLine()) != null) { 104 line = line.replaceAll("<", "<"); 105 line = line.replaceAll(">", ">"); 106 themeFileContent.append(line); 107 themeFileContent.append("\n"); 108 } 109 reader.close(); 110 111 } catch (Exception e) { 112 throw new BlogunityException(I18NStatusFactory.create( 113 I18N.ERRORS.THEME_FILE_READ_ERROR, themeFile.getAbsolutePath(), e)); 114 115 } 116 117 request.setAttribute("blog", blog); 118 request.setAttribute("themeFile", themeFile); 119 request.setAttribute("themeFileContent", themeFileContent.toString()); 120 121 navigationStack.push(ActionResultFactory.buildRedirect(I18N.MESSAGES.NAVI_EDIT_THEME_FILE, 122 currentActionPath)); 123 124 return THEME_FILE_FORM_FORWARD; 125 } 126 } | Popular Tags |