0001: /*
0002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/GeneralAdminTasksWebHandler.java,v 1.112 2008/01/16 09:03:38 minhnn Exp $
0003: * $Author: minhnn $
0004: * $Revision: 1.112 $
0005: * $Date: 2008/01/16 09:03:38 $
0006: *
0007: * ====================================================================
0008: *
0009: * Copyright (C) 2002-2007 by MyVietnam.net
0010: *
0011: * All copyright notices regarding mvnForum MUST remain
0012: * intact in the scripts and in the outputted HTML.
0013: * The "powered by" text/logo with a link back to
0014: * http://www.mvnForum.com and http://www.MyVietnam.net in
0015: * the footer of the pages MUST remain visible when the pages
0016: * are viewed on the internet or intranet.
0017: *
0018: * This program is free software; you can redistribute it and/or modify
0019: * it under the terms of the GNU General Public License as published by
0020: * the Free Software Foundation; either version 2 of the License, or
0021: * any later version.
0022: *
0023: * This program is distributed in the hope that it will be useful,
0024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0026: * GNU General Public License for more details.
0027: *
0028: * You should have received a copy of the GNU General Public License
0029: * along with this program; if not, write to the Free Software
0030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0031: *
0032: * Support can be obtained from support forums at:
0033: * http://www.mvnForum.com/mvnforum/index
0034: *
0035: * Correspondence and Marketing Questions can be sent to:
0036: * info at MyVietnam net
0037: *
0038: * @author: Minh Nguyen
0039: * @author: Mai Nguyen
0040: * @author: Igor Manic
0041: */
0042: package com.mvnforum.admin;
0043:
0044: import java.io.*;
0045: import java.sql.Timestamp;
0046: import java.util.*;
0047:
0048: import javax.mail.MessagingException;
0049: import javax.servlet.http.HttpServletRequest;
0050: import javax.servlet.http.HttpServletResponse;
0051:
0052: import net.myvietnam.mvncore.exception.*;
0053: import net.myvietnam.mvncore.filter.HtmlNewLineFilter;
0054: import net.myvietnam.mvncore.security.SecurityUtil;
0055: import net.myvietnam.mvncore.service.EventLogService;
0056: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
0057: import net.myvietnam.mvncore.util.*;
0058: import net.myvietnam.mvncore.web.GenericRequest;
0059:
0060: import org.apache.commons.logging.Log;
0061: import org.apache.commons.logging.LogFactory;
0062:
0063: import com.mvnforum.*;
0064: import com.mvnforum.auth.*;
0065: import com.mvnforum.common.SendMailUtil;
0066: import com.mvnforum.db.*;
0067: import com.mvnforum.search.attachment.AttachmentIndexer;
0068: import com.mvnforum.search.attachment.RebuildAttachmentIndexTask;
0069: import com.mvnforum.search.member.MemberIndexer;
0070: import com.mvnforum.search.member.RebuildMemberIndexTask;
0071: import com.mvnforum.search.post.PostIndexer;
0072: import com.mvnforum.search.post.RebuildPostIndexTask;
0073:
0074: import freemarker.template.*;
0075:
0076: public class GeneralAdminTasksWebHandler {
0077:
0078: private static Log log = LogFactory
0079: .getLog(GeneralAdminTasksWebHandler.class);
0080:
0081: private OnlineUserManager onlineUserManager = OnlineUserManager
0082: .getInstance();
0083: private static EventLogService eventLogService = MvnCoreServiceFactory
0084: .getMvnCoreService().getEventLogService();
0085:
0086: public GeneralAdminTasksWebHandler() {
0087: }
0088:
0089: public void prepareShowIndex(GenericRequest request)
0090: throws DatabaseException, AuthenticationException {
0091:
0092: OnlineUser onlineUser = onlineUserManager
0093: .getOnlineUser(request);
0094: MVNForumPermission permission = onlineUser.getPermission();
0095: permission.ensureIsAuthenticated();
0096:
0097: Timestamp now = DateUtil.getCurrentGMTTimestamp();
0098: Timestamp startTimestamp = MVNForumContextListener
0099: .getInstance().getStartTimestamp();
0100: long upTime = now.getTime() - startTimestamp.getTime();
0101:
0102: request.setAttribute("StartTime", onlineUser
0103: .getGMTTimestampFormat(startTimestamp));
0104: request.setAttribute("NowTime", onlineUser
0105: .getGMTTimestampFormat(now));
0106: request.setAttribute("UpTime", DateUtil.formatDuration(upTime));
0107: }
0108:
0109: public void prepareTestSystem(GenericRequest request)
0110: throws DatabaseException, AuthenticationException {
0111:
0112: OnlineUser onlineUser = onlineUserManager
0113: .getOnlineUser(request);
0114: MVNForumPermission permission = onlineUser.getPermission();
0115: permission.ensureCanAdminSystem();
0116: }
0117:
0118: public void changeShowUserArea(GenericRequest request)
0119: throws DatabaseException, AuthenticationException,
0120: BadInputException {
0121:
0122: OnlineUser onlineUser = onlineUserManager
0123: .getOnlineUser(request);
0124: MVNForumPermission permission = onlineUser.getPermission();
0125: permission.ensureCanAdminSystem();
0126:
0127: String mode = GenericParamUtil.getParameterSafe(request,
0128: "mode", true);
0129:
0130: if (mode.equals("on")) {
0131: MVNForumConfig.setShouldShowUserArea(true);
0132: } else if (mode.equals("off")) {
0133: MVNForumConfig.setShouldShowUserArea(false);
0134: } else {
0135: throw new BadInputException("Not support this mode " + mode);
0136: }
0137: }
0138:
0139: public void processClearCache(GenericRequest request)
0140: throws BadInputException, AuthenticationException,
0141: DatabaseException {
0142:
0143: OnlineUser onlineUser = onlineUserManager
0144: .getOnlineUser(request);
0145: MVNForumPermission permission = onlineUser.getPermission();
0146: permission.ensureCanAdminSystem();
0147:
0148: String target = GenericParamUtil.getParameterSafe(request,
0149: "target", true);
0150:
0151: if (target.equals("member")) {
0152: MemberCache.getInstance().clear();
0153: } else if (target.equals("post")) {
0154: PostCache.getInstance().clear();
0155: } else if (target.equals("thread")) {
0156: ThreadCache.getInstance().clear();
0157: } else if (target.equals("forum")) {
0158: ForumCache.getInstance().clear();
0159: } else if (target.equals("category")) {
0160: CategoryCache.getInstance().clear();
0161: } else if (target.equals("all")) {
0162: MemberCache.getInstance().clear();
0163: PostCache.getInstance().clear();
0164: ThreadCache.getInstance().clear();
0165: ForumCache.getInstance().clear();
0166: CategoryCache.getInstance().clear();
0167: } else {
0168: throw new BadInputException("Not support this target "
0169: + target);
0170: }
0171: }
0172:
0173: public void prepareImportExport(HttpServletRequest request)
0174: throws DatabaseException, AuthenticationException {
0175:
0176: OnlineUser onlineUser = onlineUserManager
0177: .getOnlineUser(request);
0178: MVNForumPermission permission = onlineUser.getPermission();
0179: permission.ensureCanAdminSystem();
0180:
0181: request.setAttribute("BackupFilesOnServer", ImportWebHandler
0182: .getBackupFilesOnServer());
0183: }
0184:
0185: public void importXmlZip(HttpServletRequest request,
0186: HttpServletResponse response) throws DatabaseException,
0187: AuthenticationException, ImportException {
0188:
0189: SecurityUtil.checkHttpPostMethod(request);
0190:
0191: OnlineUser onlineUser = onlineUserManager
0192: .getOnlineUser(request);
0193: MVNForumPermission permission = onlineUser.getPermission();
0194: permission.ensureCanAdminSystem();
0195:
0196: ImportWebHandler.importXmlZip(request, response);
0197: }
0198:
0199: public void exportXmlZip(HttpServletRequest request)
0200: throws DatabaseException, AuthenticationException,
0201: ExportException {
0202:
0203: OnlineUser onlineUser = onlineUserManager
0204: .getOnlineUser(request);
0205: MVNForumPermission permission = onlineUser.getPermission();
0206: permission.ensureCanAdminSystem();
0207:
0208: ExportWebHandler.exportXmlZip(request);
0209: }
0210:
0211: public void getExportXmlZip(HttpServletRequest request,
0212: HttpServletResponse response) throws BadInputException,
0213: DatabaseException, AuthenticationException, IOException {
0214:
0215: OnlineUser onlineUser = onlineUserManager
0216: .getOnlineUser(request);
0217: MVNForumPermission permission = onlineUser.getPermission();
0218: permission.ensureCanAdminSystem();
0219:
0220: ExportWebHandler.getExportXmlZip(request, response);
0221: }
0222:
0223: public void deleteExportXmlZip(GenericRequest request)
0224: throws BadInputException, DatabaseException,
0225: AuthenticationException, IOException {
0226:
0227: OnlineUser onlineUser = onlineUserManager
0228: .getOnlineUser(request);
0229: MVNForumPermission permission = onlineUser.getPermission();
0230: permission.ensureCanAdminSystem();
0231:
0232: ExportWebHandler.deleteExportXmlZip(request);
0233: //now prepare all for redirection to "/importexport"
0234: request.setAttribute("BackupFilesOnServer", ImportWebHandler
0235: .getBackupFilesOnServer());
0236: }
0237:
0238: public void prepareRebuildIndex(GenericRequest request)
0239: throws DatabaseException, AuthenticationException {
0240:
0241: OnlineUser onlineUser = onlineUserManager
0242: .getOnlineUser(request);
0243: MVNForumPermission permission = onlineUser.getPermission();
0244: permission.ensureCanAdminSystem();
0245:
0246: int postNumDocs = PostIndexer.getNumDocs();
0247:
0248: int memberNumDocs = -1;
0249: if (DAOFactory.getMemberDAO().isSupportGetMembers()) {
0250: memberNumDocs = MemberIndexer.getNumDocs();
0251: }
0252:
0253: int attachmentNumDocs = -1;
0254: if (MVNForumConfig.getEnableAttachment()) {
0255: attachmentNumDocs = AttachmentIndexer.getNumDocs();
0256: }
0257:
0258: int postCount = DAOFactory.getPostDAO().getNumberOfPosts();
0259:
0260: int memberCount = 0;
0261: if (DAOFactory.getMemberDAO().isSupportGetNumberOfMembers()) {
0262: memberCount = DAOFactory.getMemberDAO()
0263: .getNumberOfMembers();
0264: }
0265:
0266: int attachmentCount = 0;
0267: if (MVNForumConfig.getEnableAttachment()) {
0268: attachmentCount = DAOFactory.getAttachmentDAO()
0269: .getNumberOfAttachments(-1, -1);
0270: }
0271:
0272: request.setAttribute("PostNumDocs", new Integer(postNumDocs));
0273: request.setAttribute("PostCount", new Integer(postCount));
0274:
0275: request.setAttribute("MemberNumDocs",
0276: new Integer(memberNumDocs));
0277: request.setAttribute("MemberCount", new Integer(memberCount));
0278:
0279: request.setAttribute("AttachmentNumDocs", new Integer(
0280: attachmentNumDocs));
0281: request.setAttribute("AttachmentCount", new Integer(
0282: attachmentCount));
0283: }
0284:
0285: public void rebuildIndex(GenericRequest request)
0286: throws DatabaseException, AuthenticationException {
0287:
0288: OnlineUser onlineUser = onlineUserManager
0289: .getOnlineUser(request);
0290: MVNForumPermission permission = onlineUser.getPermission();
0291: permission.ensureCanAdminSystem();
0292:
0293: String target = GenericParamUtil
0294: .getParameter(request, "target");
0295:
0296: if (target.equalsIgnoreCase("post")) {
0297:
0298: if (RebuildPostIndexTask.isRebuilding() == false) {
0299: PostIndexer.scheduleRebuildIndexTask();
0300: }
0301:
0302: } else if (target.equalsIgnoreCase("member")) {
0303:
0304: /*if (MVNForumConfig.getEnableExternalUserDatabase() == false) {
0305: log.debug("Start rebuild member index");
0306: MemberIndexer.scheduleRebuildIndexTask();
0307: }*/
0308: if (DAOFactory.getMemberDAO().isSupportGetMembers()) {
0309: if (RebuildMemberIndexTask.isRebuilding() == false) {
0310: log.debug("Start rebuild member index");
0311: MemberIndexer.scheduleRebuildIndexTask();
0312: }
0313: }
0314:
0315: } else if (target.equalsIgnoreCase("attachment")) {
0316:
0317: if (RebuildAttachmentIndexTask.isRebuilding() == false) {
0318: AttachmentIndexer.scheduleRebuildIndexTask();
0319: }
0320:
0321: } else if (target.length() == 0) {
0322:
0323: if (RebuildPostIndexTask.isRebuilding() == false) {
0324: PostIndexer.scheduleRebuildIndexTask();
0325: }
0326:
0327: if (DAOFactory.getMemberDAO().isSupportGetMembers()) {
0328: if (RebuildMemberIndexTask.isRebuilding() == false) {
0329: MemberIndexer.scheduleRebuildIndexTask();
0330: }
0331: }
0332:
0333: if (MVNForumConfig.getEnableAttachment()) {
0334: if (RebuildAttachmentIndexTask.isRebuilding() == false) {
0335: AttachmentIndexer.scheduleRebuildIndexTask();
0336: }
0337: }
0338: }
0339: }
0340:
0341: public void prepareSendMail(GenericRequest request)
0342: throws BadInputException, DatabaseException,
0343: AuthenticationException, ObjectNotFoundException {
0344:
0345: OnlineUser onlineUser = onlineUserManager
0346: .getOnlineUser(request);
0347: MVNForumPermission permission = onlineUser.getPermission();
0348: permission.ensureCanSendMail();
0349:
0350: Locale locale = I18nUtil.getLocaleInRequest(request);
0351: boolean isPreviewing = GenericParamUtil.getParameterBoolean(
0352: request, "preview");
0353:
0354: if (isPreviewing) {
0355: GenericParamUtil.getParameterEmail(request, "From");//just call to check the validity of From
0356: String to = GenericParamUtil.getParameter(request, "To");
0357: String cc = GenericParamUtil.getParameter(request, "Cc");
0358: String bcc = GenericParamUtil.getParameter(request, "Bcc");
0359: String subject = GenericParamUtil.getParameter(request,
0360: "Subject", true);
0361: String message = GenericParamUtil.getParameter(request,
0362: "Message", true);
0363:
0364: int mailToSelector = GenericParamUtil.getParameterInt(
0365: request, "MailToSelector");
0366:
0367: String warningMessage = "";
0368:
0369: String previewMessage = message;
0370: String previewSubject = subject;
0371:
0372: if (mailToSelector == 0) { // that is, send to specific users
0373: if ((to.length() == 0) && (cc.length() == 0)
0374: && (bcc.length() == 0)) {
0375: String localizedMessage = MVNForumResourceBundle
0376: .getString(locale,
0377: "mvncore.exception.BadInputException.please_enter_to_or_cc_or_bcc");
0378: throw new BadInputException(localizedMessage);
0379: //throw new BadInputException("Please enter at least To, CC or BCC.");
0380: }
0381: } else { // send to group of users
0382: // Then add members coresponding to the mailToSelector
0383: String kind;
0384: int groupID = -1;
0385: switch (mailToSelector) {
0386: case 1:
0387: kind = "all";
0388: groupID = GenericParamUtil.getParameterInt(request,
0389: "group");
0390: to = "Group: "
0391: + DAOFactory.getGroupsDAO().getGroup(
0392: groupID).getGroupName();
0393: break;
0394: case 2:
0395: kind = "activated";
0396: to = "All activated members";
0397: break;
0398: case 3:
0399: kind = "nonactivated";
0400: to = "All non-activated members";
0401: break;
0402: default:
0403: String localizedMessage = MVNForumResourceBundle
0404: .getString(
0405: locale,
0406: "mvncore.exception.BadInputException.cannot_process_mailto_selector",
0407: new Object[] { new Integer(
0408: mailToSelector) });
0409: throw new BadInputException(localizedMessage);
0410: //throw new BadInputException("Cannot process MailToSelector = " + mailToSelector);
0411: }
0412:
0413: Collection memberBeans = null;
0414:
0415: if (kind.equals("all")) {
0416: if (groupID == MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) {
0417: memberBeans = DAOFactory.getMemberDAO()
0418: .getEnableMembers_inActivationStatus(
0419: "all");
0420: } else if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) {
0421: String localizedMessage = MVNForumResourceBundle
0422: .getString(locale,
0423: "java.lang.AssertionError.cannot_list_member_in_reserved_group");
0424: throw new AssertionError(localizedMessage);
0425: //throw new AssertionError("Cannot list member in a reserved (virtual) group.");
0426: } else {
0427: memberBeans = DAOFactory.getMemberDAO()
0428: .getEnableMembers_inGroup(groupID);
0429: }
0430: } else {
0431: memberBeans = DAOFactory.getMemberDAO()
0432: .getEnableMembers_inActivationStatus(kind);
0433: }
0434:
0435: if (memberBeans.size() > 0) {
0436: warningMessage = String.valueOf(memberBeans.size()
0437: + " members");
0438: } else {
0439: warningMessage = "No members";
0440: }
0441: MemberBean previewedMember = null;
0442: for (Iterator iterator = memberBeans.iterator(); iterator
0443: .hasNext();) {
0444: previewedMember = (MemberBean) iterator.next();
0445: previewMessage = processMailTemplate(
0446: previewedMember, message);
0447: previewSubject = processMailTemplate(
0448: previewedMember, subject);
0449: request.setAttribute("PreviewEmail",
0450: previewedMember.getMemberEmail());
0451: break;
0452: }
0453: }// end of send to a group of users
0454: request.setAttribute("PreviewTo", to);
0455: request.setAttribute("PreviewCc", cc);
0456: request.setAttribute("PreviewBcc", bcc);
0457: request.setAttribute("PreviewSubject", previewSubject);
0458: request.setAttribute("PreviewMessage", HtmlNewLineFilter
0459: .filter(previewMessage));
0460: request.setAttribute("WarningMessage", warningMessage);
0461: }
0462:
0463: Collection groupBeans = DAOFactory.getGroupsDAO().getGroups();
0464: for (Iterator iterator = groupBeans.iterator(); iterator
0465: .hasNext();) {
0466: GroupsBean groupBean = (GroupsBean) iterator.next();
0467: if (groupBean.getGroupID() == MVNForumConstant.GROUP_ID_OF_GUEST) {
0468: iterator.remove();
0469: break;
0470: }
0471: }
0472: request.setAttribute("GroupBeans", groupBeans);
0473: }
0474:
0475: public void sendMail(GenericRequest request)
0476: throws BadInputException, MessagingException,
0477: DatabaseException, AuthenticationException {
0478:
0479: SecurityUtil.checkHttpPostMethod(request);
0480:
0481: OnlineUser onlineUser = onlineUserManager
0482: .getOnlineUser(request);
0483: MVNForumPermission permission = onlineUser.getPermission();
0484: permission.ensureCanSendMail();
0485:
0486: Locale locale = I18nUtil.getLocaleInRequest(request);
0487:
0488: String from = GenericParamUtil.getParameterEmail(request,
0489: "From");
0490: String to = GenericParamUtil.getParameter(request, "To");
0491: String cc = GenericParamUtil.getParameter(request, "Cc");
0492: String bcc = GenericParamUtil.getParameter(request, "Bcc");
0493: String subject = GenericParamUtil.getParameter(request,
0494: "Subject", true);
0495: String message = GenericParamUtil.getParameter(request,
0496: "Message", true);
0497:
0498: int mailToSelector = GenericParamUtil.getParameterInt(request,
0499: "MailToSelector");
0500:
0501: Collection mailMessageStructs = new ArrayList();
0502: if (mailToSelector == 0) { // that is, send to specific users
0503: if ((to.length() == 0) && (cc.length() == 0)
0504: && (bcc.length() == 0)) {
0505: String localizedMessage = MVNForumResourceBundle
0506: .getString(locale,
0507: "mvncore.exception.BadInputException.please_enter_to_or_cc_or_bcc");
0508: throw new BadInputException(localizedMessage);
0509: //throw new BadInputException("Please enter at least To, CC or BCC.");
0510: }
0511: MailMessageStruct mailMessageStruct = new MailMessageStruct();
0512: mailMessageStruct.setFrom(from);
0513: mailMessageStruct.setTo(to);
0514: mailMessageStruct.setCc(cc);
0515: mailMessageStruct.setBcc(bcc);
0516: mailMessageStruct.setSubject(subject);
0517: mailMessageStruct.setMessage(message);
0518: mailMessageStructs.add(mailMessageStruct);
0519: } else { // send to group of users
0520: // now add emails in the To, CC, BCC. Please note that we ONLY use the method setTo
0521: String[] emailArray = MailUtil.getEmails(to, cc, bcc);
0522: for (int i = 0; i < emailArray.length; i++) {
0523: MailMessageStruct mailMessage = new MailMessageStruct();
0524: mailMessage.setFrom(from);
0525: mailMessage.setTo(emailArray[i]);
0526: mailMessage.setSubject(subject);
0527: mailMessage.setMessage(message);
0528: mailMessageStructs.add(mailMessage);
0529: }
0530:
0531: // Then add members corresponding to the mailToSelector
0532: String kind;
0533: int groupID = -1;
0534: switch (mailToSelector) {
0535: case 1:
0536: kind = "all";
0537: groupID = GenericParamUtil.getParameterInt(request,
0538: "group");
0539: break;
0540: case 2:
0541: kind = "activated";
0542: break;
0543: case 3:
0544: kind = "nonactivated";
0545: break;
0546: default:
0547: String localizedMessage = MVNForumResourceBundle
0548: .getString(
0549: locale,
0550: "mvncore.exception.BadInputException.cannot_process_mailto_selector",
0551: new Object[] { new Integer(
0552: mailToSelector) });
0553: throw new BadInputException(localizedMessage);
0554: //throw new BadInputException("Cannot process MailToSelector = " + mailToSelector);
0555: }
0556:
0557: Collection memberBeans = new ArrayList();
0558: if (kind.equals("all")) {
0559: if (groupID == MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) {
0560: if (DAOFactory
0561: .getMemberDAO()
0562: .isSupportGetEnableMembers_inActivationStatus()) {
0563: memberBeans = DAOFactory.getMemberDAO()
0564: .getEnableMembers_inActivationStatus(
0565: "all");
0566: }
0567: } else if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) {
0568: String localizedMessage = MVNForumResourceBundle
0569: .getString(locale,
0570: "java.lang.AssertionError.cannot_list_member_in_reserved_group");
0571: throw new AssertionError(localizedMessage);
0572: //throw new AssertionError("Cannot list member in a reserved (virtual) group.");
0573: } else {
0574: if (DAOFactory.getMemberDAO()
0575: .isSupportGetEnableMembers_inGroup()) {
0576: memberBeans = DAOFactory.getMemberDAO()
0577: .getEnableMembers_inGroup(groupID);
0578: }
0579: }
0580: } else {
0581: if (DAOFactory.getMemberDAO()
0582: .isSupportGetEnableMembers_inActivationStatus()) {
0583: memberBeans = DAOFactory.getMemberDAO()
0584: .getEnableMembers_inActivationStatus(kind);
0585: }
0586: }
0587: for (Iterator countIterator = memberBeans.iterator(); countIterator
0588: .hasNext();) {
0589: MemberBean memberBean = (MemberBean) countIterator
0590: .next();
0591: MailMessageStruct mailMessage = new MailMessageStruct();
0592:
0593: mailMessage.setFrom(from);
0594: String memberEmail = memberBean.getMemberEmail();
0595: String processedSubject = processMailTemplate(
0596: memberBean, subject);
0597: String processedMessage = processMailTemplate(
0598: memberBean, message);
0599: mailMessage.setTo(memberEmail);
0600: mailMessage.setSubject(processedSubject);
0601: mailMessage.setMessage(processedMessage);
0602: mailMessageStructs.add(mailMessage);
0603: } //for
0604: }
0605:
0606: try {
0607: MailUtil.sendMail(mailMessageStructs);
0608: } catch (UnsupportedEncodingException e) {
0609: log.error("Cannot support encoding", e);
0610: }
0611: //check here again, we only need size of mailMessageStruts
0612: request.setAttribute("SizeOfMailMessageStructs", new Integer(
0613: mailMessageStructs.size()));
0614: request.setAttribute("MailMessageStructs", mailMessageStructs);
0615: }
0616:
0617: public void sendActivateMailToAll(GenericRequest request)
0618: throws BadInputException, MessagingException,
0619: DatabaseException, AuthenticationException,
0620: ObjectNotFoundException, IOException, TemplateException {
0621:
0622: OnlineUser onlineUser = onlineUserManager
0623: .getOnlineUser(request);
0624: MVNForumPermission permission = onlineUser.getPermission();
0625: permission.ensureCanSendMail();//@todo: check if this is the correct permission
0626:
0627: //@todo: review this constant parameter "nonactivated"
0628: Collection memberBeans = DAOFactory.getMemberDAO()
0629: .getEnableMembers_inActivationStatus("nonactivated");
0630: Collection mailMessageStructs = new ArrayList();
0631: String serverName = ParamUtil.getServerPath();//ParamUtil.getServer2(request);
0632: for (Iterator countIterator = memberBeans.iterator(); countIterator
0633: .hasNext();) {
0634: MemberBean memberBean = (MemberBean) countIterator.next();
0635: MailMessageStruct mailMessage = SendMailUtil
0636: .getActivationCodeEmail(memberBean.getMemberID(),
0637: serverName);
0638: mailMessageStructs.add(mailMessage);
0639: }//for
0640:
0641: try {
0642: log
0643: .debug("About to send activate mail to all non activated members, total = "
0644: + mailMessageStructs.size());
0645: MailUtil.sendMail(mailMessageStructs);
0646: } catch (UnsupportedEncodingException e) {
0647: log.error("Cannot support encoding", e);
0648: }
0649:
0650: request.setAttribute("MailMessageStructs", mailMessageStructs);
0651: }
0652:
0653: /**
0654: *Process mail with a template from User
0655: */
0656: private String processMailTemplate(MemberBean memberBean,
0657: String message) {
0658: try {
0659: Map root = new HashMap();
0660: StringWriter messageWriter = new StringWriter(256);
0661:
0662: //Just assume some variables are needed to be replaced
0663: root.put("memberID", new Integer(memberBean.getMemberID()));
0664: root.put("memberName", memberBean.getMemberName());
0665: root
0666: .put("memberFirstname", memberBean
0667: .getMemberFirstname());
0668: root.put("memberLastname", memberBean.getMemberLastname());
0669: root.put("memberEmail", memberBean.getMemberEmail());
0670:
0671: StringReader stringReader = new StringReader(message);
0672: Configuration cfg = MVNForumConfig
0673: .getFreeMarkerConfiguration();
0674: Template messageTemplate = new Template("", stringReader,
0675: cfg, "");
0676: messageTemplate.process(root, messageWriter);
0677: message = messageWriter.toString();
0678: } catch (IOException ioe) {
0679: log.error("Cannot process mail template", ioe);
0680: //if we have problem while processing template, we will return orginal message
0681: } catch (TemplateException te) {
0682: log.error("Cannot process mail template", te);
0683: //if we have problem while processing template, we will return orginal message
0684: }
0685: //log.debug("processMailTemplate return = " + message);
0686: return message;
0687: }
0688:
0689: public void prepareViewLogSystem(GenericRequest request)
0690: throws FileNotFoundException, DatabaseException,
0691: BadInputException, AuthenticationException, IOException {
0692:
0693: OnlineUser onlineUser = onlineUserManager
0694: .getOnlineUser(request);
0695: MVNForumPermission permission = onlineUser.getPermission();
0696: permission.ensureCanAdminSystem();
0697:
0698: // just for checking the valid value of "linecount"
0699: GenericParamUtil.getParameterUnsignedInt(request, "linecount",
0700: 25);
0701: String shortName = GenericParamUtil.getParameterSafe(request,
0702: "filename", false);
0703: String logDir = MVNForumConfig.getLogDir();
0704: String logFileName = "";
0705:
0706: if (shortName.length() == 0) {
0707: logFileName = MVNForumConfig.getLogFile();
0708: } else {
0709: File logDirFile = new File(logDir);
0710: if (!logDirFile.exists()) {
0711: throw new FileNotFoundException(
0712: "Cannot find the log dir: "
0713: + logDirFile.getAbsolutePath());
0714: }
0715:
0716: if (!logDirFile.isDirectory()) {
0717: throw new IOException(
0718: "The log dir is not a directory: "
0719: + logDirFile.getAbsolutePath());
0720: }
0721: FileUtil.checkGoodFileName(shortName);
0722: logFileName = logDir + File.separatorChar + shortName;
0723: }
0724:
0725: File logFile = new File(logFileName);
0726: if (!logFile.exists()) {
0727: throw new FileNotFoundException("Cannot find the log file "
0728: + logFile.getAbsolutePath());
0729: }
0730:
0731: long size = logFile.length();
0732: String humanSize = FileUtil.getHumanSize(size);
0733:
0734: request.setAttribute("FileName", shortName);
0735: request.setAttribute("LogDir", logDir);
0736: request.setAttribute("LogFileName", logFileName);
0737: request.setAttribute("LogFileSize", String.valueOf(size));
0738: request.setAttribute("LogFileHumanSize", humanSize);
0739:
0740: if (MvnCoreServiceFactory.getMvnCoreService()
0741: .getEnvironmentService().isPortlet()) {
0742:
0743: if (!logFile.canRead()) {
0744: throw new IOException("Cannot read the log file: "
0745: + logFile.getAbsolutePath());
0746: }
0747: int lineCount = GenericParamUtil.getParameterUnsignedInt(
0748: request, "linecount", 25); // 25, 50 100 200 400 800
0749: if (lineCount > 5000) {
0750: lineCount = 5000;
0751: }
0752: String[] contentLog = FileUtil.getLastLines(logFile,
0753: lineCount);
0754:
0755: request.setAttribute("ContentLog", contentLog);
0756: }
0757: }
0758:
0759: public void prepareLogFrame(GenericRequest request)
0760: throws DatabaseException, AuthenticationException,
0761: IOException, FileNotFoundException, BadInputException {
0762:
0763: OnlineUser onlineUser = onlineUserManager
0764: .getOnlineUser(request);
0765: MVNForumPermission permission = onlineUser.getPermission();
0766: permission.ensureCanAdminSystem();
0767:
0768: String logFileName = GenericParamUtil.getParameterSafe(request,
0769: "filename", false);
0770:
0771: if (logFileName.length() == 0) {
0772: logFileName = MVNForumConfig.getLogFile();
0773: } else {
0774: String logDir = MVNForumConfig.getLogDir();
0775: File logDirFile = new File(logDir);
0776: if (!logDirFile.exists()) {
0777: throw new FileNotFoundException(
0778: "Cannot find the log dir: "
0779: + logDirFile.getAbsolutePath());
0780: }
0781:
0782: if (!logDirFile.isDirectory()) {
0783: throw new IOException(
0784: "The log dir is not a directory: "
0785: + logDirFile.getAbsolutePath());
0786: }
0787: FileUtil.checkGoodFileName(logFileName);
0788: logFileName = logDir + File.separatorChar + logFileName;
0789: }
0790:
0791: File logFile = new File(logFileName);
0792: if (!logFile.exists()) {
0793: throw new FileNotFoundException(
0794: "Cannot find the log file: "
0795: + logFile.getAbsolutePath());
0796: }
0797: if (!logFile.canRead()) {
0798: throw new IOException("Cannot read the log file: "
0799: + logFile.getAbsolutePath());
0800: }
0801:
0802: int lineCount = GenericParamUtil.getParameterUnsignedInt(
0803: request, "linecount", 25); // 25, 50 100 200 400 800
0804: if (lineCount > 5000) {
0805: lineCount = 5000;
0806: }
0807: String[] contentLog = FileUtil.getLastLines(logFile, lineCount);
0808:
0809: request.setAttribute("ContentLog", contentLog);
0810: }
0811:
0812: public void backupSystemLog(GenericRequest request)
0813: throws DatabaseException, AuthenticationException,
0814: IOException, FileNotFoundException {
0815:
0816: OnlineUser onlineUser = onlineUserManager
0817: .getOnlineUser(request);
0818: MVNForumPermission permission = onlineUser.getPermission();
0819: permission.ensureCanAdminSystem();
0820:
0821: boolean empty = GenericParamUtil.getParameterBoolean(request,
0822: "empty");
0823:
0824: String logFileName = MVNForumConfig.getLogFile();
0825:
0826: String logDir = MVNForumConfig.getLogDir();
0827: File logDirFile = new File(logDir);
0828: if (!logDirFile.exists()) {
0829: throw new FileNotFoundException("Cannot find the log dir: "
0830: + logDirFile.getAbsolutePath());
0831: }
0832: if (!logDirFile.isDirectory()) {
0833: throw new IOException("The log dir is not a directory: "
0834: + logDirFile.getAbsolutePath());
0835: }
0836:
0837: String pattern = "yyyy-MM-dd_HH-mm-ss";
0838: String newFileName = logDir + File.separator + "mvnforumlog_"
0839: + DateUtil.format(new Date(), pattern) + ".log";
0840: File newFile = new File(newFileName);
0841: // We don't have to check file here, we check it in FileUtil.copyFile
0842: FileUtil.copyFile(logFileName, newFileName, false);
0843:
0844: if (empty) {
0845: FileUtil.emptyFile(logFileName);
0846: }
0847:
0848: long size = newFile.length();
0849: String humanSize = FileUtil.getHumanSize(size);
0850: request.setAttribute("LogFileName", StringUtil
0851: .escapeBackSlash(newFile.getAbsolutePath()));// we escape because of Pluto 1.1 bug
0852: request.setAttribute("LogFileSize", String.valueOf(size));
0853: request.setAttribute("LogFileHumanSize", humanSize);
0854: }
0855:
0856: public void prepareListLogFiles(GenericRequest request)
0857: throws DatabaseException, AuthenticationException,
0858: IOException, FileNotFoundException {
0859:
0860: OnlineUser onlineUser = onlineUserManager
0861: .getOnlineUser(request);
0862: MVNForumPermission permission = onlineUser.getPermission();
0863: permission.ensureCanAdminSystem();
0864:
0865: String logDir = MVNForumConfig.getLogDir();
0866: File logDirFile = new File(logDir);
0867: if (!logDirFile.exists()) {
0868: throw new FileNotFoundException("Cannot find the log dir: "
0869: + logDirFile.getAbsolutePath());
0870: }
0871: if (!logDirFile.isDirectory()) {
0872: throw new IOException("The log dir is not a directory: "
0873: + logDirFile.getAbsolutePath());
0874: }
0875:
0876: File[] files = logDirFile.listFiles();
0877: File currentLogFile = new File(MVNForumConfig.getLogFile());
0878:
0879: request.setAttribute("LogFiles", files);
0880: request.setAttribute("CurrentLogFile", currentLogFile);
0881: }
0882:
0883: public void downloadLogFile(HttpServletRequest request,
0884: HttpServletResponse response) throws DatabaseException,
0885: AuthenticationException, BadInputException, IOException {
0886:
0887: OnlineUser onlineUser = onlineUserManager
0888: .getOnlineUser(request);
0889: MVNForumPermission permission = onlineUser.getPermission();
0890: permission.ensureCanAdminSystem();
0891:
0892: Locale locale = I18nUtil.getLocaleInRequest(request);
0893:
0894: // This method require fileName is not empty
0895: String fileName = ParamUtil.getParameterSafe(request,
0896: "filename", true);// must check empty here
0897: FileUtil.checkGoodFileName(fileName);
0898:
0899: String logDir = MVNForumConfig.getLogDir();
0900: File logDirFile = new File(logDir);
0901: if (!logDirFile.exists()) {
0902: throw new FileNotFoundException("Cannot find the log dir: "
0903: + logDirFile.getAbsolutePath());
0904: }
0905: if (!logDirFile.isDirectory()) {
0906: throw new IOException("The log dir is not a directory: "
0907: + logDirFile.getAbsolutePath());
0908: }
0909:
0910: File file = new File(MVNForumConfig.getLogDir()
0911: + File.separatorChar + fileName);
0912: if ((!file.exists()) || (!file.isFile())) {
0913: log.error("Can't find a file " + file
0914: + " to be downloaded (or maybe it's directory).");
0915: String localizedMessage = MVNForumResourceBundle
0916: .getString(locale,
0917: "java.io.IOException.not_exist_or_not_file_to_be_downloaded");
0918: throw new IOException(localizedMessage + " " + file);
0919: //throw new IOException("Can't find a file to be downloaded (or maybe it's directory).");
0920: }
0921:
0922: BufferedOutputStream output = null;
0923: try {
0924: response.setContentType("application/octet-stream");
0925: response.setHeader("Location", fileName);
0926: response.setHeader("Content-Disposition",
0927: "attachment; filename=" + fileName);
0928: int length = (int) file.length();
0929: if (length > 0) {
0930: response.setContentLength(length);
0931: }
0932:
0933: output = new BufferedOutputStream(response
0934: .getOutputStream(), 1024 /* buffer size */);
0935: response.setBufferSize(1024);
0936:
0937: //when we start download, we cannot redirect or raise exceptions
0938: FileUtil.popFile(file, output);
0939: output.flush();
0940: } catch (FileNotFoundException e) {
0941: log.error("Can't find the such log file on server "
0942: + fileName);
0943: } catch (IOException e) {
0944: log.error(
0945: "Error while trying to send backup file from server ("
0946: + fileName + ").", e);
0947: } finally {
0948: if (output != null) {
0949: try {
0950: output.close();
0951: } catch (IOException e) {
0952: }
0953: }
0954: }
0955: }
0956:
0957: public void deleteLogFile(GenericRequest request)
0958: throws DatabaseException, AuthenticationException,
0959: BadInputException, IOException {
0960:
0961: OnlineUser onlineUser = onlineUserManager
0962: .getOnlineUser(request);
0963: MVNForumPermission permission = onlineUser.getPermission();
0964: permission.ensureCanAdminSystem();
0965:
0966: String fileName = GenericParamUtil.getParameterSafe(request,
0967: "filename", true);
0968: FileUtil.checkGoodFileName(fileName);
0969:
0970: String logDir = MVNForumConfig.getLogDir();
0971: File logDirFile = new File(logDir);
0972: if (logDirFile.exists() == false) {
0973: throw new FileNotFoundException("Cannot find the log dir: "
0974: + logDirFile.getAbsolutePath());
0975: }
0976: if (logDirFile.isDirectory() == false) {
0977: throw new IOException("The log dir is not a directory: "
0978: + logDirFile.getAbsolutePath());
0979: }
0980:
0981: fileName = logDir + File.separator + fileName;
0982: String logFileName = MVNForumConfig.getLogFile();
0983: File currentLogFile = new File(logFileName);
0984:
0985: if (currentLogFile.equals(new File(fileName))) {
0986: throw new AssertionError(
0987: "Cannot delete the current log file: " + fileName);
0988: }
0989:
0990: FileUtil.deleteFile(fileName);
0991:
0992: String actionDesc = MVNForumResourceBundle.getString(
0993: MVNForumConfig.getEventLogLocale(),
0994: "mvnforum.eventlog.desc.DeleteLogFile");
0995: eventLogService.logEvent(onlineUser.getMemberName(), request
0996: .getRemoteAddr(),
0997: MVNForumConstant.EVENT_LOG_MAIN_MODULE,
0998: MVNForumConstant.EVENT_LOG_SUB_MODULE_ADMIN,
0999: "delete log file", actionDesc, EventLogService.HIGH);
1000:
1001: }
1002:
1003: }
|