KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > blog > TrackBackAction


1 /*
2  * Created on 20.12.2004
3  * Last commit $Date: 2005/01/17 21:36:03 $
4  * $Revision: 1.7 $
5  * $Author: michelson $
6  */

7
8 package com.j2biz.blogunity.web.actions.blog;
9
10 import java.util.Date JavaDoc;
11
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import com.j2biz.blogunity.dao.EntryDAO;
20 import com.j2biz.blogunity.dao.TrackbackDAO;
21 import com.j2biz.blogunity.exception.BlogunityException;
22 import com.j2biz.blogunity.pojo.Blog;
23 import com.j2biz.blogunity.pojo.Entry;
24 import com.j2biz.blogunity.pojo.Trackback;
25 import com.j2biz.blogunity.web.ActionResultFactory;
26 import com.j2biz.blogunity.web.IActionResult;
27 import com.j2biz.blogunity.web.actions.AbstractAction;
28
29 /**
30  * @author tag Juan Antonio Agudo This class implements the Movable Type (TM)
31  * TrackBack mechanism, as described in
32  * http://www.movabletype.org/docs/mttrackback.html
33  */

34 public class TrackBackAction extends AbstractAction {
35
36     private Blog blog;
37
38     private String JavaDoc entryId;
39
40     private static final Log log = LogFactory.getLog(TrackBackAction.class);
41
42     private static final IActionResult BLOG_FRONTPAGE_FORWARD = ActionResultFactory
43             .buildForward("/trackBackView.vm");
44
45     final static String JavaDoc _trackBackErrorResponse = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
46             + "<response>" + "<error>1</error>" + "<message>The error message</message>"
47             + "</response>";
48
49     final static String JavaDoc _trackBackSuccessResponse = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
50             + "<response>" + "<error>0</error>" + "</response>";
51
52     /**
53      * @param string
54      */

55     public TrackBackAction(Blog blog, String JavaDoc entryId) {
56         this.blog = blog;
57         this.entryId = entryId;
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
64      * javax.servlet.http.HttpServletResponse)
65      */

66     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
67             throws BlogunityException {
68
69         IActionResult errorRedirect = ActionResultFactory.buildRedirect("/blogs/"
70                 + blog.getUrlName());
71
72         // url is mandatory
73
// The permalink for the entry. Like any permalink, this should point as
74
// closely as possible to the actual entry on the HTML page, as it will
75
// be used when linking to the entry in question.
76
String JavaDoc tbUrl = (String JavaDoc) request.getParameter("url");
77
78         if (StringUtils.isEmpty(tbUrl)) return errorRedirect;
79
80         // The title of the entry.
81
String JavaDoc tbTitle = (String JavaDoc) request.getParameter("title");
82
83         // An excerpt of the entry. In the Movable Type implementation, if this
84
// string is longer than 255 characters, it will be cropped to 252
85
// characters, and "..." will be added to the end.
86
String JavaDoc tbExcerpt = (String JavaDoc) request.getParameter("excerpt");
87
88         // The name of the blog in which the entry is posted.
89
String JavaDoc tbBlogName = (String JavaDoc) request.getParameter("blog_name");
90
91         /*
92          * In the Movable Type implementation, of the above parameters only url
93          * is required. If title is not provided, the value for url will be set
94          * as the title.
95          */

96         if (StringUtils.isEmpty(tbTitle)) tbTitle = tbUrl;
97
98         Trackback tb = new Trackback();
99         tb.setDate(new Date JavaDoc(System.currentTimeMillis()));
100         tb.setDirection(Trackback.DIRECTION_INCOMING);
101         tb.setTrackbackContent(tbExcerpt);
102         tb.setUrl(tbUrl);
103         tb.setTitle(tbTitle);
104         tb.setLoggedIp(request.getRemoteAddr());
105         tb.setReferencedEntryId(new Long JavaDoc(Long.parseLong(this.entryId)));
106
107         (new TrackbackDAO()).createTrackback(tb);
108         Entry entry = (new EntryDAO()).getEntryByID(tb.getReferencedEntryId());
109         String JavaDoc str = "/blogs/" + blog.getUrlName() + entry.getPermalink();
110         return ActionResultFactory.buildRedirect(str);
111     }
112 }
Popular Tags