1 42 package com.mvnforum.admin; 43 44 import java.io.*; 45 import java.sql.Timestamp ; 46 import java.util.*; 47 import java.util.Date ; 48 49 import javax.mail.MessagingException ; 50 import javax.servlet.http.HttpServletRequest ; 51 import javax.servlet.http.HttpServletResponse ; 52 53 import com.mvnforum.*; 54 import com.mvnforum.auth.*; 55 import com.mvnforum.common.SendMailUtil; 56 import com.mvnforum.db.*; 57 import com.mvnforum.search.company.CompanyIndexer; 58 import com.mvnforum.search.member.MemberIndexer; 59 import com.mvnforum.search.post.PostIndexer; 60 import com.mvnforum.search.post.RebuildPostIndexTask; 61 import freemarker.template.*; 62 import net.myvietnam.mvncore.exception.*; 63 import net.myvietnam.mvncore.filter.HtmlNewLineFilter; 64 import net.myvietnam.mvncore.util.*; 65 import net.myvietnam.mvncore.web.GenericRequest; 66 import org.apache.commons.logging.Log; 67 import org.apache.commons.logging.LogFactory; 68 69 public class GeneralAdminTasksWebHandler { 70 71 private static Log log = LogFactory.getLog(GeneralAdminTasksWebHandler.class); 72 73 private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance(); 74 75 public GeneralAdminTasksWebHandler() { 76 } 77 78 public void prepareShowIndex(GenericRequest request) 79 throws DatabaseException, AssertionException, AuthenticationException { 80 81 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 82 MVNForumPermission permission = onlineUser.getPermission(); 83 permission.ensureIsAuthenticated(); 84 85 Timestamp now = DateUtil.getCurrentGMTTimestamp(); 86 Timestamp startTimestamp = MVNForumContextListener.getInstance().getStartTimestamp(); 87 long upTime = now.getTime() - startTimestamp.getTime(); 88 89 request.setAttribute("StartTime", onlineUser.getGMTTimestampFormat(startTimestamp)); 90 request.setAttribute("NowTime", onlineUser.getGMTTimestampFormat(now)); 91 request.setAttribute("UpTime", DateUtil.formatDuration(upTime)); 92 } 93 94 public void prepareTestSystem(GenericRequest request) 95 throws DatabaseException, AssertionException, AuthenticationException { 96 97 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 98 MVNForumPermission permission = onlineUser.getPermission(); 99 permission.ensureCanAdminSystem(); 100 } 101 102 public void changeShowUserArea(GenericRequest request) 103 throws DatabaseException, AuthenticationException, 104 BadInputException, AssertionException { 105 106 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 107 MVNForumPermission permission = onlineUser.getPermission(); 108 permission.ensureCanAdminSystem(); 109 110 String mode = GenericParamUtil.getParameterSafe(request, "mode", true); 111 112 if (mode.equals("on")) { 113 MVNForumConfig.setShouldShowUserArea(true); 114 } else if (mode.equals("off")) { 115 MVNForumConfig.setShouldShowUserArea(false); 116 } else { 117 throw new BadInputException("Not support this mode " + mode); 118 } 119 } 120 121 public void processClearCache(GenericRequest request) 122 throws BadInputException, AuthenticationException, DatabaseException, AssertionException { 123 124 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 125 MVNForumPermission permission = onlineUser.getPermission(); 126 permission.ensureCanAdminSystem(); 127 128 String target = GenericParamUtil.getParameterSafe(request, "target", true); 129 130 if (target.equals("member")) { 131 MemberCache.getInstance().clear(); 132 } else if (target.equals("post")) { 133 PostCache.getInstance().clear(); 134 } else if (target.equals("thread")) { 135 ThreadCache.getInstance().clear(); 136 } else if (target.equals("forum")) { 137 ForumCache.getInstance().clear(); 138 } else if (target.equals("category")) { 139 CategoryCache.getInstance().clear(); 140 } else if (target.equals("all")) { 141 MemberCache.getInstance().clear(); 142 PostCache.getInstance().clear(); 143 ThreadCache.getInstance().clear(); 144 ForumCache.getInstance().clear(); 145 CategoryCache.getInstance().clear(); 146 } else { 147 throw new BadInputException("Not support this target " + target); 148 } 149 } 150 151 public void prepareImportExport(HttpServletRequest request) 152 throws DatabaseException, AssertionException, AuthenticationException { 153 154 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 155 MVNForumPermission permission = onlineUser.getPermission(); 156 permission.ensureCanAdminSystem(); 157 158 request.setAttribute("BackupFilesOnServer", ImportWebHandler.getBackupFilesOnServer()); 159 } 160 161 public void importXmlZip(HttpServletRequest request, HttpServletResponse response) 162 throws DatabaseException, AssertionException, AuthenticationException, ImportException { 163 164 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 165 MVNForumPermission permission = onlineUser.getPermission(); 166 permission.ensureCanAdminSystem(); 167 168 ImportWebHandler.importXmlZip(request, response); 169 } 170 171 public void exportXmlZip(HttpServletRequest request) 172 throws DatabaseException, AssertionException, AuthenticationException, ExportException { 173 174 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 175 MVNForumPermission permission = onlineUser.getPermission(); 176 permission.ensureCanAdminSystem(); 177 178 ExportWebHandler.exportXmlZip(request); 179 } 180 181 public void getExportXmlZip(HttpServletRequest request, HttpServletResponse response) 182 throws BadInputException, DatabaseException, AssertionException, AuthenticationException, IOException { 183 184 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 185 MVNForumPermission permission = onlineUser.getPermission(); 186 permission.ensureCanAdminSystem(); 187 188 ExportWebHandler.getExportXmlZip(request, response); 189 } 190 191 public void deleteExportXmlZip(GenericRequest request) 192 throws BadInputException, DatabaseException, AssertionException, AuthenticationException, IOException { 193 194 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 195 MVNForumPermission permission = onlineUser.getPermission(); 196 permission.ensureCanAdminSystem(); 197 198 ExportWebHandler.deleteExportXmlZip(request); 199 request.setAttribute("BackupFilesOnServer", ImportWebHandler.getBackupFilesOnServer()); 201 } 202 203 public void prepareRebuildIndex(GenericRequest request) 204 throws DatabaseException, AssertionException, AuthenticationException { 205 206 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 207 MVNForumPermission permission = onlineUser.getPermission(); 208 permission.ensureCanAdminSystem(); 209 210 int postNumDocs = PostIndexer.getNumDocs(); 211 int memberNumDocs = MemberIndexer.getNumDocs(); 212 int companyNumDocs = -1; 213 if (MVNForumConfig.getEnableCompany()) { 214 companyNumDocs = CompanyIndexer.getNumDocs(); 215 } 216 217 int postCount = DAOFactory.getPostDAO().getNumberOfPosts(); 218 int memberCount = DAOFactory.getMemberDAO().getNumberOfMembers(); 219 int companyCount = 0; 220 if (MVNForumConfig.getEnableCompany()) { 221 companyCount = DAOFactory.getCompanyDAO().getNumberOfCompanies(); 222 } 223 224 request.setAttribute("PostNumDocs", new Integer (postNumDocs)); 225 request.setAttribute("PostCount", new Integer (postCount)); 226 227 request.setAttribute("MemberNumDocs", new Integer (memberNumDocs)); 228 request.setAttribute("MemberCount", new Integer (memberCount)); 229 230 request.setAttribute("CompanyNumDocs", new Integer (companyNumDocs)); 231 request.setAttribute("CompanyCount", new Integer (companyCount)); 232 } 233 234 public void rebuildIndex(GenericRequest request) 235 throws DatabaseException, AssertionException, AuthenticationException { 236 237 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 238 MVNForumPermission permission = onlineUser.getPermission(); 239 permission.ensureCanAdminSystem(); 240 241 String target = GenericParamUtil.getParameter(request, "target"); 242 243 if (target.equalsIgnoreCase("post")) { 244 if ( !RebuildPostIndexTask.isRebuilding()) { 245 PostIndexer.scheduleRebuildIndexTask(); 246 } 247 } else if (target.equalsIgnoreCase("member")) { 248 MemberIndexer.scheduleRebuildIndexTask(); 249 } else if (target.equalsIgnoreCase("company") && MVNForumConfig.getEnableCompany()) { 250 CompanyIndexer.scheduleRebuildIndexTask(); 251 } else if (target.length() == 0) { 252 PostIndexer.scheduleRebuildIndexTask(); 253 MemberIndexer.scheduleRebuildIndexTask(); 254 if (MVNForumConfig.getEnableCompany()) { 255 CompanyIndexer.scheduleRebuildIndexTask(); 256 } 257 } 258 } 259 260 public void prepareSendMail(GenericRequest request) 261 throws BadInputException, DatabaseException, 262 AuthenticationException, AssertionException, ObjectNotFoundException { 263 264 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 265 MVNForumPermission permission = onlineUser.getPermission(); 266 permission.ensureCanSendMail(); 267 268 Locale locale = I18nUtil.getLocaleInRequest(request); 269 boolean isPreviewing = GenericParamUtil.getParameterBoolean(request, "preview"); 270 271 if (isPreviewing) { 272 GenericParamUtil.getParameterEmail(request, "From"); String to = GenericParamUtil.getParameter(request, "To"); 274 String cc = GenericParamUtil.getParameter(request, "Cc"); 275 String bcc = GenericParamUtil.getParameter(request, "Bcc"); 276 String subject = GenericParamUtil.getParameter(request, "Subject", true); 277 String message = GenericParamUtil.getParameter(request, "Message", true); 278 279 int mailToSelector = GenericParamUtil.getParameterInt(request, "MailToSelector"); 280 281 String warningMessage = ""; 282 283 String previewMessage = message; 284 String previewSubject = subject; 285 286 if (mailToSelector == 0) { if ((to.length() == 0) && (cc.length() == 0) && (bcc.length() == 0)) { 288 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.please_enter_to_or_cc_or_bcc"); 289 throw new BadInputException(localizedMessage); 290 } 292 } else { String kind; 295 int groupID = -1; 296 switch (mailToSelector) { 297 case 1: 298 kind = "all"; 299 groupID = GenericParamUtil.getParameterInt(request, "group"); 300 to = "Group: " + DAOFactory.getGroupsDAO().getGroup(groupID).getGroupName(); 301 break; 302 case 2: 303 kind = "activated"; 304 to = "All activated members"; 305 break; 306 case 3: 307 kind = "nonactivated"; 308 to = "All non-activated members"; 309 break; 310 default: 311 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_process_mailto_selector", new Object [] {new Integer (mailToSelector)}); 312 throw new BadInputException(localizedMessage); 313 } 315 316 Collection memberBeans = null; 317 318 if (kind.equals("all")) { 319 if (groupID == MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) { 320 memberBeans = DAOFactory.getMemberDAO().getEnableMembers_inActivationStatus("all"); 321 } else if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) { 322 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_list_member_in_reserved_group"); 323 throw new AssertionException(localizedMessage); 324 } else { 326 memberBeans = DAOFactory.getMemberDAO().getEnableMembers_inGroup(groupID); 327 } 328 } else { 329 memberBeans = DAOFactory.getMemberDAO().getEnableMembers_inActivationStatus(kind); 330 } 331 332 if (memberBeans.size() > 0 ) { 333 warningMessage = String.valueOf(memberBeans.size() + " members"); 334 } else { 335 warningMessage = "No members"; 336 } 337 MemberBean previewedMember = null; 338 for(Iterator iterator = memberBeans.iterator();iterator.hasNext();) { 339 previewedMember = (MemberBean)iterator.next(); 340 previewMessage = processMailTemplate(previewedMember, message); 341 previewSubject = processMailTemplate(previewedMember, subject); 342 request.setAttribute("PreviewEmail", previewedMember.getMemberEmail()); 343 break; 344 } 345 } request.setAttribute("PreviewTo", to); 347 request.setAttribute("PreviewCc", cc); 348 request.setAttribute("PreviewBcc", bcc); 349 request.setAttribute("PreviewSubject", previewSubject); 350 request.setAttribute("PreviewMessage", HtmlNewLineFilter.filter(previewMessage)); 351 request.setAttribute("WarningMessage", warningMessage); 352 } 353 354 Collection groupBeans = DAOFactory.getGroupsDAO().getGroups(); 355 for(Iterator iterator = groupBeans.iterator(); iterator.hasNext();) { 356 GroupsBean groupBean = (GroupsBean)iterator.next(); 357 if (groupBean.getGroupID() == MVNForumConstant.GROUP_ID_OF_GUEST) { 358 iterator.remove(); 359 break; 360 } 361 } 362 request.setAttribute("GroupBeans", groupBeans); 363 } 364 365 public void sendMail(GenericRequest request) 366 throws BadInputException, MessagingException , DatabaseException, 367 AuthenticationException, AssertionException { 368 369 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 370 MVNForumPermission permission = onlineUser.getPermission(); 371 permission.ensureCanSendMail(); 372 373 Locale locale = I18nUtil.getLocaleInRequest(request); 374 375 String from = GenericParamUtil.getParameterEmail(request, "From"); 376 String to = GenericParamUtil.getParameter(request, "To"); 377 String cc = GenericParamUtil.getParameter(request, "Cc"); 378 String bcc = GenericParamUtil.getParameter(request, "Bcc"); 379 String subject = GenericParamUtil.getParameter(request, "Subject", true); 380 String message = GenericParamUtil.getParameter(request, "Message", true); 381 382 int mailToSelector = GenericParamUtil.getParameterInt(request, "MailToSelector"); 383 384 Collection mailMessageStructs = new ArrayList(); 385 if (mailToSelector == 0) { if ((to.length() == 0) && (cc.length() == 0) && (bcc.length() == 0)) { 387 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.please_enter_to_or_cc_or_bcc"); 388 throw new BadInputException(localizedMessage); 389 } 391 MailMessageStruct mailMessageStruct = new MailMessageStruct(); 392 mailMessageStruct.setFrom(from); 393 mailMessageStruct.setTo(to); 394 mailMessageStruct.setCc(cc); 395 mailMessageStruct.setBcc(bcc); 396 mailMessageStruct.setSubject(subject); 397 mailMessageStruct.setMessage(message); 398 mailMessageStructs.add(mailMessageStruct); 399 } else { String [] emailArray = MailUtil.getEmails(to, cc, bcc); 402 for (int i = 0; i < emailArray.length; i++) { 403 MailMessageStruct mailMessage = new MailMessageStruct(); 404 mailMessage.setFrom(from); 405 mailMessage.setTo(emailArray[i]); 406 mailMessage.setSubject(subject); 407 mailMessage.setMessage(message); 408 mailMessageStructs.add(mailMessage); 409 } 410 411 String kind; 413 int groupID = -1; 414 switch (mailToSelector) { 415 case 1: 416 kind = "all"; 417 groupID = GenericParamUtil.getParameterInt(request, "group"); 418 break; 419 case 2: 420 kind = "activated"; 421 break; 422 case 3: 423 kind = "nonactivated"; 424 break; 425 default: 426 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_process_mailto_selector", new Object [] {new Integer (mailToSelector)}); 427 throw new BadInputException(localizedMessage); 428 } 430 431 Collection memberBeans = null; 432 if (kind.equals("all")) { 433 if (groupID == MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) { 434 memberBeans = DAOFactory.getMemberDAO().getEnableMembers_inActivationStatus("all"); 435 } else if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) { 436 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_list_member_in_reserved_group"); 437 throw new AssertionException(localizedMessage); 438 } else { 440 memberBeans = DAOFactory.getMemberDAO().getEnableMembers_inGroup(groupID); 441 } 442 } else { 443 memberBeans = DAOFactory.getMemberDAO().getEnableMembers_inActivationStatus(kind); 444 } 445 for (Iterator countIterator = memberBeans.iterator(); countIterator.hasNext(); ) { 446 MemberBean memberBean = (MemberBean) countIterator.next(); 447 MailMessageStruct mailMessage = new MailMessageStruct(); 448 449 mailMessage.setFrom(from); 450 String memberEmail = memberBean.getMemberEmail(); 451 String processedSubject = processMailTemplate(memberBean, subject); 452 String processedMessage = processMailTemplate(memberBean, message); 453 mailMessage.setTo(memberEmail); 454 mailMessage.setSubject(processedSubject); 455 mailMessage.setMessage(processedMessage); 456 mailMessageStructs.add(mailMessage); 457 } } 459 460 try { 461 MailUtil.sendMail(mailMessageStructs); 462 } catch (UnsupportedEncodingException e) { 463 log.error("Cannot support encoding", e); 464 } 465 request.setAttribute("MailMessageStructs", mailMessageStructs); 466 } 467 468 public void sendActivateMailToAll(GenericRequest request) 469 throws BadInputException, MessagingException , DatabaseException, 470 AuthenticationException, AssertionException, ObjectNotFoundException, IOException, TemplateException { 471 472 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 473 MVNForumPermission permission = onlineUser.getPermission(); 474 permission.ensureCanSendMail(); 476 Collection memberBeans = DAOFactory.getMemberDAO().getEnableMembers_inActivationStatus("nonactivated"); 478 Collection mailMessageStructs = new ArrayList(); 479 String serverName = ParamUtil.getServerPath(); for (Iterator countIterator = memberBeans.iterator(); countIterator.hasNext(); ) { 481 MemberBean memberBean = (MemberBean) countIterator.next(); 482 MailMessageStruct mailMessage = SendMailUtil.getActivationCodeEmail(memberBean.getMemberID(), serverName); 483 mailMessageStructs.add(mailMessage); 484 } 486 try { 487 log.debug("About to send activate mail to all non activated members, total = " + mailMessageStructs.size()); 488 MailUtil.sendMail(mailMessageStructs); 489 } catch (UnsupportedEncodingException e) { 490 log.error("Cannot support encoding", e); 491 } 492 493 request.setAttribute("MailMessageStructs", mailMessageStructs); 494 } 495 496 499 private String processMailTemplate(MemberBean memberBean, String message) { 500 try { 501 Map root = new HashMap(); 502 StringWriter messageWriter = new StringWriter(256); 503 504 root.put("memberID", new Integer (memberBean.getMemberID())); 506 root.put("memberName", memberBean.getMemberName()); 507 root.put("memberFirstname", memberBean.getMemberFirstname()); 508 root.put("memberLastname", memberBean.getMemberLastname()); 509 root.put("memberEmail", memberBean.getMemberEmail()); 510 511 StringReader stringReader = new StringReader(message); 512 Configuration cfg = MVNForumConfig.getFreeMarkerConfiguration(); 513 Template messageTemplate = new Template("", stringReader, cfg, ""); 514 messageTemplate.process(root, messageWriter); 515 message = messageWriter.toString(); 516 } catch (IOException ioe) { 517 log.error("Cannot process mail template", ioe); 518 } catch (TemplateException te) { 520 log.error("Cannot process mail template", te); 521 } 523 return message; 525 } 526 527 public void prepareViewLogSystem(GenericRequest request) 528 throws FileNotFoundException, DatabaseException, 529 BadInputException, AuthenticationException, AssertionException, IOException { 530 531 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 532 MVNForumPermission permission = onlineUser.getPermission(); 533 permission.ensureCanAdminSystem(); 534 535 GenericParamUtil.getParameterUnsignedInt(request, "linecount", 25); 537 String shortName = GenericParamUtil.getParameterSafe(request, "filename", false); 538 String logDir = MVNForumConfig.getLogDir(); 539 String logFileName = ""; 540 541 if (shortName.length() == 0) { 542 logFileName = MVNForumConfig.getLogFile(); 543 } else { 544 File logDirFile = new File(logDir); 545 if (!logDirFile.exists()) { 546 throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath()); 547 } 548 549 if (!logDirFile.isDirectory()) { 550 throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath()); 551 } 552 FileUtil.checkGoodFileName(shortName); 553 logFileName = logDir + File.separatorChar + shortName; 554 } 555 556 File logFile = new File(logFileName); 557 if (!logFile.exists()) { 558 throw new FileNotFoundException("Cannot find the log file " + logFile.getAbsolutePath()); 559 } 560 561 long size = logFile.length(); 562 String humanSize = FileUtil.getHumanSize(size); 563 564 request.setAttribute("FileName", shortName); 565 request.setAttribute("LogDir", logDir); 566 request.setAttribute("LogFileName", logFileName); 567 request.setAttribute("LogFileSize", String.valueOf(size)); 568 request.setAttribute("LogFileHumanSize", humanSize); 569 570 if (MVNForumConfig.getEnablePortlet()) { 571 572 if (!logFile.canRead()) { 573 throw new IOException("Cannot read the log file: " + logFile.getAbsolutePath()); 574 } 575 int lineCount = GenericParamUtil.getParameterUnsignedInt(request, "linecount", 25); if (lineCount > 5000) { 577 lineCount = 5000; 578 } 579 String [] contentLog = FileUtil.getLastLines(logFile, lineCount); 580 581 request.setAttribute("ContentLog", contentLog); 582 } 583 } 584 585 public void prepareLogFrame(GenericRequest request) 586 throws DatabaseException, AuthenticationException, IOException, 587 FileNotFoundException, AssertionException, BadInputException { 588 589 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 590 MVNForumPermission permission = onlineUser.getPermission(); 591 permission.ensureCanAdminSystem(); 592 593 String logFileName = GenericParamUtil.getParameterSafe(request, "filename", false); 594 595 if (logFileName.length() == 0) { 596 logFileName = MVNForumConfig.getLogFile(); 597 } else { 598 String logDir = MVNForumConfig.getLogDir(); 599 File logDirFile = new File(logDir); 600 if (!logDirFile.exists()) { 601 throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath()); 602 } 603 604 if (!logDirFile.isDirectory()) { 605 throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath()); 606 } 607 FileUtil.checkGoodFileName(logFileName); 608 logFileName = logDir + File.separatorChar + logFileName; 609 } 610 611 File logFile = new File(logFileName); 612 if (!logFile.exists()) { 613 throw new FileNotFoundException("Cannot find the log file: " + logFile.getAbsolutePath()); 614 } 615 if (!logFile.canRead()) { 616 throw new IOException("Cannot read the log file: " + logFile.getAbsolutePath()); 617 } 618 619 int lineCount = GenericParamUtil.getParameterUnsignedInt(request, "linecount", 25); if (lineCount > 5000) { 621 lineCount = 5000; 622 } 623 int offset = 0; 624 if (offset < 0) offset = 0; 626 String [] contentLog = FileUtil.getLastLines(logFile, lineCount); 627 628 request.setAttribute("Offset", new Integer (offset)); 629 request.setAttribute("ContentLog", contentLog); 630 } 631 632 public void backupSystemLog(GenericRequest request) 633 throws DatabaseException, AuthenticationException, 634 IOException, FileNotFoundException, AssertionException { 635 636 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 637 MVNForumPermission permission = onlineUser.getPermission(); 638 permission.ensureCanAdminSystem(); 639 640 boolean empty = GenericParamUtil.getParameterBoolean(request, "empty"); 641 642 String logFileName = MVNForumConfig.getLogFile(); 643 644 String logDir = MVNForumConfig.getLogDir(); 645 File logDirFile = new File(logDir); 646 if (!logDirFile.exists()) { 647 throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath()); 648 } 649 if (!logDirFile.isDirectory()) { 650 throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath()); 651 } 652 653 String pattern = "yyyy-MM-dd_HH-mm-ss"; 654 String newFileName = logDir + File.separator + "mvnforumlog_" + DateUtil.format(new Date (), pattern) + ".log"; 655 File newFile = new File(newFileName); 656 FileUtil.copyFile(logFileName, newFileName, false); 658 659 if (empty) { 660 FileUtil.emptyFile(logFileName); 661 } 662 663 long size = newFile.length(); 664 String humanSize = FileUtil.getHumanSize(size); 665 666 request.setAttribute("LogFileName", newFile.getAbsolutePath()); 667 request.setAttribute("LogFileSize", String.valueOf(size)); 668 request.setAttribute("LogFileHumanSize", humanSize); 669 } 670 671 public void prepareListLogFiles(GenericRequest request) 672 throws DatabaseException, AuthenticationException, 673 IOException, FileNotFoundException, AssertionException { 674 675 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 676 MVNForumPermission permission = onlineUser.getPermission(); 677 permission.ensureCanAdminSystem(); 678 679 String logDir = MVNForumConfig.getLogDir(); 680 File logDirFile = new File(logDir); 681 if (!logDirFile.exists()) { 682 throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath()); 683 } 684 if (!logDirFile.isDirectory()) { 685 throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath()); 686 } 687 688 File[] files = logDirFile.listFiles(); 689 File currentLogFile = new File(MVNForumConfig.getLogFile()); 690 691 request.setAttribute("LogFiles", files); 692 request.setAttribute("CurrentLogFile", currentLogFile); 693 } 694 695 public void downloadLogFile(HttpServletRequest request, HttpServletResponse response) 696 throws DatabaseException, AuthenticationException, AssertionException, BadInputException, IOException { 697 698 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 699 MVNForumPermission permission = onlineUser.getPermission(); 700 permission.ensureCanAdminSystem(); 701 702 Locale locale = I18nUtil.getLocaleInRequest(request); 703 704 String fileName = ParamUtil.getParameterSafe(request, "filename", true); FileUtil.checkGoodFileName(fileName); 707 708 String logDir = MVNForumConfig.getLogDir(); 709 File logDirFile = new File(logDir); 710 if (!logDirFile.exists()) { 711 throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath()); 712 } 713 if (!logDirFile.isDirectory()) { 714 throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath()); 715 } 716 717 File file = new File(MVNForumConfig.getLogDir() + File.separatorChar + fileName); 718 if ((!file.exists()) || (!file.isFile())) { 719 log.error("Can't find a file " + file + " to be downloaded (or maybe it's directory)."); 720 String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.not_exist_or_not_file_to_be_downloaded"); 721 throw new IOException(localizedMessage + " " + file); 722 } 724 725 BufferedOutputStream output = null; 726 try { 727 response.setContentType("application/octet-stream"); 728 response.setHeader("Location", fileName); 729 response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 730 int length = (int) file.length(); 731 if (length > 0) { 732 response.setContentLength(length); 733 } 734 735 output = new BufferedOutputStream(response.getOutputStream(), 1024 ); 736 response.setBufferSize(1024); 737 738 FileUtil.popFile(file, output); 740 output.flush(); 741 } catch (FileNotFoundException e) { 742 log.error("Can't find the such log file on server " + fileName); 743 } catch (IOException e) { 744 log.error("Error while trying to send backup file from server (" + fileName + ").", e); 745 } finally { 746 if (output != null) { 747 try { 748 output.close(); 749 } catch (IOException e) { } 750 } 751 } 752 } 753 754 public void deleteLogFile(GenericRequest request) 755 throws DatabaseException, AuthenticationException, AssertionException, BadInputException, IOException { 756 757 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 758 MVNForumPermission permission = onlineUser.getPermission(); 759 permission.ensureCanAdminSystem(); 760 761 String fileName = GenericParamUtil.getParameterSafe(request, "filename", true); 762 FileUtil.checkGoodFileName(fileName); 763 764 String logDir = MVNForumConfig.getLogDir(); 765 File logDirFile = new File(logDir); 766 if (!logDirFile.exists()) { 767 throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath()); 768 } 769 if (!logDirFile.isDirectory()) { 770 throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath()); 771 } 772 773 fileName = logDir + File.separator + fileName; 774 String logFileName = MVNForumConfig.getLogFile(); 775 File currentLogFile = new File(logFileName); 776 777 if (currentLogFile.equals(new File(fileName))) { 778 throw new AssertionException("Cannot delete the current log file: " + fileName); 779 } 780 781 FileUtil.deleteFile(fileName); 782 } 783 784 } 785 | Popular Tags |