001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.action;
017:
018: import java.util.Date;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import net.sf.hibernate.Session;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.struts.action.ActionErrors;
027: import org.apache.struts.action.ActionForm;
028: import org.apache.struts.action.ActionForward;
029: import org.apache.struts.action.ActionMapping;
030:
031: import dlog4j.formbean.TrackBackForm;
032:
033: /**
034: * 用于处理TrackBack的请求
035: * @author Winter Lau
036: */
037: public class DlogTrackBackAction extends DlogActionBase {
038:
039: /**
040: *
041: */
042: public ActionForward execute(ActionMapping mapping,
043: ActionForm form, HttpServletRequest req,
044: HttpServletResponse res) throws Exception {
045: TrackBackForm tbf = (TrackBackForm) form;
046: ActionErrors errors = new ActionErrors();
047: Session ssn = null;
048: String msg = validate(tbf);
049: if (msg == null) {
050: try {
051: ssn = getSession();
052: tbf.setRefTime(new Date());
053: tbf.setRemoteAddr(req.getRemoteAddr());
054: ssn.save(tbf);
055: } catch (Exception e) {
056: getServlet().log("DlogTrackBackAction.doDefault", e);
057: msg = e.getMessage();
058: } finally {
059: commitSession(ssn, true);
060: }
061: }
062: String xml = getResponse(msg != null, msg);
063: res.getWriter().print(xml);
064: return null;
065: }
066:
067: /**
068: * 验证输入的有效性
069: */
070: protected String validate(TrackBackForm form) {
071: if (StringUtils.isEmpty(form.getUrl()))
072: return "url is empty";
073: else if (form.getLog_id() < 0)
074: return "Illegal value of log_id";
075: else if (StringUtils.isEmpty(form.getBlog_name()))
076: return "Blog_name is empty";
077: else if (StringUtils.isEmpty(form.getTitle()))
078: return "Title is empty";
079: return null;
080: }
081:
082: /**
083: * 得到TrackBack的反馈信息
084: * @param error
085: * @param msg
086: * @return
087: */
088: protected String getResponse(boolean error, String msg) {
089: StringBuffer xml = new StringBuffer();
090: xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
091: xml.append("<response>");
092: if (error) {
093: xml.append("<error>1</error>");
094: xml.append("<message>");
095: xml.append(msg);
096: xml.append("</message>");
097: } else
098: xml.append("<error>0</error>");
099: xml.append("</response>");
100: return xml.toString();
101: }
102:
103: }
|