?? dlogaction.java
字號:
/*
* SiteAction.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Author: Winter Lau
* http://dlog4j.sourceforge.net
*/
package com.liusoft.dlog4j.action;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import com.liusoft.dlog4j.DLOGUserManager;
import com.liusoft.dlog4j.Globals;
import com.liusoft.dlog4j.HtmlNodeFilters;
import com.liusoft.dlog4j.MailTransportQueue;
import com.liusoft.dlog4j.SessionUserObject;
import com.liusoft.dlog4j.UserLoginManager;
import com.liusoft.dlog4j.beans.FriendBean;
import com.liusoft.dlog4j.beans.UserBean;
import com.liusoft.dlog4j.dao.UserDAO;
import com.liusoft.dlog4j.formbean.FormBean;
import com.liusoft.dlog4j.formbean.UserForm;
import com.liusoft.dlog4j.util.DateUtils;
import com.liusoft.dlog4j.util.ImageUtils;
import com.liusoft.dlog4j.util.MailSender;
import com.liusoft.dlog4j.util.RequestUtils;
import com.liusoft.dlog4j.util.StringUtils;
/**
* 個人網記相關的Action類
* @author Winter Lau
*/
public class DlogAction extends ActionBase {
private final static Log log = LogFactory.getLog(DlogAction.class);
/**
* 存放用戶頭像文件的路徑
*/
public final static String PORTRAIT_PATH = "/uploads/portrait/";
/**
* 用戶頭像的最大寬度
*/
public final static int PORTRAIT_WIDTH = 155;
/**
* 用戶頭像的最大高度
*/
public final static int PORTRAIT_HEIGHT = 200;
public final static int MIN_SITENAME_LEN = 3;
/**
* 獲取用戶密碼
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
protected ActionForward doFetchPwd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm uform = (UserForm) form;
validateClientId(request, uform);
ActionMessages msgs = new ActionMessages();
do{
if(StringUtils.isEmpty(uform.getName())){
msgs.add("name", new ActionMessage("error.username_empty"));
break;
}
UserBean ubean = UserDAO.getUserByName(uform.getName());
if(ubean==null){
msgs.add("name", new ActionMessage("error.user_not_found"));
break;
}
if (ubean.getStatus() != UserBean.STATUS_NORMAL) {
msgs.add("name", new ActionMessage("error.user_disabled"));
break;
}
if(!StringUtils.isEmail(ubean.getEmail())){
msgs.add("name", new ActionMessage("error.email_format"));
break;
}
//發送郵件
int siteid = (ubean.getSite()!=null)?ubean.getSite().getId():-1;
sendPasswordNotify(request, siteid, ubean);
msgs.add("name", new ActionMessage("mail.sent"));
break;
}while(true);
if(!msgs.isEmpty())
saveMessages(request, msgs);
return mapping.findForward("fetchpwd");
}
/**
* 發送忘記密碼郵件提醒
*
* @param request
* @param rbean
* @throws Exception
*/
protected void sendPasswordNotify(HttpServletRequest request, final int site_id,
final UserBean ubean) throws Exception {
//final String contextPath = request.getContextPath();
final String urlPrefix = RequestUtils.getUrlPrefix(request);
final String template = super.getPasswordTipTemplate();
new Thread() {
public void run() {
try {
// 發送郵件提醒
String notify_content = MessageFormat.format(template,
new String[]{ubean.getName(),ubean.getPassword(),urlPrefix});
Parser html = new Parser();
html.setEncoding(Globals.ENC_8859_1);
html.setInputHTML(notify_content);
Node[] nodes = html.extractAllNodesThatMatch(
HtmlNodeFilters.titleFilter).toNodeArray();
String title = nodes[0].toPlainTextString();
MailSender sender = MailSender.getHtmlMailSender(null, 25,
null, null);
sender.setSubject(title);
sender.setSendDate(new Date());
sender.setMailContent(notify_content);
sender.setMailTo(new String[] { ubean
.getContactInfo().getEmail() }, "to");
MailTransportQueue queue = (MailTransportQueue) getServlet()
.getServletContext().getAttribute(
Globals.MAIL_QUEUE);
// 寫入待發送郵件隊列
queue.write(site_id, sender.getMimeMessage());
if(log.isDebugEnabled())
log.debug("Notification mail was written to the sending queue.");
} catch (Exception e) {
log.error("send notification mail failed.", e);
}
}
}.start();
}
/**
* 添加好友
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
* @usage http://localhost/dlog/user.do?sid=1&uid=223&eventSubmit_AddFriend&fromPage=xxxx
* @ajax_enabled
*/
protected ActionForward doAddFriend(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FormBean fbean = (FormBean) form;
validateClientId(request, fbean);
String msg = null;
//判斷用戶是否登錄
SessionUserObject loginUser = super.getLoginUser(request, response, false);
if(loginUser != null){
int friendId = RequestUtils.getParam(request, "uid", -1);
UserBean friend = UserDAO.getUserByID(friendId);
if(friend==null)
msg = getMessage(request,null,"error.user_not_found", new Integer(friendId));
else if(friendId == loginUser.getId())
msg = getMessage(request,null,"error.cannot_add_myself");
else{
FriendBean fb = new FriendBean();
fb.setAddTime(new Date());
fb.setFriend(friend);
fb.setOwner(loginUser.getId());
fb.setType(FriendBean.TYPE_GENERAL);
fb.setRole(FriendBean.ROLE_GENERAL);
UserDAO.addFriend(fb);
msg = getMessage(request,null,"error.friend_added", new Integer(friendId));
}
}
else
msg = getMessage(request,null,"error.user_not_login");
return msgbox(mapping,form,request,response,msg,fbean.getFromPage());
}
/**
* 阻止用戶(加入黑名單)
* @param mapping
* @param form
* @param request
* @param response
* @param s_other_id
* @return
* @throws Exception
* @ajax_enabled
*/
protected ActionForward doBlockUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm fbean = (UserForm)form;
super.validateClientId(request, fbean);
String msg = null;
//判斷用戶是否登錄
SessionUserObject loginUser = super.getLoginUser(request, response, false);
if(loginUser!=null){
String s_id = String.valueOf(fbean.getId());
if(UserDAO.isUserInBlackList(loginUser.getId(), fbean.getId())){
msg = getMessage(request,null,"error.user_already_in_block", s_id);
}
else{
UserDAO.addBlackList(loginUser.getId(), fbean.getId(), 0);
msg = getMessage(request,null,"error.user_added_to_block", s_id);
}
}
else
msg = getMessage(request,null,"error.user_not_login");
request.setAttribute("msg", msg);
return makeForward(mapping.findForward("blacklist"),fbean.getSid());
}
/**
* 取消阻止(從黑名單中刪除)
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
* http://localhost/dlog/user.do?sid=1&uid=223&eventSubmit_DelFriend
*/
protected ActionForward doUnblockUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FormBean fbean = (FormBean) form;
validateClientId(request, fbean);
String msg = null;
//判斷用戶是否登錄
SessionUserObject loginUser = super.getLoginUser(request, response, false);
if(loginUser != null){
String[] uids = request.getParameterValues("uid");
UserDAO.deleteBlacklist(loginUser.getId(), uids);
msg = getMessage(request,null,"error.user_delete_from_block", "");
}
else
msg = getMessage(request,null,"error.user_not_login");
return msgbox(mapping,form,request,response,msg,fbean.getFromPage());
}
/**
* 刪除好友
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
* http://localhost/dlog/user.do?sid=1&uid=223&eventSubmit_DelFriend
*/
protected ActionForward doDelFriend(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FormBean fbean = (FormBean) form;
validateClientId(request, fbean);
String msg = null;
//判斷用戶是否登錄
SessionUserObject loginUser = super.getLoginUser(request, response, false);
if(loginUser != null){
String[] uids = request.getParameterValues("uid");
int er = UserDAO.deleteFriend(loginUser.getId(), uids);
if(er>0)
msg = getMessage(request,null,"error.friend_deleted");
}
else
msg = getMessage(request,null,"error.user_not_login");
return msgbox(mapping,form,request,response,msg,fbean.getFromPage());
}
/**
* 用戶登錄并注冊個人網記
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
* @see com.liusoft.dlog4j.action.UserAction#doLogin(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)
*/
protected ActionForward doLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
int own_site_id = -1;
UserForm user = (UserForm) form;
validateClientId(request, user);
ActionMessages msgs = new ActionMessages();
if (StringUtils.isEmpty(user.getName()))
msgs.add("username", new ActionMessage("error.username_empty"));
else if (StringUtils.isEmpty(user.getPassword()))
msgs.add("password", new ActionMessage("error.password_empty"));
else {
UserBean ubean = DLOGUserManager.getUserByName(user.getName());
if (ubean == null
|| !StringUtils.equals(ubean.getPassword(), user
.getPassword()))
msgs.add("password",
new ActionMessage("error.user_auth_failed"));
else if (ubean.getStatus() != UserBean.STATUS_NORMAL) {
msgs.add("password", new ActionMessage("error.user_disabled"));
} else {
// 執行登錄過程
UserLoginManager.loginUser(request, response, ubean, user.getKeepDays());
own_site_id = ubean.getOwnSiteId();
}
}
if (!msgs.isEmpty()) {
saveMessages(request, msgs);
return mapping.findForward("login");
}
String fromPage = user.getFromPage();
if (StringUtils.isNotEmpty(fromPage))
return new ActionForward(fromPage, true);
if(user.getSid()>0)
return makeForward(mapping.findForward("main"), user.getSid());
if(own_site_id < 1)
return mapping.findForward("home");
return makeForward(mapping.findForward("home"), own_site_id);
}
/**
* 用戶注銷,清除COOKIE
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
protected ActionForward doLogout(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
String fromPage) throws Exception {
//執行注銷過程
UserLoginManager.logoutUser(request, response);
if (StringUtils.isNotEmpty(fromPage))
return new ActionForward(fromPage, true);
return mapping.findForward("home");
}
private static String g_portrait_path;
private static String g_portrait_uri;
/**
* 獲取頭像的存放路徑
*/
private synchronized void initPortraitPath(){
if(g_portrait_uri!=null)
return;
g_portrait_uri = getServlet().getInitParameter("portrait_base_uri");
String portrait_path = this.getServlet().getInitParameter("portrait_base_path");
if(portrait_path.startsWith(Globals.LOCAL_PATH_PREFIX))
g_portrait_path = portrait_path.substring(Globals.LOCAL_PATH_PREFIX.length());
else if(portrait_path.startsWith("/"))
g_portrait_path = getServlet().getServletContext().getRealPath(portrait_path);
else
g_portrait_path = portrait_path;
if(!g_portrait_path.endsWith(File.separator))
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -