KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > action > DlogTrackBackAction


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.action;
17
18 import java.util.Date JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import net.sf.hibernate.Session;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.struts.action.ActionErrors;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30
31 import dlog4j.formbean.TrackBackForm;
32
33 /**
34  * 用于处理TrackBack的请求
35  * @author Winter Lau
36  */

37 public class DlogTrackBackAction extends DlogActionBase {
38
39     /**
40      *
41      */

42     public ActionForward execute(ActionMapping mapping, ActionForm form,
43             HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws Exception JavaDoc
44     {
45         TrackBackForm tbf = (TrackBackForm)form;
46         ActionErrors errors = new ActionErrors();
47         Session ssn = null;
48         String JavaDoc msg = validate(tbf);
49         if(msg==null){
50             try{
51                 ssn = getSession();
52                 tbf.setRefTime(new Date JavaDoc());
53                 tbf.setRemoteAddr(req.getRemoteAddr());
54                 ssn.save(tbf);
55             }catch(Exception JavaDoc e){
56                 getServlet().log("DlogTrackBackAction.doDefault", e);
57                 msg = e.getMessage();
58             }finally{
59                 commitSession(ssn,true);
60             }
61         }
62         String JavaDoc xml = getResponse(msg!=null, msg);
63         res.getWriter().print(xml);
64         return null;
65     }
66     
67
68     /**
69      * 验证输入的有效性
70      */

71     protected String JavaDoc validate(TrackBackForm form) {
72         if(StringUtils.isEmpty(form.getUrl()))
73             return "url is empty";
74         else
75         if(form.getLog_id()<0)
76             return "Illegal value of log_id";
77         else
78         if(StringUtils.isEmpty(form.getBlog_name()))
79             return "Blog_name is empty";
80         else
81         if(StringUtils.isEmpty(form.getTitle()))
82             return "Title is empty";
83         return null;
84     }
85     
86     
87     /**
88      * 得到TrackBack的反馈信息
89      * @param error
90      * @param msg
91      * @return
92      */

93     protected String JavaDoc getResponse(boolean error, String JavaDoc msg){
94         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
95         xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
96         xml.append("<response>");
97         if(error){
98             xml.append("<error>1</error>");
99             xml.append("<message>");
100             xml.append(msg);
101             xml.append("</message>");
102         }
103         else
104             xml.append("<error>0</error>");
105         xml.append("</response>");
106         return xml.toString();
107     }
108     
109 }
110
Popular Tags