001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/MailWebHandler.java,v 1.27 2008/01/15 09:22:00 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.27 $
005: * $Date: 2008/01/15 09:22:00 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Hau Nguyen
039: */
040: package com.mvnforum.user;
041:
042: import java.io.UnsupportedEncodingException;
043: import java.util.Locale;
044:
045: import javax.mail.MessagingException;
046:
047: import net.myvietnam.mvncore.exception.*;
048: import net.myvietnam.mvncore.security.SecurityUtil;
049: import net.myvietnam.mvncore.util.*;
050: import net.myvietnam.mvncore.web.GenericRequest;
051:
052: import org.apache.commons.logging.Log;
053: import org.apache.commons.logging.LogFactory;
054:
055: import com.mvnforum.MVNForumConfig;
056: import com.mvnforum.MVNForumResourceBundle;
057: import com.mvnforum.auth.*;
058: import com.mvnforum.db.MemberCache;
059:
060: public class MailWebHandler {
061:
062: private static Log log = LogFactory.getLog(MailWebHandler.class);
063:
064: private OnlineUserManager onlineUserManager = OnlineUserManager
065: .getInstance();
066:
067: public void prepareSendMail(GenericRequest request)
068: throws BadInputException, ObjectNotFoundException,
069: DatabaseException, AuthenticationException {
070:
071: OnlineUser onlineUser = onlineUserManager
072: .getOnlineUser(request);
073: MVNForumPermission permission = onlineUser.getPermission();
074: permission.ensureIsAuthenticated();
075:
076: //boolean preview = GenericParamUtil.getParameterBoolean(request, "preview");
077:
078: // from
079: int fromID = onlineUser.getMemberID();
080: String fromEmail = MemberCache.getInstance().getMember(fromID)
081: .getMemberEmail();
082: request.setAttribute("FromEmail", fromEmail);
083:
084: // subject
085: String subject = GenericParamUtil.getParameterFilter(request,
086: "Subject");
087: request.setAttribute("Subject", subject);
088:
089: // message
090: String message = GenericParamUtil.getParameterFilter(request,
091: "Message");
092: request.setAttribute("Message", message);
093:
094: // to
095: String toMember = "";
096: boolean emailToAdmin = GenericParamUtil.getParameterBoolean(
097: request, "ToAdmin");
098: if (emailToAdmin == false) {
099: toMember = GenericParamUtil.getParameterFilter(request,
100: "ToMember", true);
101: try {
102: MemberCache.getInstance().getMemberIDFromMemberName(
103: toMember);
104: } catch (ObjectNotFoundException e) {
105: Locale locale = I18nUtil.getLocaleInRequest(request);
106: String localizedMessage = MVNForumResourceBundle
107: .getString(
108: locale,
109: "mvncore.exception.ObjectNotFoundException.membername_not_exists",
110: new Object[] { toMember });
111: throw new ObjectNotFoundException(localizedMessage);
112: }
113: }
114: request.setAttribute("ToAdmin", emailToAdmin ? "true" : "");
115: request.setAttribute("ToMember", toMember);
116:
117: }
118:
119: public void sendEmailProcess(GenericRequest request)
120: throws AuthenticationException, DatabaseException,
121: BadInputException, MessagingException,
122: ObjectNotFoundException {
123:
124: SecurityUtil.checkHttpPostMethod(request);
125:
126: OnlineUser onlineUser = onlineUserManager
127: .getOnlineUser(request);
128: MVNForumPermission permission = onlineUser.getPermission();
129: permission.ensureIsAuthenticated();
130:
131: int fromID = onlineUser.getMemberID();
132: String from = MemberCache.getInstance().getMember(fromID)
133: .getMemberEmail();
134:
135: String subject = GenericParamUtil.getParameter(request,
136: "Subject", true);
137: String message = GenericParamUtil.getParameter(request,
138: "Message", true);
139:
140: String to = null;
141: boolean emailToAdmin = GenericParamUtil.getParameterBoolean(
142: request, "ToAdmin");
143: if (emailToAdmin == false) {
144: String toMember = GenericParamUtil.getParameterFilter(
145: request, "ToMember", true);
146: int toID = MemberCache.getInstance()
147: .getMemberIDFromMemberName(toMember);
148: to = MemberCache.getInstance().getMember(toID)
149: .getMemberEmail();
150: } else {
151: to = MVNForumConfig.getWebMasterEmail();
152: }
153: if (to.length() == 0) {
154: Locale locale = I18nUtil.getLocaleInRequest(request);
155: String localizedMessage = MVNForumResourceBundle
156: .getString(locale,
157: "mvncore.exception.BadInputException.please_enter_to_or_cc_or_bcc");
158: throw new BadInputException(localizedMessage);
159: }
160:
161: MailMessageStruct mailMessageStruct = new MailMessageStruct();
162: mailMessageStruct.setFrom(from);
163: mailMessageStruct.setTo(to);
164: mailMessageStruct.setSubject(subject);
165: mailMessageStruct.setMessage(message);
166:
167: try {
168: MailUtil.sendMail(mailMessageStruct);
169: } catch (UnsupportedEncodingException e) {
170: log.error("Cannot support encoding", e);
171: }
172:
173: }
174:
175: }
|