📄 postthreadaction.java
字号:
package org.redsoft.forum.web;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.mail.MessagingException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;
import org.redsoft.forum.dao.PersistentThread;
import org.redsoft.forum.dao.ThreadDAO;
import org.redsoft.forum.dao.DAOFactory;
import org.redsoft.forum.dao.AccountDAO;
import org.redsoft.forum.util.ForumUtils;
import org.redsoft.forum.util.StringUtils;
import org.redsoft.forum.ForumConstants;
import org.redsoft.forum.exception.ThreadNotFoundException;
import org.redsoft.forum.exception.AccountNotFoundException;
import org.redsoft.forum.mail.SendMail;
import org.redsoft.forum.mail.MailAgent;
/**
* Post a thread
*
* @author Charles Huang
* @version 1,March 10,2002
*/
public final class PostThreadAction extends Action {
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
final ActionErrors errors = new ActionErrors();
final String author = ((PostThreadForm) form).getAuthor();
String content = ((PostThreadForm) form).getContent();
String subject = ((PostThreadForm) form).getSubject();
final String parentID = ((PostThreadForm) form).getParentID();
final String category = ((PostThreadForm) form).getCategory();
final String repliedThreadID = ((PostThreadForm) form).getRepliedThreadID();
final String notifyString = ((PostThreadForm) form).getNotify();
boolean notify = false;
if( notifyString != null && notifyString.equals("Y") ){
notify = true;
}
HttpSession session = request.getSession();
// Create a persistent thread object with the input data
// The reply is set to 1 by default for a new thread
long currentTime = System.currentTimeMillis();
final PersistentThread newThread
= new PersistentThread( subject,
content,
author,
currentTime,
Long.parseLong( parentID ),
Integer.parseInt( category ),
currentTime,
0,
Long.parseLong( repliedThreadID ), //reply
notify );
// Persist the thread object using ThreadDAO
try{
final ThreadDAO dao = DAOFactory.getInstance().getThreadDAO();
dao.addThread( newThread );
// If this is a reply post,check if we need to send out a notification email
if( Long.parseLong( repliedThreadID ) != -1 ){
final PersistentThread thread = dao.findByUID( Long.parseLong( repliedThreadID ) );
// If this thread allows notification,we send the author an email
if( thread.getNotify() ){
final AccountDAO accountDAO = DAOFactory.getInstance().getAccountDAO();
final Account account = accountDAO.findByUserName( thread.getAuthor() );
// Construct a message
final StringBuffer message = new StringBuffer();
message.append("<html>");
message.append("<META HTTP-EQUIV='Content-Type' CONTENT='text/html;CHARSET=gb2312'>");
message.append("<META HTTP-EQUIV='Content-Language' CONTENT='zh-CN'>");
message.append("<body>");
message.append("<img src='http://www.chinaxp.org/forum/images/logo.gif'>");
message.append("<br><br><b>");
message.append(subject);
message.append("</b>");
message.append("<br>");
message.append( StringUtils.escapeHTMLTagsButHref( content ) );
message.append("<br>");
final String url = "http://www.chinaxp.org/forum/viewThread.go?parentId=" + Long.parseLong( parentID ) + "&forum=" + Integer.parseInt( category );
message.append("<a href='" + url + "'>" + url + "</a>" );
message.append("</body>");
message.append("</html>");
// Spwan a thread to send email to avoid blocking
new MailAgent( account.getEmail(),"chinaxp@chinaxp.org", subject, message.toString() ).start();
}
}
}catch( final SQLException sqlException ){
sqlException.printStackTrace();
return (mapping.findForward("error"));
}catch( final ThreadNotFoundException threadNotFound ){
threadNotFound.printStackTrace();
return (mapping.findForward("error"));
}catch( final AccountNotFoundException accountNotFound ){
accountNotFound.printStackTrace();
return (mapping.findForward("error"));
}
// Report any errors we have discovered back to the original form
if (!errors.empty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
// Remove the obsolete form bean
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
// Forward control to the specified success URI
return ( new ActionForward( mapping.findForward("success").getPath()+"?forum=" + category, true));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -