1 25 26 package com.j2biz.blogunity.web.actions.my; 27 28 import java.io.File ; 29 import java.io.FileFilter ; 30 import java.util.Arrays ; 31 import java.util.Comparator ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.commons.lang.StringUtils; 37 38 import com.j2biz.blogunity.BlogunityManager; 39 import com.j2biz.blogunity.dao.BlogDAO; 40 import com.j2biz.blogunity.exception.BlogunityException; 41 import com.j2biz.blogunity.i18n.I18N; 42 import com.j2biz.blogunity.i18n.I18NStatusFactory; 43 import com.j2biz.blogunity.pojo.Blog; 44 import com.j2biz.blogunity.web.ActionResultFactory; 45 import com.j2biz.blogunity.web.IActionResult; 46 47 54 public class ListLogsAction extends MyAbstractAction { 55 56 private static final IActionResult LOGS_FORWARD = ActionResultFactory 57 .buildForward("/jsp/my/listLogs.jsp"); 58 59 65 public IActionResult execute(HttpServletRequest request, HttpServletResponse response) 66 throws BlogunityException { 67 68 String blogId = request.getParameter("id"); 69 70 if (StringUtils.isEmpty(blogId)) 71 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ID_NOT_SETTED, 72 "Blog")); 73 74 Blog blog = (new BlogDAO()).getBlogByID(new Long (blogId)); 75 76 if (user.getId().longValue() != blog.getFounder().getId().longValue() 77 && !user.isAdministrator()) { throw new BlogunityException(I18NStatusFactory 78 .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION)); } 79 80 File blogsDir = BlogunityManager.getSystemConfiguration().getBlogsDirectory(); 81 File blogLogsDir = new File (blogsDir, blog.getUrlName() + "/logs"); 82 83 if (!blogLogsDir.exists()) { 84 boolean result = blogLogsDir.mkdir(); 86 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 87 I18N.ERRORS.CREATE_DIRECTORY, blogLogsDir.getAbsolutePath())); } 88 89 } else if (!blogLogsDir.isDirectory() || !blogLogsDir.canRead() || !blogLogsDir.canWrite()) { throw new BlogunityException( 90 I18NStatusFactory.create(I18N.ERRORS.READ_DIRECTORY, blogLogsDir.getAbsolutePath())); } 91 92 File [] logFiles = blogLogsDir.listFiles(new FileFilter () { 93 public boolean accept(File f) { 94 String name = f.getName(); 95 if (f.isFile() && (name.endsWith(".log"))) return true; 96 return false; 97 } 98 }); 99 100 Arrays.sort(logFiles, new Comparator () { 101 public int compare(Object o1, Object o2) { 102 File f1 = (File ) o1; 103 File f2 = (File ) o2; 104 105 return (int) (f2.lastModified() - f1.lastModified()); 106 } 107 }); 108 109 request.setAttribute("blog", blog); 110 request.setAttribute("logFiles", logFiles); 111 112 navigationStack.push(ActionResultFactory.buildRedirect(I18N.MESSAGES.NAVI_LIST_LOGS, 113 currentActionPath)); 114 115 return LOGS_FORWARD; 116 } 117 118 } | Popular Tags |